From 8a91cbd9fbe5bc4cf750cd949d2b8d48ff4c311d Mon Sep 17 00:00:00 2001 From: Cparros <65684072+cparros@users.noreply.github.com> Date: Tue, 6 Dec 2022 15:23:28 -0700 Subject: [PATCH] feat(eslint-plugin): [prefer-nullish-coalescing] logic and test for strict null checks (#6174) * chore(website): [prefer-nullish-coalescing] explicit notice for strictNullChecks * Added null check to prefer-nullish-coalescing along with a test. --- .../src/rules/prefer-nullish-coalescing.ts | 30 ++++++++++++++++++- .../rules/prefer-nullish-coalescing.test.ts | 20 +++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts index ca40160e982..14157427efa 100644 --- a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts +++ b/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'; @@ -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({ name: 'prefer-nullish-coalescing', @@ -34,6 +37,8 @@ export default util.createRule({ 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: [ { @@ -48,6 +53,9 @@ export default util.createRule({ ignoreMixedLogicalExpressions: { type: 'boolean', }, + allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: { + type: 'boolean', + }, }, additionalProperties: false, }, @@ -58,6 +66,7 @@ export default util.createRule({ ignoreConditionalTests: true, ignoreTernaryTests: true, ignoreMixedLogicalExpressions: true, + allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false, }, ], create( @@ -67,12 +76,31 @@ export default util.createRule({ 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 { diff --git a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts index 3824f464a58..49e50e741a8 100644 --- a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts @@ -1,4 +1,5 @@ import type { TSESLint } from '@typescript-eslint/utils'; +import * as path from 'path'; import type { MessageIds, @@ -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: `