-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix FP S6606 (
prefer-nullish-coalescing
): When the first argument i…
…s boolean | undefined Also when first argument is `string | undefined` and `number | undefined`
- Loading branch information
1 parent
a8d3908
commit a0017dc
Showing
6 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// intentionally left empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"strictNullChecks": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
export * from './rule'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
import { tsEslintRules } from '../typescript-eslint'; | ||
import { type Rule } from 'eslint'; | ||
import { | ||
getTypeFromTreeNode, | ||
interceptReport, | ||
isNullOrUndefinedType, | ||
isObjectType, | ||
isPrimitiveType, | ||
} from '../helpers'; | ||
import { LogicalExpression } from 'estree'; | ||
|
||
const preferNullishCoalescingRule = tsEslintRules['prefer-nullish-coalescing']; | ||
|
||
export const rule = interceptReport(preferNullishCoalescingRule, (context, reportDescriptor) => { | ||
const { node: token, messageId } = reportDescriptor as Rule.ReportDescriptor & { | ||
node: Rule.Node; | ||
messageId: string; | ||
}; | ||
|
||
if (messageId === 'preferNullishOverOr') { | ||
const services = context.sourceCode.parserServices; | ||
const node = context.sourceCode.getNodeByRangeIndex(token.range![0]) as LogicalExpression; | ||
const leftOperand = node.left; | ||
const leftOperandType = getTypeFromTreeNode(leftOperand, services); | ||
|
||
if ( | ||
leftOperandType.isUnion() && | ||
leftOperandType.types.some(isNullOrUndefinedType) && | ||
(leftOperandType.types.some(isPrimitiveType) || leftOperandType.types.some(isObjectType)) | ||
) { | ||
return; | ||
} | ||
} | ||
|
||
context.report(reportDescriptor); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
import { rule } from './rule'; | ||
import { RuleTester } from 'eslint'; | ||
import path from 'path'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
project: `./tsconfig.json`, | ||
tsconfigRootDir: path.join(__dirname, 'fixtures'), | ||
}, | ||
}); | ||
|
||
ruleTester.run('S6606', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
function foo(value: string) { | ||
return value || 'default'; | ||
} | ||
`, | ||
filename: path.join(__dirname, 'fixtures/index.ts'), | ||
}, | ||
{ | ||
code: ` | ||
function foo(value: string | number) { | ||
return value || 'default'; | ||
} | ||
`, | ||
filename: path.join(__dirname, 'fixtures/index.ts'), | ||
}, | ||
{ | ||
code: ` | ||
function foo(value: string | null) { | ||
return value || 'default'; | ||
} | ||
`, | ||
filename: path.join(__dirname, 'fixtures/index.ts'), | ||
}, | ||
{ | ||
code: ` | ||
function foo(value: number | null) { | ||
return value || 'default'; | ||
} | ||
`, | ||
filename: path.join(__dirname, 'fixtures/index.ts'), | ||
}, | ||
{ | ||
code: ` | ||
function foo(value: bigint | null) { | ||
return value || 'default'; | ||
} | ||
`, | ||
filename: path.join(__dirname, 'fixtures/index.ts'), | ||
}, | ||
{ | ||
code: ` | ||
function foo(value: boolean | null) { | ||
return value || 'default'; | ||
} | ||
`, | ||
filename: path.join(__dirname, 'fixtures/index.ts'), | ||
}, | ||
{ | ||
code: ` | ||
function foo(value: { baz: number } | null) { | ||
return value || 'default'; | ||
} | ||
`, | ||
filename: path.join(__dirname, 'fixtures/index.ts'), | ||
}, | ||
{ | ||
code: ` | ||
function foo(value: Date | null) { | ||
return value || 'default'; | ||
} | ||
`, | ||
filename: path.join(__dirname, 'fixtures/index.ts'), | ||
}, | ||
], | ||
invalid: [], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters