Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: check renaming identifier in object destructuring (fixes 1282…
…7) (#12881)

* Update: check renaming identifier in object destructuring (fixes 12827)

* Fix wrong error type

* ignore properties options on object pattern, ignore computed prop

* modify docs, add test cases

* add test cases

* remove wrong test cases

* change to ignore destructuring on properties never

* update documentation

* fix typo

* fix documentation

* include error loc, data info
  • Loading branch information
yeonjuan committed Feb 17, 2020
1 parent 41de9df commit 77df505
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 24 deletions.
24 changes: 13 additions & 11 deletions docs/rules/id-length.md
Expand Up @@ -31,9 +31,9 @@ var myObj = { a: 1 };
class x { }
class Foo { x() {} }
function foo(...x) { }
function foo({x}) { }
var { x } = {};
var { x: a} = {};
var { a: [x]} = {};
var { prop: a} = {};
({ prop: obj.x } = {});
```

Expand All @@ -59,8 +59,10 @@ function foo(num = 0) { }
class MyClass { }
class Foo { method() {} }
function foo(...args) { }
function foo({ prop }) { }
function foo({ a: prop }) { }
var { prop } = {};
var { prop: a } = {};
var { a: prop } = {};
var { prop: [x] } = {};
({ prop: obj.longName } = {});
var data = { "x": 1 }; // excused because of quotes
Expand Down Expand Up @@ -89,8 +91,7 @@ class x { }
class Foo { x() {} }
function foo(...x) { }
var { x } = {};
var { x: a} = {};
var { a: [x]} = {};
var { prop: x} = {};
({ prop: obj.x } = {});
```

Expand All @@ -103,7 +104,7 @@ Examples of **correct** code for this rule with a minimum of 4:
var value = 5;
function func() { return 42; }
obj.element = document.body;
var foo = function (event) { /* do stuff */ };
var foobar = function (event) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
Expand All @@ -116,7 +117,7 @@ class MyClass { }
class Foobar { method() {} }
function foobar(...args) { }
var { prop } = {};
var { prop: a } = {};
var { a: longName } = {};
var { prop: [x] } = {};
({ prop: obj.name } = {});
var data = { "x": 1 }; // excused because of quotes
Expand Down Expand Up @@ -153,8 +154,7 @@ class x { }
class Foo { x() {} }
function foo(...x) { }
var { x } = {};
var { x: a} = {};
var { a: [x]} = {};
var { prop: a} = {};
({ prop: obj.x } = {});
```

Expand All @@ -167,7 +167,7 @@ Examples of **correct** code for this rule with the `{ "min": 4 }` option:
var value = 5;
function func() { return 42; }
obj.element = document.body;
var foo = function (event) { /* do stuff */ };
var foobar = function (event) { /* do stuff */ };
try {
dangerousStuff();
} catch (error) {
Expand All @@ -180,7 +180,7 @@ class MyClass { }
class Foobar { method() {} }
function foobar(...args) { }
var { prop } = {};
var { prop: a } = {};
var { a: longName } = {};
var { prop: [x] } = {};
({ prop: obj.name } = {});
var data = { "x": 1 }; // excused because of quotes
Expand Down Expand Up @@ -256,6 +256,8 @@ try {
// ignore as many do
}
(x) => { return x * x; };
const { x } = foo;
const { a: x } = foo;
```

## Related Rules
Expand Down
15 changes: 12 additions & 3 deletions lib/rules/id-length.js
Expand Up @@ -63,6 +63,7 @@ module.exports = {

return obj;
}, {});
const reportedNode = new Set();

const SUPPORTED_EXPRESSIONS = {
MemberExpression: properties && function(parent) {
Expand All @@ -82,8 +83,15 @@ module.exports = {
VariableDeclarator(parent, node) {
return parent.id === node;
},
Property: properties && function(parent, node) {
return parent.key === node;
Property(parent, node) {

if (parent.parent.type === "ObjectPattern") {
return (
parent.value !== parent.key && parent.value === node ||
parent.value === parent.key && parent.key === node && properties
);
}
return properties && !parent.computed && parent.key === node;
},
ImportDefaultSpecifier: true,
RestElement: true,
Expand All @@ -109,7 +117,8 @@ module.exports = {

const isValidExpression = SUPPORTED_EXPRESSIONS[parent.type];

if (isValidExpression && (isValidExpression === true || isValidExpression(parent, node))) {
if (isValidExpression && !reportedNode.has(node) && (isValidExpression === true || isValidExpression(parent, node))) {
reportedNode.add(node);
context.report({
node,
messageId: isShort ? "tooShort" : "tooLong",
Expand Down

0 comments on commit 77df505

Please sign in to comment.