From 9d490f5dc8a0f0d2308dbc753a0081a0fa5eeecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Thu, 7 Nov 2024 09:39:23 +0900 Subject: [PATCH 1/4] fix: add unaryExpression to recursive --- .../src/rules/prefer-nullish-coalescing.ts | 4 +++ .../rules/prefer-nullish-coalescing.test.ts | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts index 9f5a6661cca6..b5942d1a0ff6 100644 --- a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts +++ b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts @@ -476,6 +476,10 @@ function isConditionalTest(node: TSESTree.Node): boolean { return isConditionalTest(parent); } + if (parent.type === AST_NODE_TYPES.UnaryExpression) { + 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..e267e57ef359 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: [ From 3f16099ca5a55540e1c307af4e29c878df78b361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Sat, 9 Nov 2024 01:44:54 +0900 Subject: [PATCH 2/4] fix: only apply ! operator --- .../src/rules/prefer-nullish-coalescing.ts | 5 ++- .../rules/prefer-nullish-coalescing.test.ts | 31 +++++++++++++++++++ 2 files changed, 35 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 b5942d1a0ff6..c6b6bf27a840 100644 --- a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts +++ b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts @@ -476,7 +476,10 @@ function isConditionalTest(node: TSESTree.Node): boolean { return isConditionalTest(parent); } - if (parent.type === AST_NODE_TYPES.UnaryExpression) { + if ( + parent.type === AST_NODE_TYPES.UnaryExpression && + parent.operator === '!' + ) { return isConditionalTest(parent); } 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 e267e57ef359..a84de9f24ae8 100644 --- a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts @@ -2286,6 +2286,37 @@ let b: string | boolean | undefined; declare function f(x: unknown): unknown; if (f(a ?? b)) { +} + `, + }, + ], + }, + ], + options: [ + { + ignoreBooleanCoercion: 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)) { } `, }, From 707188ed78b1c5eb1a3a9ad2fb5e383c7b3fe09c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Mon, 11 Nov 2024 21:37:57 +0900 Subject: [PATCH 3/4] feat: add same logic to boolean --- .../src/rules/prefer-nullish-coalescing.ts | 7 +++ .../rules/prefer-nullish-coalescing.test.ts | 57 ++++++++++++++++++- 2 files changed, 63 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 c6b6bf27a840..3d943c1852fd 100644 --- a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts +++ b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts @@ -524,6 +524,13 @@ function isBooleanConstructorContext( return isBooleanConstructorContext(parent, context); } + if ( + parent.type === AST_NODE_TYPES.UnaryExpression && + parent.operator === '!' + ) { + return isBooleanConstructorContext(parent, context); + } + return isBuiltInBooleanCall(parent, context); } 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 a84de9f24ae8..ab3c4d3fe1a0 100644 --- a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts @@ -611,6 +611,32 @@ if (!!(a || b)) { }, ], }, + { + code: ` +let a: string | undefined; +let b: string | undefined; + +Boolean(!!(a || b)); + `, + options: [ + { + ignoreBooleanCoercion: true, + }, + ], + }, + { + code: ` +let a: string | undefined; +let b: string | undefined; + +Boolean(!(a || b)); + `, + options: [ + { + ignoreBooleanCoercion: true, + }, + ], + }, ], invalid: [ ...nullishTypeTest((nullish, type, equals) => ({ @@ -2294,7 +2320,7 @@ if (f(a ?? b)) { ], options: [ { - ignoreBooleanCoercion: true, + ignoreConditionalTests: true, }, ], }, @@ -2318,6 +2344,35 @@ declare const b: string; if (+(a ?? b)) { } + `, + }, + ], + }, + ], + options: [ + { + ignoreConditionalTests: true, + }, + ], + }, + { + code: ` +declare const a: string | undefined; +declare const b: string; + +Boolean(+(a || b)); + `, + errors: [ + { + messageId: 'preferNullishOverOr', + suggestions: [ + { + messageId: 'suggestNullish', + output: ` +declare const a: string | undefined; +declare const b: string; + +Boolean(+(a ?? b)); `, }, ], From 6b4dbdaa07d610ab1311bff454fac32b6bffd93f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Wed, 13 Nov 2024 19:22:53 +0900 Subject: [PATCH 4/4] fix: revert ignoreBooleanCoercion option logic --- .../src/rules/prefer-nullish-coalescing.ts | 7 --- .../rules/prefer-nullish-coalescing.test.ts | 55 ------------------- 2 files changed, 62 deletions(-) diff --git a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts index 3d943c1852fd..c6b6bf27a840 100644 --- a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts +++ b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts @@ -524,13 +524,6 @@ function isBooleanConstructorContext( return isBooleanConstructorContext(parent, context); } - if ( - parent.type === AST_NODE_TYPES.UnaryExpression && - parent.operator === '!' - ) { - return isBooleanConstructorContext(parent, context); - } - return isBuiltInBooleanCall(parent, context); } 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 ab3c4d3fe1a0..9e0bbdfab720 100644 --- a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts @@ -611,32 +611,6 @@ if (!!(a || b)) { }, ], }, - { - code: ` -let a: string | undefined; -let b: string | undefined; - -Boolean(!!(a || b)); - `, - options: [ - { - ignoreBooleanCoercion: true, - }, - ], - }, - { - code: ` -let a: string | undefined; -let b: string | undefined; - -Boolean(!(a || b)); - `, - options: [ - { - ignoreBooleanCoercion: true, - }, - ], - }, ], invalid: [ ...nullishTypeTest((nullish, type, equals) => ({ @@ -2355,34 +2329,5 @@ if (+(a ?? b)) { }, ], }, - { - code: ` -declare const a: string | undefined; -declare const b: string; - -Boolean(+(a || b)); - `, - errors: [ - { - messageId: 'preferNullishOverOr', - suggestions: [ - { - messageId: 'suggestNullish', - output: ` -declare const a: string | undefined; -declare const b: string; - -Boolean(+(a ?? b)); - `, - }, - ], - }, - ], - options: [ - { - ignoreBooleanCoercion: true, - }, - ], - }, ], });