Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[no-unnecessary-boolean-literal-compare] changes code behavior on autofix if strictNullCheck is disabled. #2830

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

Closed
3 tasks done
sbaechler opened this issue Nov 30, 2020 · 5 comments · Fixed by #2832
Closed
3 tasks done
Labels
documentation Documentation ("docs") that needs adding/updating package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@sbaechler
Copy link
Contributor

sbaechler commented Nov 30, 2020

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

This issue is related to #1632.

When strictNullChecks is disabled, autofix for this rule changes the actual behaviour of the code, which can cause new bugs.

In that case, autofix should be disabled for this rule.

Repro

{
  "rules": {
    "@typescript-eslint/no-unnecessary-boolean-literal-compare": ["error"]
  }
}
let a: boolean | undefined
if (a === false) { 
  // a is undefined and this is not executed,
  launchMissile()
}

Expected Result

if (a === false) { 
  launchMissile()
}

or

if (!(a ?? true)) {
  launchMissile() 
}

Actual Result

if (!a) {
  // a is still undefined, but now this is executed. 
  launchMissile() 
}

Additional Info

tsconfig:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "lib": ["dom", "dom.iterable", "esnext", "scripthost"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
     "strict": false,
    "strictBindCallApply": true,
    "noUnusedLocals": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "noEmit": true,
    "jsx": "preserve",
    "newLine": "lf",
    "locale": "en"
  }
}

Versions

package version
@typescript-eslint/eslint-plugin 4.8.2
@typescript-eslint/parser 4.8.2
TypeScript 4.0.5
ESLint 7.9.0
node 12.19.0
@sbaechler sbaechler added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Nov 30, 2020
@bradzacher
Copy link
Member

Why do you have strictNullChecks turned off?

This rule (like all rules in the same vein) will not work correctly and will cause issues without it.

You should not be using this rule without strictNullChecks.

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for team members to take a look labels Nov 30, 2020
@sbaechler
Copy link
Contributor Author

I would rather have it on, but our Typescript codebase is 250k lines and it cannot 'just' be activated. That would be a two weeks task.

However, this does not matter. Calling eslint --fix on a codebase must never change the behaviour of the program no matter what the typescript settings or eslint settings are.

In our case the eslint configuration is shared throughout the company for many projects. Our project is a banking application and I just found out about the change because the tests failed after running eslint with an updated configuration.

There should at least be a warning or an error message if this rule is active and strictNullChecks is not.

@bradzacher
Copy link
Member

TypeScript just says to the rule that "variable a is of type boolean".
It doesn't say "variable a is of type boolean, but strictNullChecks is off, so it might be null | undefined ".

This is why the rule autofixes - the rule is acting based on the information that it is given.

I probably should have included this rule when I did #2345, but I didn't think about it at the time.

To reiterate:
you should not be using this rule without strictNullChecks, as it won't work.

@sbaechler
Copy link
Contributor Author

Would you mind adding a note to the rule documentation? https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unnecessary-boolean-literal-compare.md

Other people might run into the same issue.

It would be good if TypeScript said to the rule that "variable a was of type boolean or undefined.

@bradzacher
Copy link
Member

It would be good if TypeScript said to the rule that "variable a was of type boolean or undefined.

It will - if and only if strictNullChecks is turned on.
Otherwise TS acts like nothing can be null | undefined by removing the types from any and all type unions.

Which is why it's so very, very dangerous to ever code with strictNullChecks turned off.


Would you mind adding a note to the rule documentation?

Happy to accept PR. Everything here is open source and community maintained.

sbaechler added a commit to sbaechler/typescript-eslint that referenced this issue Dec 1, 2020
…ning message

Added a warning message to the rule readme since this rule can change
the behaviour of a program if `strictNullChecks` is disabled.

Fixes typescript-eslint#2830
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 1, 2021
@bradzacher bradzacher added documentation Documentation ("docs") that needs adding/updating and removed awaiting response Issues waiting for a reply from the OP or another party labels Jan 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Documentation ("docs") that needs adding/updating package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants