diff --git a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts index 9f5a6661cca6..c6b6bf27a840 100644 --- a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts +++ b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts @@ -476,6 +476,13 @@ function isConditionalTest(node: TSESTree.Node): boolean { return isConditionalTest(parent); } + if ( + parent.type === AST_NODE_TYPES.UnaryExpression && + parent.operator === '!' + ) { + return isConditionalTest(parent); + } + if ( (parent.type === AST_NODE_TYPES.ConditionalExpression || parent.type === AST_NODE_TYPES.DoWhileStatement || 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 38e65b6c56ae..9e0bbdfab720 100644 --- a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts @@ -575,6 +575,34 @@ let b: string | boolean | undefined; let c: string | boolean | undefined; if (((a = b), b || c)) { +} + `, + options: [ + { + ignoreConditionalTests: true, + }, + ], + }, + { + code: ` +let a: string | undefined; +let b: string | undefined; + +if (!(a || b)) { +} + `, + options: [ + { + ignoreConditionalTests: true, + }, + ], + }, + { + code: ` +let a: string | undefined; +let b: string | undefined; + +if (!!(a || b)) { } `, options: [ @@ -2266,7 +2294,38 @@ if (f(a ?? b)) { ], options: [ { - ignoreBooleanCoercion: true, + ignoreConditionalTests: true, + }, + ], + }, + { + code: ` +declare const a: string | undefined; +declare const b: string; + +if (+(a || b)) { +} + `, + errors: [ + { + messageId: 'preferNullishOverOr', + suggestions: [ + { + messageId: 'suggestNullish', + output: ` +declare const a: string | undefined; +declare const b: string; + +if (+(a ?? b)) { +} + `, + }, + ], + }, + ], + options: [ + { + ignoreConditionalTests: true, }, ], },