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
…#12839)

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

* fix wrong test case

* Update documentation
  • Loading branch information
yeonjuan committed Feb 28, 2020
1 parent 4af06fc commit 051567a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 58 deletions.
71 changes: 14 additions & 57 deletions docs/rules/id-length.md
Expand Up @@ -31,6 +31,9 @@ var myObj = { a: 1 };
class x { }
class Foo { x() {} }
function foo(...x) { }
function foo([x]) { }
var [x] = arr;
var { prop: [x]} = {};
function foo({x}) { }
var { x } = {};
var { prop: a} = {};
Expand Down Expand Up @@ -59,71 +62,19 @@ function foo(num = 0) { }
class MyClass { }
class Foo { method() {} }
function foo(...args) { }
function foo([longName]) { }
var { prop } = {};
var { prop: [longName] } = {};
var [longName] = arr;
function foo({ prop }) { }
function foo({ a: prop }) { }
var { prop } = {};
var { a: prop } = {};
var { prop: [x] } = {};
({ prop: obj.longName } = {});
var data = { "x": 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access
```

This rule has a shorthand integer option for the `"min"` object property.

Examples of **incorrect** code for this rule with a minimum of 4:

```js
/*eslint id-length: ["error", { "min": 4 }]*/
/*eslint-env es6*/

var val = 5;
obj.e = document.body;
function foo (e) { };
try {
dangerousStuff();
} catch (e) {
// ignore as many do
}
var myObj = { a: 1 };
(val) => { val * val };
class x { }
class Foo { x() {} }
function foo(...x) { }
var { x } = {};
var { prop: x} = {};
({ prop: obj.x } = {});
```

Examples of **correct** code for this rule with a minimum of 4:

```js
/*eslint id-length: ["error", { "min": 4 }]*/
/*eslint-env es6*/

var value = 5;
function func() { return 42; }
obj.element = document.body;
var foobar = function (event) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
// ignore as many do
}
var myObj = { apple: 1 };
(value) => { value * value };
function foobar(value = 0) { }
class MyClass { }
class Foobar { method() {} }
function foobar(...args) { }
var { prop } = {};
var { a: longName } = {};
var { prop: [x] } = {};
({ prop: obj.name } = {});
var data = { "x": 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access
```

This rule has an object option:

* `"min"` (default: 2) enforces a minimum identifier length
Expand Down Expand Up @@ -155,6 +106,8 @@ class Foo { x() {} }
function foo(...x) { }
var { x } = {};
var { prop: a} = {};
var [x] = arr;
var { prop: [x]} = {};
({ prop: obj.x } = {});
```

Expand All @@ -180,8 +133,9 @@ class MyClass { }
class Foobar { method() {} }
function foobar(...args) { }
var { prop } = {};
var [longName] = foo;
var { a: [prop] } = {};
var { a: longName } = {};
var { prop: [x] } = {};
({ prop: obj.name } = {});
var data = { "x": 1 }; // excused because of quotes
data["y"] = 3; // excused because of calculated property access
Expand All @@ -205,6 +159,7 @@ try {
// ignore as many do
}
(reallyLongArgName) => { return !reallyLongArgName; };
var [reallyLongFirstElementName] = arr;
```

Examples of **correct** code for this rule with the `{ "max": 10 }` option:
Expand All @@ -223,6 +178,7 @@ try {
// ignore as many do
}
(arg) => { return !arg; };
var [first] = arr;
```

### properties
Expand Down Expand Up @@ -256,6 +212,7 @@ try {
// ignore as many do
}
(x) => { return x * x; };
var [x] = arr;
const { x } = foo;
const { a: x } = foo;
```
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/id-length.js
Expand Up @@ -100,7 +100,8 @@ module.exports = {
ClassDeclaration: true,
FunctionDeclaration: true,
MethodDefinition: true,
CatchClause: true
CatchClause: true,
ArrayPattern: true
};

return {
Expand Down
19 changes: 19 additions & 0 deletions tests/lib/rules/id-length.js
Expand Up @@ -72,6 +72,8 @@ ruleTester.run("id-length", rule, {
{ 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: "var arr = [i,j,f,b]", parserOptions: { ecmaVersion: 6 } },
{ code: "function foo([arr]) {}", parserOptions: { ecmaVersion: 6 } },
{ code: "var {x} = foo;", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } },
{ code: "var {x, y: {z}} = foo;", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } },
{ code: "let foo = { [a]: 1 };", options: [{ properties: "always" }], parserOptions: { ecmaVersion: 6 } },
Expand All @@ -88,6 +90,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 @@ -252,6 +257,20 @@ ruleTester.run("id-length", rule, {
tooShortError
]
},
{
code: "var { prop: [x] } = {};",
parserOptions: { ecmaVersion: 6 },
errors: [
tooShortError
]
},
{
code: "var { prop: [[x]] } = {};",
parserOptions: { ecmaVersion: 6 },
errors: [
tooShortError
]
},
{
code: "var { prop: longName } = {};",
options: [{ min: 3, max: 5 }],
Expand Down

0 comments on commit 051567a

Please sign in to comment.