Skip to content

Commit

Permalink
Update: check identifier in array pattern in id-length (fixes #12832)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Jan 28, 2020
1 parent aea1729 commit 41ec2dc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 5 additions & 1 deletion docs/rules/id-length.md
Expand Up @@ -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 } = {});
```

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/id-length.js
Expand Up @@ -92,7 +92,8 @@ module.exports = {
ClassDeclaration: true,
FunctionDeclaration: true,
MethodDefinition: true,
CatchClause: true
CatchClause: true,
ArrayPattern: true
};

return {
Expand Down
23 changes: 21 additions & 2 deletions tests/lib/rules/id-length.js
Expand Up @@ -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 } },
Expand All @@ -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] },
Expand All @@ -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 }],
Expand Down Expand Up @@ -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
]
Expand Down

0 comments on commit 41ec2dc

Please sign in to comment.