Fix: prefer-destructuring invalid autofix with computed property access#13704
Merged
Conversation
nzakas
requested changes
Sep 24, 2020
nzakas
left a comment
Member
There was a problem hiding this comment.
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.
Member
Author
|
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 |
btmills
approved these changes
Sep 26, 2020
| - 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. |
Member
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-destructuringrule 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?