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

Bug: --fix ignores defined order and creates a JS error #13899

Closed
darlandemarco opened this issue Dec 1, 2020 · 4 comments
Closed

Bug: --fix ignores defined order and creates a JS error #13899

darlandemarco opened this issue Dec 1, 2020 · 4 comments

Comments

@darlandemarco
Copy link

@darlandemarco darlandemarco commented Dec 1, 2020

Tell us about your environment

  • ESLint Version: 6.8.0
  • Node Version: 8.15
  • npm Version: 6.4.1

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

Please show your full configuration:

Configuration
module.exports = {
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "rules": {
        "prefer-const": "error",
        "no-undef-init": "warn"
    }
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

let foo= undefined; //foo is not reasigned so it will try to transform to const and keep initialization as undefined;

Eslint command:

node node_modules/eslint/bin/eslint.js --fix testfile.js

What did you expect to happen?
transform variable to constant but not remove the undefined init as it will become a constant.

What actually happened? Please include the actual, raw output from ESLint.
I have runned an autofix in a legacy code with more than 10k files. When it finds a variable that should be a constant (because it is not reasigned) and is initialized as undefined it fix the code transforming it to const foo; causing errors over the code. I tried to change the rules order to first turns it to a constant and them check the undefined init so it should be fixed (as per: https://eslint.org/docs/rules/no-undef-init). But is still happening.

Are you willing to submit a pull request to fix this bug?
Yes, need some guidance but I can do this :)

eslint

@mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Dec 2, 2020

Hi @darlandemarco, thanks for the issue!

I can confirm that let foo = undefined; gets auto-fixed to const foo;

You're using an old ESLint version, but this happens in the actual version as well: Online Demo

@mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Dec 2, 2020

Are you willing to submit a pull request to fix this bug?
Yes, need some guidance but I can do this :)

That would be great, thanks!

I think the "fix range" of prefer-const should be extended to the whole declaration, in order to prevent other fixes in the same pass. Then, in the second pass, no-undef-init will see const and won't report an error.

There's fix-tracker helper for this purpose. We should call retainRange and then replaceTextRange.

You can find some examples of use in rules such as semi, no-extra-semi, no-else-return, and no-useless-return.

A test case for prefer-const that should pass after the change:

// https://github.com/eslint/eslint/issues/13899
{
    code: "/*eslint no-undef-init:error*/ let foo = undefined;",
    output: "/*eslint no-undef-init:error*/ const foo = undefined;",
    errors: 2
}

@snitin315
Copy link
Contributor

@snitin315 snitin315 commented Jan 20, 2021

Can I work on this issue?

@mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Jan 22, 2021

@snitin315 sure, thanks! It doesn't seem anyone is working on this at the moment.

mdjermanovic pushed a commit that referenced this issue Jan 30, 2021
…) (#14033)

* Fix: extend fixer range of prefer const to whole declaration

* Chore: update prefer-const test case
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jul 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.