Closed
Description
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 |