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: no-useless-rename invalid autofix with parenthesized identifiers #14032

Merged
merged 1 commit into from Jan 30, 2021

Conversation

mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Jan 24, 2021

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.18.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:

Configuration
module.exports = {
  parserOptions: {
    ecmaVersion: 2015
  }
};

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

Online Demo

/* eslint no-useless-rename: error */

({ a: (a) } = foo );

({ a: (a) = 1 } = foo );

What did you expect to happen?

2 errors, but not an invalid autofix.

What actually happened? Please include the actual, raw output from ESLint.

2 errors and autofix to:

/* eslint no-useless-rename: error */

({ a) } = foo );

({ (a) = 1 } = foo );
  3:5  error  Parsing error: Unexpected token )

{ a) } is obviously a syntax error.

{ (a) = 1 } is also a syntax error, those parentheses are not allowed in shorthand properties.

What changes did you make? (Give an overview)

Changed the no-useless-rename rule to correctly fix code such as { a: (a) } and avoid autofixing code such as { a: (a) = 1 }.

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

This change should be also helpful for future syntax tc39/ecma262#2154, like export { "a b" as "a b" } from "foo";

@mdjermanovic mdjermanovic added bug rule accepted autofix labels Jan 24, 2021
nzakas
nzakas approved these changes Jan 28, 2021
Copy link
Member

@btmills btmills left a comment

LGTM

@btmills btmills merged commit d76e8f6 into master Jan 30, 2021
13 checks passed
@btmills btmills deleted the nouselessrename-parens branch Jan 30, 2021
/**
* Properties using shorthand syntax and rest elements can not be renamed.
* If the property is computed, we have no idea if a rename is useless or not.
*/
if (property.shorthand || property.type === "RestElement" || property.computed) {
if (property.type !== "Property" || property.shorthand || property.computed) {
Copy link
Member Author

@mdjermanovic mdjermanovic Jan 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change to be consistent with the condition in fix() (where it expects only Property or module specifiers), so checking for ExperimentalRestProperty above became unnecessary.

This was referenced Mar 17, 2021
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jul 30, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age label Jul 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted archived due to age autofix bug rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants