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
merged 2 commits into from Sep 26, 2020

Conversation

mdjermanovic
Copy link
Member

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?

default

Please show your full configuration:

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 ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion autofix This change is related to ESLint's autofixing capabilities labels Sep 20, 2020
Copy link
Member

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

@mdjermanovic
Copy link
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

Demo link

Copy link
Member

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

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
@btmills btmills deleted the preferdestructuring-computedautofix branch September 26, 2020 20:38
@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 This issue has been archived; please open a new issue for any further discussion label Mar 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion autofix This change is related to ESLint's autofixing capabilities bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants