Skip to content

[prefer-nullish-coalescing] eslint --fix changes behavior when Logical OR's left side is optional and can be falsy #1294

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
berlysia opened this issue Dec 2, 2019 · 1 comment
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look

Comments

@berlysia
Copy link

berlysia commented Dec 2, 2019

Repro

{
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
      "project": "./tsconfig.json"
    },
    "plugins": [
      "@typescript-eslint"
    ],
    "rules": {
      "@typescript-eslint/prefer-nullish-coalescing": "error"
    }
  }
import assert from 'assert';

function f(arg: null | boolean): null | boolean {
  return arg;
}
function g(arg: boolean): boolean {
  return arg;
}

function h(a: null | boolean, b: boolean) {
  return f(a) || g(b);
}

assert.equal(h(true, true), true);
assert.equal(h(true, false), true);
assert.equal(h(false, true), true); // broken after fix
assert.equal(h(false, false), false); // not broken, but returns `f`'s result(bad sample...)
assert.equal(h(null, true), true);
assert.equal(h(null, false), false);

Expected Result
no report. Fallback to g() when f() is falsy.

Actual Result
@typescript-eslint/prefer-nullish-coalescing reports the error, and also the fixed output is broken.

Sample repo to reproduce the behavior: https://github.com/berlysia/typescript-eslint-nullish-coalescing-false-positive

Additional Info

This issue applies not only to boolean but also to number and string.

Versions

package version
@typescript-eslint/eslint-plugin 2.9.0
@typescript-eslint/parser 2.9.0
TypeScript 3.7.2
ESLint 6.7.2
node 12.4.0
npm 6.9.0
@berlysia berlysia added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Dec 2, 2019
@berlysia berlysia changed the title [prefer-nullish-coalescing] false positive when Logical OR's left side is nullish [prefer-nullish-coalescing] false positive when Logical OR's left side is optional Dec 2, 2019
@berlysia berlysia changed the title [prefer-nullish-coalescing] false positive when Logical OR's left side is optional [prefer-nullish-coalescing] false positive when Logical OR's left side is optional and can be falsy Dec 2, 2019
@berlysia berlysia changed the title [prefer-nullish-coalescing] false positive when Logical OR's left side is optional and can be falsy [prefer-nullish-coalescing] eslint --fix changes behavior when Logical OR's left side is optional and can be falsy Dec 2, 2019
@berlysia
Copy link
Author

berlysia commented Dec 2, 2019

Close this issue. #1272 is solving this.

@berlysia berlysia closed this as completed Dec 2, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look
Projects
None yet
Development

No branches or pull requests

1 participant