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