Skip to content

Commit

Permalink
feat(eslint-plugin): [prefer-nullish-coalescing] logic and test for s…
Browse files Browse the repository at this point in the history
…trict null checks (#6174)

* chore(website): [prefer-nullish-coalescing] explicit notice for strictNullChecks

* Added null check to prefer-nullish-coalescing along with a test.
  • Loading branch information
cparros committed Dec 6, 2022
1 parent 2d0a883 commit 8a91cbd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
@@ -1,5 +1,6 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';

import * as util from '../util';
Expand All @@ -9,13 +10,15 @@ export type Options = [
ignoreConditionalTests?: boolean;
ignoreTernaryTests?: boolean;
ignoreMixedLogicalExpressions?: boolean;
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean;
},
];

export type MessageIds =
| 'preferNullishOverOr'
| 'preferNullishOverTernary'
| 'suggestNullish';
| 'suggestNullish'
| 'noStrictNullCheck';

export default util.createRule<Options, MessageIds>({
name: 'prefer-nullish-coalescing',
Expand All @@ -34,6 +37,8 @@ export default util.createRule<Options, MessageIds>({
preferNullishOverTernary:
'Prefer using nullish coalescing operator (`??`) instead of a ternary expression, as it is simpler to read.',
suggestNullish: 'Fix to nullish coalescing operator (`??`).',
noStrictNullCheck:
'This rule requires the `strictNullChecks` compiler option to be turned on to function correctly.',
},
schema: [
{
Expand All @@ -48,6 +53,9 @@ export default util.createRule<Options, MessageIds>({
ignoreMixedLogicalExpressions: {
type: 'boolean',
},
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -58,6 +66,7 @@ export default util.createRule<Options, MessageIds>({
ignoreConditionalTests: true,
ignoreTernaryTests: true,
ignoreMixedLogicalExpressions: true,
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false,
},
],
create(
Expand All @@ -67,12 +76,31 @@ export default util.createRule<Options, MessageIds>({
ignoreConditionalTests,
ignoreTernaryTests,
ignoreMixedLogicalExpressions,
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing,
},
],
) {
const parserServices = util.getParserServices(context);
const compilerOptions = parserServices.program.getCompilerOptions();
const sourceCode = context.getSourceCode();
const checker = parserServices.program.getTypeChecker();
const isStrictNullChecks = tsutils.isStrictCompilerOptionEnabled(
compilerOptions,
'strictNullChecks',
);

if (
!isStrictNullChecks &&
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing !== true
) {
context.report({
loc: {
start: { line: 0, column: 0 },
end: { line: 0, column: 0 },
},
messageId: 'noStrictNullCheck',
});
}

return {
ConditionalExpression(node: TSESTree.ConditionalExpression): void {
Expand Down
@@ -1,4 +1,5 @@
import type { TSESLint } from '@typescript-eslint/utils';
import * as path from 'path';

import type {
MessageIds,
Expand Down Expand Up @@ -385,6 +386,25 @@ x ?? y;
],
})),

// noStrictNullCheck
{
code: `
declare const x: string[] | null;
if (x) {
}
`,
errors: [
{
messageId: 'noStrictNullCheck',
line: 0,
column: 1,
},
],
parserOptions: {
tsconfigRootDir: path.join(rootPath, 'unstrict'),
},
},

// ignoreConditionalTests
...nullishTypeInvalidTest((nullish, type) => ({
code: `
Expand Down

0 comments on commit 8a91cbd

Please sign in to comment.