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

[prefer-nullish-coalescing] eslint --fix outputs broken code #1290

Closed
susisu opened this issue Dec 1, 2019 · 0 comments · Fixed by #1326
Closed

[prefer-nullish-coalescing] eslint --fix outputs broken code #1290

susisu opened this issue Dec 1, 2019 · 0 comments · Fixed by #1326
Labels
bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@susisu
Copy link
Contributor

susisu commented Dec 1, 2019

Repro

.eslintrc.json

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "project": "./tsconfig.json"
  },
  "rules": {
    "@typescript-eslint/prefer-nullish-coalescing": "error"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "lib",
    "strict": true
  },
  "include": [
    "src/**/*"
  ]
}

src/index.ts

declare const a: string | undefined;
declare const b: string;
declare const c: string;

const x = a || b || c;

Expected Result
The last line should be fixed like this:

const x = (a ?? b) || c;

Here parentheses are required because nullish coalescing operators and logical operators cannot be mixed.
Since logical operator || is left-associative, it is natural to add parentheses in left-to-right manner.

Actual Result
eslint --fix outputs this:

const x = a ?? b || c;

This is not a valid syntax and we get the following error.

$ /path/to/project/node_modules/.bin/tsc --build .
src/index.ts:5:16 - error TS5076: '||' and '??' operations cannot be mixed without parentheses.

5 const x = a ?? b || c;
                 ~~~~~~


Found 1 error.

Additional Info

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.13.0
npm 6.13.1
@susisu susisu added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Dec 1, 2019
@bradzacher bradzacher added bug Something isn't working good first issue Good for newcomers and removed triage Waiting for maintainers to take a look labels Dec 1, 2019
dimabory added a commit to dimabory/typescript-eslint that referenced this issue Dec 11, 2019
dimabory added a commit to dimabory/typescript-eslint that referenced this issue Dec 11, 2019
dimabory added a commit to dimabory/typescript-eslint that referenced this issue Dec 11, 2019
dimabory added a commit to dimabory/typescript-eslint that referenced this issue Dec 11, 2019
dimabory added a commit to dimabory/typescript-eslint that referenced this issue Dec 11, 2019
dimabory added a commit to dimabory/typescript-eslint that referenced this issue Dec 12, 2019
dimabory added a commit to dimabory/typescript-eslint that referenced this issue Dec 12, 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
bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants