• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7common.skipIfEslintMissing();
8
9const { RuleTester } = require('../../tools/node_modules/eslint');
10const rule = require('../../tools/eslint-rules/no-array-destructuring');
11
12const USE_OBJ_DESTRUCTURING =
13  'Use object destructuring instead of array destructuring.';
14const USE_ARRAY_METHODS =
15  'Use primordials.ArrayPrototypeSlice to avoid unsafe array iteration.';
16
17new RuleTester({
18  parserOptions: { ecmaVersion: 2021 },
19  env: { es6: true }
20})
21  .run('no-array-destructuring', rule, {
22    valid: [
23      'const first = [1, 2, 3][0];',
24      'const {0:first} = [1, 2, 3];',
25      '({1:elem} = array);',
26      'function name(param, { 0: key, 1: value },) {}',
27    ],
28    invalid: [
29      {
30        code: 'const [Array] = args;',
31        errors: [{ message: USE_OBJ_DESTRUCTURING }],
32        output: 'const {0:Array} = args;'
33      },
34      {
35        code: 'const [ , res] = args;',
36        errors: [{ message: USE_OBJ_DESTRUCTURING }],
37        output: 'const {  1:res} = args;',
38      },
39      {
40        code: '[, elem] = options;',
41        errors: [{ message: USE_OBJ_DESTRUCTURING }],
42        output: '({ 1:elem} = options);',
43      },
44      {
45        code: 'const {values:[elem]} = options;',
46        errors: [{ message: USE_OBJ_DESTRUCTURING }],
47        output: 'const {values:{0:elem}} = options;',
48      },
49      {
50        code: '[[[elem]]] = options;',
51        errors: [
52          { message: USE_OBJ_DESTRUCTURING },
53          { message: USE_OBJ_DESTRUCTURING },
54          { message: USE_OBJ_DESTRUCTURING },
55        ],
56        output: '({0:[[elem]]} = options);',
57      },
58      {
59        code: '[, ...rest] = options;',
60        errors: [{ message: USE_ARRAY_METHODS }],
61      },
62      {
63        code: 'for(const [key, value] of new Map);',
64        errors: [{ message: USE_OBJ_DESTRUCTURING }],
65        output: 'for(const {0:key, 1:value} of new Map);',
66      },
67      {
68        code: 'let [first,,,fourth] = array;',
69        errors: [{ message: USE_OBJ_DESTRUCTURING }],
70        output: 'let {0:first,3:fourth} = array;',
71      },
72      {
73        code: 'let [,second,,fourth] = array;',
74        errors: [{ message: USE_OBJ_DESTRUCTURING }],
75        output: 'let {1:second,3:fourth} = array;',
76      },
77      {
78        code: 'let [ ,,,fourth ] = array;',
79        errors: [{ message: USE_OBJ_DESTRUCTURING }],
80        output: 'let { 3:fourth } = array;',
81      },
82      {
83        code: 'let [,,,fourth, fifth,, minorFall, majorLift,...music] = arr;',
84        errors: [{ message: USE_ARRAY_METHODS }],
85      },
86      {
87        code: 'function map([key, value]) {}',
88        errors: [{ message: USE_OBJ_DESTRUCTURING }],
89        output: 'function map({0:key, 1:value}) {}',
90      },
91      {
92        code: 'function map([key, value],) {}',
93        errors: [{ message: USE_OBJ_DESTRUCTURING }],
94        output: 'function map({0:key, 1:value},) {}',
95      },
96      {
97        code: '(function([key, value]) {})',
98        errors: [{ message: USE_OBJ_DESTRUCTURING }],
99        output: '(function({0:key, 1:value}) {})',
100      },
101      {
102        code: '(function([key, value] = [null, 0]) {})',
103        errors: [{ message: USE_OBJ_DESTRUCTURING }],
104        output: '(function({0:key, 1:value} = [null, 0]) {})',
105      },
106      {
107        code: 'function map([key, ...values]) {}',
108        errors: [{ message: USE_ARRAY_METHODS }],
109      },
110      {
111        code: 'function map([key, value], ...args) {}',
112        errors: [{ message: USE_OBJ_DESTRUCTURING }],
113        output: 'function map({0:key, 1:value}, ...args) {}',
114      },
115      {
116        code: 'async function map([key, value], ...args) {}',
117        errors: [{ message: USE_OBJ_DESTRUCTURING }],
118        output: 'async function map({0:key, 1:value}, ...args) {}',
119      },
120      {
121        code: 'async function* generator([key, value], ...args) {}',
122        errors: [{ message: USE_OBJ_DESTRUCTURING }],
123        output: 'async function* generator({0:key, 1:value}, ...args) {}',
124      },
125      {
126        code: 'function* generator([key, value], ...args) {}',
127        errors: [{ message: USE_OBJ_DESTRUCTURING }],
128        output: 'function* generator({0:key, 1:value}, ...args) {}',
129      },
130      {
131        code: 'const cb = ([key, value], ...args) => {}',
132        errors: [{ message: USE_OBJ_DESTRUCTURING }],
133        output: 'const cb = ({0:key, 1:value}, ...args) => {}',
134      },
135      {
136        code: 'class name{ method([key], ...args){} }',
137        errors: [{ message: USE_OBJ_DESTRUCTURING }],
138        output: 'class name{ method({0:key}, ...args){} }',
139      },
140    ]
141  });
142