Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: check identifier in array pattern in id-length (fixes #12832) #12839

Merged
merged 4 commits into from Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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] }
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
],
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,
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved
tooShortError
]
},
{
code: "var { prop: [x] } = {};",
parserOptions: { ecmaVersion: 6 },
errors: [
tooShortError
]
},
{
code: "var { prop: [[x]] } = {};",
parserOptions: { ecmaVersion: 6 },
errors: [
tooShortError
]
Expand Down