Skip to content

Commit 051567a

Browse files
authoredFeb 28, 2020
Update: check identifier in array pattern in id-length (fixes #12832) (#12839)
* Update: check identifier in array pattern in id-length (fixes #12832) * fix wrong test case * Update documentation
1 parent 4af06fc commit 051567a

File tree

3 files changed

+35
-58
lines changed

3 files changed

+35
-58
lines changed
 

‎docs/rules/id-length.md

+14-57
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var myObj = { a: 1 };
3131
class x { }
3232
class Foo { x() {} }
3333
function foo(...x) { }
34+
function foo([x]) { }
35+
var [x] = arr;
36+
var { prop: [x]} = {};
3437
function foo({x}) { }
3538
var { x } = {};
3639
var { prop: a} = {};
@@ -59,71 +62,19 @@ function foo(num = 0) { }
5962
class MyClass { }
6063
class Foo { method() {} }
6164
function foo(...args) { }
65+
function foo([longName]) { }
66+
var { prop } = {};
67+
var { prop: [longName] } = {};
68+
var [longName] = arr;
6269
function foo({ prop }) { }
6370
function foo({ a: prop }) { }
6471
var { prop } = {};
6572
var { a: prop } = {};
66-
var { prop: [x] } = {};
6773
({ prop: obj.longName } = {});
6874
var data = { "x": 1 }; // excused because of quotes
6975
data["y"] = 3; // excused because of calculated property access
7076
```
7177

72-
This rule has a shorthand integer option for the `"min"` object property.
73-
74-
Examples of **incorrect** code for this rule with a minimum of 4:
75-
76-
```js
77-
/*eslint id-length: ["error", { "min": 4 }]*/
78-
/*eslint-env es6*/
79-
80-
var val = 5;
81-
obj.e = document.body;
82-
function foo (e) { };
83-
try {
84-
dangerousStuff();
85-
} catch (e) {
86-
// ignore as many do
87-
}
88-
var myObj = { a: 1 };
89-
(val) => { val * val };
90-
class x { }
91-
class Foo { x() {} }
92-
function foo(...x) { }
93-
var { x } = {};
94-
var { prop: x} = {};
95-
({ prop: obj.x } = {});
96-
```
97-
98-
Examples of **correct** code for this rule with a minimum of 4:
99-
100-
```js
101-
/*eslint id-length: ["error", { "min": 4 }]*/
102-
/*eslint-env es6*/
103-
104-
var value = 5;
105-
function func() { return 42; }
106-
obj.element = document.body;
107-
var foobar = function (event) { /* do stuff */ };
108-
try {
109-
dangerousStuff();
110-
} catch (error) {
111-
// ignore as many do
112-
}
113-
var myObj = { apple: 1 };
114-
(value) => { value * value };
115-
function foobar(value = 0) { }
116-
class MyClass { }
117-
class Foobar { method() {} }
118-
function foobar(...args) { }
119-
var { prop } = {};
120-
var { a: longName } = {};
121-
var { prop: [x] } = {};
122-
({ prop: obj.name } = {});
123-
var data = { "x": 1 }; // excused because of quotes
124-
data["y"] = 3; // excused because of calculated property access
125-
```
126-
12778
This rule has an object option:
12879

12980
* `"min"` (default: 2) enforces a minimum identifier length
@@ -155,6 +106,8 @@ class Foo { x() {} }
155106
function foo(...x) { }
156107
var { x } = {};
157108
var { prop: a} = {};
109+
var [x] = arr;
110+
var { prop: [x]} = {};
158111
({ prop: obj.x } = {});
159112
```
160113

@@ -180,8 +133,9 @@ class MyClass { }
180133
class Foobar { method() {} }
181134
function foobar(...args) { }
182135
var { prop } = {};
136+
var [longName] = foo;
137+
var { a: [prop] } = {};
183138
var { a: longName } = {};
184-
var { prop: [x] } = {};
185139
({ prop: obj.name } = {});
186140
var data = { "x": 1 }; // excused because of quotes
187141
data["y"] = 3; // excused because of calculated property access
@@ -205,6 +159,7 @@ try {
205159
// ignore as many do
206160
}
207161
(reallyLongArgName) => { return !reallyLongArgName; };
162+
var [reallyLongFirstElementName] = arr;
208163
```
209164

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

228184
### properties
@@ -256,6 +212,7 @@ try {
256212
// ignore as many do
257213
}
258214
(x) => { return x * x; };
215+
var [x] = arr;
259216
const { x } = foo;
260217
const { a: x } = foo;
261218
```

‎lib/rules/id-length.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ module.exports = {
100100
ClassDeclaration: true,
101101
FunctionDeclaration: true,
102102
MethodDefinition: true,
103-
CatchClause: true
103+
CatchClause: true,
104+
ArrayPattern: true
104105
};
105106

106107
return {

‎tests/lib/rules/id-length.js

+19
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ ruleTester.run("id-length", rule, {
7272
{ code: "var obj = {}; obj.aaaaa = 1;", options: [{ max: 4, properties: "never" }] },
7373
{ code: "({ a: obj.x.y.z } = {});", options: [{ max: 4, properties: "never" }], parserOptions: { ecmaVersion: 6 } },
7474
{ code: "({ prop: obj.xxxxx } = {});", options: [{ max: 4, properties: "never" }], parserOptions: { ecmaVersion: 6 } },
75+
{ code: "var arr = [i,j,f,b]", parserOptions: { ecmaVersion: 6 } },
76+
{ code: "function foo([arr]) {}", parserOptions: { ecmaVersion: 6 } },
7577
{ code: "var {x} = foo;", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } },
7678
{ code: "var {x, y: {z}} = foo;", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } },
7779
{ code: "let foo = { [a]: 1 };", options: [{ properties: "always" }], parserOptions: { ecmaVersion: 6 } },
@@ -88,6 +90,9 @@ ruleTester.run("id-length", rule, {
8890
{ code: "var handler = function (e) {};", errors: [tooShortError] },
8991
{ code: "for (var i=0; i < 10; i++) { console.log(i); }", errors: [tooShortError] },
9092
{ code: "var j=0; while (j > -10) { console.log(--j); }", errors: [tooShortError] },
93+
{ code: "var [i] = arr;", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError] },
94+
{ code: "var [,i,a] = arr;", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError, tooShortError] },
95+
{ code: "function foo([a]) {}", parserOptions: { ecmaVersion: 6 }, errors: [tooShortError] },
9196
{
9297
code: "var _$xt_$ = Foo(42)",
9398
options: [{ min: 2, max: 4 }],
@@ -252,6 +257,20 @@ ruleTester.run("id-length", rule, {
252257
tooShortError
253258
]
254259
},
260+
{
261+
code: "var { prop: [x] } = {};",
262+
parserOptions: { ecmaVersion: 6 },
263+
errors: [
264+
tooShortError
265+
]
266+
},
267+
{
268+
code: "var { prop: [[x]] } = {};",
269+
parserOptions: { ecmaVersion: 6 },
270+
errors: [
271+
tooShortError
272+
]
273+
},
255274
{
256275
code: "var { prop: longName } = {};",
257276
options: [{ min: 3, max: 5 }],

0 commit comments

Comments
 (0)
Please sign in to comment.