From 28d8cd7a7f54275488e5ad61a5e70bcee6469cf7 Mon Sep 17 00:00:00 2001 From: Olivier Zalmanski <88216225+OlivierZal@users.noreply.github.com> Date: Tue, 28 Jan 2025 17:04:23 +0100 Subject: [PATCH 1/2] fix(eslint-plugin): [prefer-nullish-coalescing] fix missing return --- .../src/rules/prefer-nullish-coalescing.ts | 2 ++ .../rules/prefer-nullish-coalescing.test.ts | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts index bb506b38a6ac..e52f83543af7 100644 --- a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts +++ b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts @@ -437,6 +437,8 @@ export default createRule({ isNodeEqual(testNode, node.alternate) ) { identifierOrMemberExpression = testNode; + } else { + return; } } } 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 51442dbc7467..ce4f99237378 100644 --- a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts @@ -98,6 +98,30 @@ x ??${equals} 'foo'; 'null != x ? y : x;', 'undefined != x ? y : x;', ` +declare let x: number | undefined; +x !== 15 && x !== undefined ? x : y; + `, + ` +declare let x: number | undefined; +x !== undefined && x !== 15 ? x : y; + `, + ` +declare let x: number | undefined; +15 !== x && undefined !== x ? x : y; + `, + ` +declare let x: number | undefined; +undefined !== x && 15 !== x ? x : y; + `, + ` +declare let x: number | undefined; +15 !== x && x !== undefined ? x : y; + `, + ` +declare let x: number | undefined; +undefined !== x && x !== 15 ? x : y; + `, + ` declare let x: string; x === null ? x : y; `, From 2d267d8830e746fff66fbafaed2644230e7011e2 Mon Sep 17 00:00:00 2001 From: Olivier Zalmanski <88216225+OlivierZal@users.noreply.github.com> Date: Tue, 28 Jan 2025 18:19:42 +0100 Subject: [PATCH 2/2] add tests --- .../tests/rules/prefer-nullish-coalescing.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 ce4f99237378..a9ad05c5cef5 100644 --- a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts @@ -122,6 +122,18 @@ declare let x: number | undefined; undefined !== x && x !== 15 ? x : y; `, ` +declare let x: string | undefined; +x !== 'foo' && x !== undefined ? x : y; + `, + ` +function test(value: number | undefined): number { + return value !== foo() && value !== undefined ? value : 1; +} + `, + ` +const test = (value: boolean | undefined): boolean => value !== undefined && value !== false ? value : false; + `, + ` declare let x: string; x === null ? x : y; `, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy