From 62ef487a99010827e99a792db5e565ad7c1d6220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Koz=C5=82owski?= Date: Mon, 20 Mar 2023 16:50:08 +0100 Subject: [PATCH] fix(eslint-plugin): [strict-boolean-expression] support falsy and truthy literals simultaneously (#6672) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * support falsy and truthy literals simultanously * replace Array.prototype.some condition with Array.prototype.every * format test cases to match prettier configuration --------- Co-authored-by: Michał Kozłowski --- .../src/rules/strict-boolean-expressions.ts | 7 ++- .../rules/strict-boolean-expressions.test.ts | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/strict-boolean-expressions.ts b/packages/eslint-plugin/src/rules/strict-boolean-expressions.ts index f1e8eacbdba..dc6d09c75a6 100644 --- a/packages/eslint-plugin/src/rules/strict-boolean-expressions.ts +++ b/packages/eslint-plugin/src/rules/strict-boolean-expressions.ts @@ -835,7 +835,9 @@ export default util.createRule({ ); if (strings.length) { - if (strings.some(type => type.isStringLiteral() && type.value !== '')) { + if ( + strings.every(type => type.isStringLiteral() && type.value !== '') + ) { variantTypes.add('truthy string'); } else { variantTypes.add('string'); @@ -848,8 +850,9 @@ export default util.createRule({ ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike, ), ); + if (numbers.length) { - if (numbers.some(type => type.isNumberLiteral() && type.value !== 0)) { + if (numbers.every(type => type.isNumberLiteral() && type.value !== 0)) { variantTypes.add('truthy number'); } else { variantTypes.add('number'); diff --git a/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts b/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts index 388c2d4660f..1e33bae9889 100644 --- a/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts +++ b/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts @@ -894,6 +894,7 @@ if (y) { declare const x: string | null; if (x) {} (x?: string) => !x; (x: T) => x ? 1 : 0; + function foo(x: '' | 'bar' | null) { if (!x) {} } `, errors: [ { @@ -956,6 +957,28 @@ if (y) { }, ], }, + { + messageId: 'conditionErrorNullableString', + line: 5, + column: 51, + suggestions: [ + { + messageId: 'conditionFixCompareNullish', + output: + " function foo(x: '' | 'bar' | null) { if (x == null) {} }", + }, + { + messageId: 'conditionFixDefaultEmptyString', + output: + " function foo(x: '' | 'bar' | null) { if (!(x ?? \"\")) {} }", + }, + { + messageId: 'conditionFixCastBoolean', + output: + " function foo(x: '' | 'bar' | null) { if (!Boolean(x)) {} }", + }, + ], + }, ], }), @@ -965,6 +988,7 @@ if (y) { declare const x: number | null; if (x) {} (x?: number) => !x; (x: T) => x ? 1 : 0; + function foo(x: 0 | 1 | null) { if (!x) {} } `, errors: [ { @@ -1027,6 +1051,28 @@ if (y) { }, ], }, + { + messageId: 'conditionErrorNullableNumber', + line: 5, + column: 46, + suggestions: [ + { + messageId: 'conditionFixCompareNullish', + output: + ' function foo(x: 0 | 1 | null) { if (x == null) {} }', + }, + { + messageId: 'conditionFixDefaultZero', + output: + ' function foo(x: 0 | 1 | null) { if (!(x ?? 0)) {} }', + }, + { + messageId: 'conditionFixCastBoolean', + output: + ' function foo(x: 0 | 1 | null) { if (!Boolean(x)) {} }', + }, + ], + }, ], }),