11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciif ((!common.hasCrypto) || (!common.hasIntl)) { 51cb0ef41Sopenharmony_ci common.skip('ESLint tests require crypto and Intl'); 61cb0ef41Sopenharmony_ci} 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_cicommon.skipIfEslintMissing(); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst { RuleTester } = require('../../tools/node_modules/eslint'); 111cb0ef41Sopenharmony_ciconst rule = require('../../tools/eslint-rules/no-array-destructuring'); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst USE_OBJ_DESTRUCTURING = 141cb0ef41Sopenharmony_ci 'Use object destructuring instead of array destructuring.'; 151cb0ef41Sopenharmony_ciconst USE_ARRAY_METHODS = 161cb0ef41Sopenharmony_ci 'Use primordials.ArrayPrototypeSlice to avoid unsafe array iteration.'; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cinew RuleTester({ 191cb0ef41Sopenharmony_ci parserOptions: { ecmaVersion: 2021 }, 201cb0ef41Sopenharmony_ci env: { es6: true } 211cb0ef41Sopenharmony_ci}) 221cb0ef41Sopenharmony_ci .run('no-array-destructuring', rule, { 231cb0ef41Sopenharmony_ci valid: [ 241cb0ef41Sopenharmony_ci 'const first = [1, 2, 3][0];', 251cb0ef41Sopenharmony_ci 'const {0:first} = [1, 2, 3];', 261cb0ef41Sopenharmony_ci '({1:elem} = array);', 271cb0ef41Sopenharmony_ci 'function name(param, { 0: key, 1: value },) {}', 281cb0ef41Sopenharmony_ci ], 291cb0ef41Sopenharmony_ci invalid: [ 301cb0ef41Sopenharmony_ci { 311cb0ef41Sopenharmony_ci code: 'const [Array] = args;', 321cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 331cb0ef41Sopenharmony_ci output: 'const {0:Array} = args;' 341cb0ef41Sopenharmony_ci }, 351cb0ef41Sopenharmony_ci { 361cb0ef41Sopenharmony_ci code: 'const [ , res] = args;', 371cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 381cb0ef41Sopenharmony_ci output: 'const { 1:res} = args;', 391cb0ef41Sopenharmony_ci }, 401cb0ef41Sopenharmony_ci { 411cb0ef41Sopenharmony_ci code: '[, elem] = options;', 421cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 431cb0ef41Sopenharmony_ci output: '({ 1:elem} = options);', 441cb0ef41Sopenharmony_ci }, 451cb0ef41Sopenharmony_ci { 461cb0ef41Sopenharmony_ci code: 'const {values:[elem]} = options;', 471cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 481cb0ef41Sopenharmony_ci output: 'const {values:{0:elem}} = options;', 491cb0ef41Sopenharmony_ci }, 501cb0ef41Sopenharmony_ci { 511cb0ef41Sopenharmony_ci code: '[[[elem]]] = options;', 521cb0ef41Sopenharmony_ci errors: [ 531cb0ef41Sopenharmony_ci { message: USE_OBJ_DESTRUCTURING }, 541cb0ef41Sopenharmony_ci { message: USE_OBJ_DESTRUCTURING }, 551cb0ef41Sopenharmony_ci { message: USE_OBJ_DESTRUCTURING }, 561cb0ef41Sopenharmony_ci ], 571cb0ef41Sopenharmony_ci output: '({0:[[elem]]} = options);', 581cb0ef41Sopenharmony_ci }, 591cb0ef41Sopenharmony_ci { 601cb0ef41Sopenharmony_ci code: '[, ...rest] = options;', 611cb0ef41Sopenharmony_ci errors: [{ message: USE_ARRAY_METHODS }], 621cb0ef41Sopenharmony_ci }, 631cb0ef41Sopenharmony_ci { 641cb0ef41Sopenharmony_ci code: 'for(const [key, value] of new Map);', 651cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 661cb0ef41Sopenharmony_ci output: 'for(const {0:key, 1:value} of new Map);', 671cb0ef41Sopenharmony_ci }, 681cb0ef41Sopenharmony_ci { 691cb0ef41Sopenharmony_ci code: 'let [first,,,fourth] = array;', 701cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 711cb0ef41Sopenharmony_ci output: 'let {0:first,3:fourth} = array;', 721cb0ef41Sopenharmony_ci }, 731cb0ef41Sopenharmony_ci { 741cb0ef41Sopenharmony_ci code: 'let [,second,,fourth] = array;', 751cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 761cb0ef41Sopenharmony_ci output: 'let {1:second,3:fourth} = array;', 771cb0ef41Sopenharmony_ci }, 781cb0ef41Sopenharmony_ci { 791cb0ef41Sopenharmony_ci code: 'let [ ,,,fourth ] = array;', 801cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 811cb0ef41Sopenharmony_ci output: 'let { 3:fourth } = array;', 821cb0ef41Sopenharmony_ci }, 831cb0ef41Sopenharmony_ci { 841cb0ef41Sopenharmony_ci code: 'let [,,,fourth, fifth,, minorFall, majorLift,...music] = arr;', 851cb0ef41Sopenharmony_ci errors: [{ message: USE_ARRAY_METHODS }], 861cb0ef41Sopenharmony_ci }, 871cb0ef41Sopenharmony_ci { 881cb0ef41Sopenharmony_ci code: 'function map([key, value]) {}', 891cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 901cb0ef41Sopenharmony_ci output: 'function map({0:key, 1:value}) {}', 911cb0ef41Sopenharmony_ci }, 921cb0ef41Sopenharmony_ci { 931cb0ef41Sopenharmony_ci code: 'function map([key, value],) {}', 941cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 951cb0ef41Sopenharmony_ci output: 'function map({0:key, 1:value},) {}', 961cb0ef41Sopenharmony_ci }, 971cb0ef41Sopenharmony_ci { 981cb0ef41Sopenharmony_ci code: '(function([key, value]) {})', 991cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 1001cb0ef41Sopenharmony_ci output: '(function({0:key, 1:value}) {})', 1011cb0ef41Sopenharmony_ci }, 1021cb0ef41Sopenharmony_ci { 1031cb0ef41Sopenharmony_ci code: '(function([key, value] = [null, 0]) {})', 1041cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 1051cb0ef41Sopenharmony_ci output: '(function({0:key, 1:value} = [null, 0]) {})', 1061cb0ef41Sopenharmony_ci }, 1071cb0ef41Sopenharmony_ci { 1081cb0ef41Sopenharmony_ci code: 'function map([key, ...values]) {}', 1091cb0ef41Sopenharmony_ci errors: [{ message: USE_ARRAY_METHODS }], 1101cb0ef41Sopenharmony_ci }, 1111cb0ef41Sopenharmony_ci { 1121cb0ef41Sopenharmony_ci code: 'function map([key, value], ...args) {}', 1131cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 1141cb0ef41Sopenharmony_ci output: 'function map({0:key, 1:value}, ...args) {}', 1151cb0ef41Sopenharmony_ci }, 1161cb0ef41Sopenharmony_ci { 1171cb0ef41Sopenharmony_ci code: 'async function map([key, value], ...args) {}', 1181cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 1191cb0ef41Sopenharmony_ci output: 'async function map({0:key, 1:value}, ...args) {}', 1201cb0ef41Sopenharmony_ci }, 1211cb0ef41Sopenharmony_ci { 1221cb0ef41Sopenharmony_ci code: 'async function* generator([key, value], ...args) {}', 1231cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 1241cb0ef41Sopenharmony_ci output: 'async function* generator({0:key, 1:value}, ...args) {}', 1251cb0ef41Sopenharmony_ci }, 1261cb0ef41Sopenharmony_ci { 1271cb0ef41Sopenharmony_ci code: 'function* generator([key, value], ...args) {}', 1281cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 1291cb0ef41Sopenharmony_ci output: 'function* generator({0:key, 1:value}, ...args) {}', 1301cb0ef41Sopenharmony_ci }, 1311cb0ef41Sopenharmony_ci { 1321cb0ef41Sopenharmony_ci code: 'const cb = ([key, value], ...args) => {}', 1331cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 1341cb0ef41Sopenharmony_ci output: 'const cb = ({0:key, 1:value}, ...args) => {}', 1351cb0ef41Sopenharmony_ci }, 1361cb0ef41Sopenharmony_ci { 1371cb0ef41Sopenharmony_ci code: 'class name{ method([key], ...args){} }', 1381cb0ef41Sopenharmony_ci errors: [{ message: USE_OBJ_DESTRUCTURING }], 1391cb0ef41Sopenharmony_ci output: 'class name{ method({0:key}, ...args){} }', 1401cb0ef41Sopenharmony_ci }, 1411cb0ef41Sopenharmony_ci ] 1421cb0ef41Sopenharmony_ci }); 143