Skip to content
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

merged 2 commits into from Sep 26, 2020


Copy link

@mdjermanovic mdjermanovic commented Sep 20, 2020

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

  • ESLint Version: v7.9.0
  • Node Version: v12.18.4
  • npm Version: v6.14.6

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, etc.) are you using?


Please show your full configuration:

module.exports = {
  parserOptions: {
    ecmaVersion: 2015

What did you do? Please include the actual source code causing the issue.

Online Demo

/* eslint prefer-destructuring: ["error",
  { "object": true },
  { "enforceForRenamedProperties": true }
] */

var a = b[a];

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:

/* eslint prefer-destructuring: ["error",
  { "object": true },
  { "enforceForRenamedProperties": true }
] */

var {a} = b;

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 was var a = b.a;.

Is there anything you'd like reviewers to focus on?

@mdjermanovic mdjermanovic added bug rule accepted autofix labels Sep 20, 2020
Copy link

@nzakas nzakas left a 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.

Copy link
Member Author

@mdjermanovic mdjermanovic commented Sep 24, 2020

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 =; // auto-fixable

var foo =; // not auto-fixable

var foo = object[bar]; // not auto-fixable

var foo = object[foo]; // was mistakenly auto-fixable

Demo link

@mdjermanovic mdjermanovic requested a review from nzakas Sep 24, 2020
nzakas approved these changes Sep 25, 2020
Copy link

@nzakas nzakas left a comment

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 =` 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 =`) are not automatically fixed.
Copy link

@btmills btmills Sep 26, 2020

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.

@btmills btmills merged commit cb44e93 into master Sep 26, 2020
12 checks passed
@btmills btmills deleted the preferdestructuring-computedautofix branch Sep 26, 2020
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Mar 26, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age label Mar 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
accepted archived due to age autofix bug rule
None yet

Successfully merging this pull request may close these issues.

None yet

3 participants