diff --git a/docs/rules/id-length.md b/docs/rules/id-length.md index 081784bbebc..f9f6de10080 100644 --- a/docs/rules/id-length.md +++ b/docs/rules/id-length.md @@ -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} = {}; @@ -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 @@ -155,6 +106,8 @@ class Foo { x() {} } function foo(...x) { } var { x } = {}; var { prop: a} = {}; +var [x] = arr; +var { prop: [x]} = {}; ({ prop: obj.x } = {}); ``` @@ -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 @@ -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: @@ -223,6 +178,7 @@ try { // ignore as many do } (arg) => { return !arg; }; +var [first] = arr; ``` ### properties @@ -256,6 +212,7 @@ try { // ignore as many do } (x) => { return x * x; }; +var [x] = arr; const { x } = foo; const { a: x } = foo; ``` diff --git a/lib/rules/id-length.js b/lib/rules/id-length.js index 5509a486aff..a68873ac062 100644 --- a/lib/rules/id-length.js +++ b/lib/rules/id-length.js @@ -100,7 +100,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 69ea8670abb..a63ae1be99c 100644 --- a/tests/lib/rules/id-length.js +++ b/tests/lib/rules/id-length.js @@ -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 } }, @@ -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 }], @@ -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 }],