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 removes comments (refs #13678) #13682

Merged
merged 1 commit into from Sep 12, 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:

refs #13678, fixes prefer-destructuring to not remove comments. It doesn't change the logic of this rule.

Tell us about your environment

  • ESLint Version: v7.8.1
  • Node Version: v12.14.0
  • npm Version: v6.13.4

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" */

var foo = /* comment */ obj.foo;

What did you expect to happen?

Autofix should not remove comments.

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

ESLint auto-fixed the code to:

/* eslint prefer-destructuring: "error" */

var {foo} = obj;

What changes did you make? (Give an overview)

Changed prefer-destructuring to not auto-fix if that would remove comments. Any comment inside the VariableDeclarator will prevent fixing, except for those inside the right side's object, which will be preserved.

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

  • It would be possible to auto-fix and preserve other comments, but their surrounding context will change so maybe the comments should be changed as well. It seemed best to just not auto-fix and let the user fix everything if there are comments in the range.
  • The added conditional could have been placed in shouldFix, but fixIntoObjectDestructuring seemed like a more appropriate place, as this check is closely related to the fixing algorithm.

@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 10, 2020
@anikethsaha
Copy link
Member

  • It would be possible to auto-fix and preserve other comments, but their surrounding context will change so maybe the comments should be changed as well. It seemed best to just not auto-fix and let the user fix everything if there are comments in the range

Having comments inside I think is very rare in js (not in jsdoc or in other type - comments tools/env), so it would make more sense to just not autofix

  • The added conditional could have been placed in shouldFix, but fixIntoObjectDestructuring seemed like a more appropriate place, as this check is closely related to the fixing algorithm.

IMO, shouldFix looks good as it is the deciding function to whether do the fixing or not.

though not a strong opinion on either

Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

LGTM!

@kaicataldo
Copy link
Member

  • The added conditional could have been placed in shouldFix, but fixIntoObjectDestructuring seemed like a more appropriate place, as this check is closely related to the fixing algorithm.

I don't have a strong preference either way, but I believe there's precedent for this check being done in the fixer callback itself so I think this is fine.

@kaicataldo kaicataldo merged commit 29d1cdc into master Sep 12, 2020
@kaicataldo kaicataldo deleted the preferdestructuring-comments branch September 12, 2020 18:02
@kaicataldo
Copy link
Member

Thanks for contributing!

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Mar 18, 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 18, 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