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
Fix: prefer-destructuring invalid autofix with computed property access #13704
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add something about this to the docs too? It seems like this new behavior could be misinterpreted as a bug without an explanation somewhere.
Added a paragraph explaining what code is fixable by this rule. This PR doesn't change behavior much, the rule already fixes only simple cases: /* eslint prefer-destructuring: ["error",
{ "object": true },
{ "enforceForRenamedProperties": true }
] */
var foo = object.foo; // auto-fixable
var foo = object.bar; // not auto-fixable
var foo = object[bar]; // not auto-fixable
var foo = object[foo]; // was mistakenly auto-fixable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works. Thanks.
@@ -21,6 +21,8 @@ The rule has a second object with a single key, `enforceForRenamedProperties`, w | |||
- Accessing an object property whose key is an integer will fall under the category `array` destructuring. | |||
- Accessing an array element through a computed index will fall under the category `object` destructuring. | |||
|
|||
The `--fix` option on the command line fixes only problems reported in variable declarations, and among them only those that fall under the category `object` destructuring. Furthermore, the name of the declared variable has to be the same as the name used for non-computed member access in the initializer. For example, `var foo = object.foo` can be automatically fixed by this rule. Problems that involve computed member access (e.g., `var foo = object[foo]`) or renamed properties (e.g., `var foo = object.bar`) are not automatically fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this also covers the var foo = object['foo'];
case, which we don't fix either and for which we already have a test asserting the same.
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[X] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
Tell us about your environment
What parser (default,
@babel/eslint-parser
,@typescript-eslint/parser
, etc.) are you using?default
Please show your full configuration:
Configuration
What did you do? Please include the actual source code causing the issue.
Online Demo
What did you expect to happen?
1 error, but not with an invalid autofix.
What actually happened? Please include the actual, raw output from ESLint.
1 error and autofix to:
This changes the semantics. A correct autofix would be
var { [a]: a } = b;
What changes did you make? (Give an overview)
Fixed the
prefer-destructuring
rule to not auto-fix in this case, since the rule generally doesn't auto-fix renamed properties. This was just mistakenly treated as if it wasvar a = b.a;
.Is there anything you'd like reviewers to focus on?