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

feat(eslint-plugin): [prefer-nullish-coalescing] logic and test for strict null checks #6174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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