From 41ec2dc23a0af54574bde1db8cdf72a2ffd1cbe0 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Tue, 28 Jan 2020 20:49:18 +0900 Subject: [PATCH] Update: check identifier in array pattern in id-length (fixes #12832) --- docs/rules/id-length.md | 6 +++++- lib/rules/id-length.js | 3 ++- tests/lib/rules/id-length.js | 23 +++++++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/docs/rules/id-length.md b/docs/rules/id-length.md index d49435d0b3e..39c686d8ae5 100644 --- a/docs/rules/id-length.md +++ b/docs/rules/id-length.md @@ -31,9 +31,11 @@ var myObj = { a: 1 }; class x { } class Foo { x() {} } function foo(...x) { } +function foo([x]) { } var { x } = {}; var { x: a} = {}; var { a: [x]} = {}; +var [x] = arr; ({ prop: obj.x } = {}); ``` @@ -59,9 +61,11 @@ function foo(num = 0) { } class MyClass { } class Foo { method() {} } function foo(...args) { } +function foo([longName]) { } var { prop } = {}; var { prop: a } = {}; -var { prop: [x] } = {}; +var { prop: [longName] } = {}; +var [longName] = arr; ({ prop: obj.longName } = {}); var data = { "x": 1 }; // excused because of quotes data["y"] = 3; // excused because of calculated property access diff --git a/lib/rules/id-length.js b/lib/rules/id-length.js index c8586ea3481..956814a6e78 100644 --- a/lib/rules/id-length.js +++ b/lib/rules/id-length.js @@ -92,7 +92,8 @@ module.exports = { ClassDeclaration: true, FunctionDeclaration: true, MethodDefinition: true, - CatchClause: true + CatchClause: true, + ArrayPattern: true }; return { diff --git a/tests/lib/rules/id-length.js b/tests/lib/rules/id-length.js index 3800fa16986..7ce6fa192a4 100644 --- a/tests/lib/rules/id-length.js +++ b/tests/lib/rules/id-length.js @@ -51,7 +51,6 @@ ruleTester.run("id-length", rule, { { code: "function foo(...args) { }", parserOptions: { ecmaVersion: 6 } }, { code: "var { prop } = {};", parserOptions: { ecmaVersion: 6 } }, { code: "var { prop: a } = {};", parserOptions: { ecmaVersion: 6 } }, - { code: "var { prop: [x] } = {};", parserOptions: { ecmaVersion: 6 } }, { code: "import something from 'y';", parserOptions: { ecmaVersion: 6, sourceType: "module" } }, { code: "export var num = 0;", parserOptions: { ecmaVersion: 6, sourceType: "module" } }, { code: "({ prop: obj.x.y.something } = {});", parserOptions: { ecmaVersion: 6 } }, @@ -63,7 +62,9 @@ ruleTester.run("id-length", rule, { { code: "var obj = { aaaaa: 1 };", options: [{ max: 4, properties: "never" }] }, { code: "var obj = {}; obj.aaaaa = 1;", options: [{ max: 4, properties: "never" }] }, { code: "({ a: obj.x.y.z } = {});", options: [{ max: 4, properties: "never" }], parserOptions: { ecmaVersion: 6 } }, - { code: "({ prop: obj.xxxxx } = {});", options: [{ max: 4, properties: "never" }], parserOptions: { ecmaVersion: 6 } } + { code: "({ prop: obj.xxxxx } = {});", options: [{ max: 4, properties: "never" }], parserOptions: { ecmaVersion: 6 } }, + { code: "var arr = [i,j,f,b]", parserOptions: { ecmaVersion: 6 } }, + { code: "function foo([arr]) {}", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError, tooShortError] } ], invalid: [ { code: "var x = 1;", errors: [tooShortError] }, @@ -76,6 +77,9 @@ ruleTester.run("id-length", rule, { { code: "var handler = function (e) {};", errors: [tooShortError] }, { code: "for (var i=0; i < 10; i++) { console.log(i); }", errors: [tooShortError] }, { code: "var j=0; while (j > -10) { console.log(--j); }", errors: [tooShortError] }, + { code: "var [i] = arr;", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError] }, + { code: "var [,i,a] = arr;", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError, tooShortError] }, + { code: "function foo([a]) {}", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError] }, { code: "var _$xt_$ = Foo(42)", options: [{ min: 2, max: 4 }], @@ -143,6 +147,21 @@ ruleTester.run("id-length", rule, { { code: "var { a: [x]} = {};", parserOptions: { ecmaVersion: 6 }, + errors: [ + tooShortError, + tooShortError + ] + }, + { + code: "var { prop: [x] } = {};", + parserOptions: { ecmaVersion: 6 }, + errors: [ + tooShortError + ] + }, + { + code: "var { prop: [[x]] } = {};", + parserOptions: { ecmaVersion: 6 }, errors: [ tooShortError ]