Skip to content

Commit 8a91cbd

Browse files
authored
feat(eslint-plugin): [prefer-nullish-coalescing] logic and test for strict null checks (typescript-eslint#6174)
* chore(website): [prefer-nullish-coalescing] explicit notice for strictNullChecks * Added null check to prefer-nullish-coalescing along with a test.
1 parent 2d0a883 commit 8a91cbd

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
22
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils';
3+
import * as tsutils from 'tsutils';
34
import * as ts from 'typescript';
45

56
import * as util from '../util';
@@ -9,13 +10,15 @@ export type Options = [
910
ignoreConditionalTests?: boolean;
1011
ignoreTernaryTests?: boolean;
1112
ignoreMixedLogicalExpressions?: boolean;
13+
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean;
1214
},
1315
];
1416

1517
export type MessageIds =
1618
| 'preferNullishOverOr'
1719
| 'preferNullishOverTernary'
18-
| 'suggestNullish';
20+
| 'suggestNullish'
21+
| 'noStrictNullCheck';
1922

2023
export default util.createRule<Options, MessageIds>({
2124
name: 'prefer-nullish-coalescing',
@@ -34,6 +37,8 @@ export default util.createRule<Options, MessageIds>({
3437
preferNullishOverTernary:
3538
'Prefer using nullish coalescing operator (`??`) instead of a ternary expression, as it is simpler to read.',
3639
suggestNullish: 'Fix to nullish coalescing operator (`??`).',
40+
noStrictNullCheck:
41+
'This rule requires the `strictNullChecks` compiler option to be turned on to function correctly.',
3742
},
3843
schema: [
3944
{
@@ -48,6 +53,9 @@ export default util.createRule<Options, MessageIds>({
4853
ignoreMixedLogicalExpressions: {
4954
type: 'boolean',
5055
},
56+
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: {
57+
type: 'boolean',
58+
},
5159
},
5260
additionalProperties: false,
5361
},
@@ -58,6 +66,7 @@ export default util.createRule<Options, MessageIds>({
5866
ignoreConditionalTests: true,
5967
ignoreTernaryTests: true,
6068
ignoreMixedLogicalExpressions: true,
69+
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false,
6170
},
6271
],
6372
create(
@@ -67,12 +76,31 @@ export default util.createRule<Options, MessageIds>({
6776
ignoreConditionalTests,
6877
ignoreTernaryTests,
6978
ignoreMixedLogicalExpressions,
79+
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing,
7080
},
7181
],
7282
) {
7383
const parserServices = util.getParserServices(context);
84+
const compilerOptions = parserServices.program.getCompilerOptions();
7485
const sourceCode = context.getSourceCode();
7586
const checker = parserServices.program.getTypeChecker();
87+
const isStrictNullChecks = tsutils.isStrictCompilerOptionEnabled(
88+
compilerOptions,
89+
'strictNullChecks',
90+
);
91+
92+
if (
93+
!isStrictNullChecks &&
94+
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing !== true
95+
) {
96+
context.report({
97+
loc: {
98+
start: { line: 0, column: 0 },
99+
end: { line: 0, column: 0 },
100+
},
101+
messageId: 'noStrictNullCheck',
102+
});
103+
}
76104

77105
return {
78106
ConditionalExpression(node: TSESTree.ConditionalExpression): void {

packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { TSESLint } from '@typescript-eslint/utils';
2+
import * as path from 'path';
23

34
import type {
45
MessageIds,
@@ -385,6 +386,25 @@ x ?? y;
385386
],
386387
})),
387388

389+
// noStrictNullCheck
390+
{
391+
code: `
392+
declare const x: string[] | null;
393+
if (x) {
394+
}
395+
`,
396+
errors: [
397+
{
398+
messageId: 'noStrictNullCheck',
399+
line: 0,
400+
column: 1,
401+
},
402+
],
403+
parserOptions: {
404+
tsconfigRootDir: path.join(rootPath, 'unstrict'),
405+
},
406+
},
407+
388408
// ignoreConditionalTests
389409
...nullishTypeInvalidTest((nullish, type) => ({
390410
code: `

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy