Skip to content

Commit 62ef487

Browse files
kozlovvskiMichał Kozłowski
and
Michał Kozłowski
authored
fix(eslint-plugin): [strict-boolean-expression] support falsy and truthy literals simultaneously (#6672)
* 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 <michal.kozlowski@start-up.house>
1 parent 58c102d commit 62ef487

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

packages/eslint-plugin/src/rules/strict-boolean-expressions.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,9 @@ export default util.createRule<Options, MessageId>({
835835
);
836836

837837
if (strings.length) {
838-
if (strings.some(type => type.isStringLiteral() && type.value !== '')) {
838+
if (
839+
strings.every(type => type.isStringLiteral() && type.value !== '')
840+
) {
839841
variantTypes.add('truthy string');
840842
} else {
841843
variantTypes.add('string');
@@ -848,8 +850,9 @@ export default util.createRule<Options, MessageId>({
848850
ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike,
849851
),
850852
);
853+
851854
if (numbers.length) {
852-
if (numbers.some(type => type.isNumberLiteral() && type.value !== 0)) {
855+
if (numbers.every(type => type.isNumberLiteral() && type.value !== 0)) {
853856
variantTypes.add('truthy number');
854857
} else {
855858
variantTypes.add('number');

packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ if (y) {
894894
declare const x: string | null; if (x) {}
895895
(x?: string) => !x;
896896
<T extends string | null | undefined>(x: T) => x ? 1 : 0;
897+
function foo(x: '' | 'bar' | null) { if (!x) {} }
897898
`,
898899
errors: [
899900
{
@@ -956,6 +957,28 @@ if (y) {
956957
},
957958
],
958959
},
960+
{
961+
messageId: 'conditionErrorNullableString',
962+
line: 5,
963+
column: 51,
964+
suggestions: [
965+
{
966+
messageId: 'conditionFixCompareNullish',
967+
output:
968+
" function foo(x: '' | 'bar' | null) { if (x == null) {} }",
969+
},
970+
{
971+
messageId: 'conditionFixDefaultEmptyString',
972+
output:
973+
" function foo(x: '' | 'bar' | null) { if (!(x ?? \"\")) {} }",
974+
},
975+
{
976+
messageId: 'conditionFixCastBoolean',
977+
output:
978+
" function foo(x: '' | 'bar' | null) { if (!Boolean(x)) {} }",
979+
},
980+
],
981+
},
959982
],
960983
}),
961984

@@ -965,6 +988,7 @@ if (y) {
965988
declare const x: number | null; if (x) {}
966989
(x?: number) => !x;
967990
<T extends number | null | undefined>(x: T) => x ? 1 : 0;
991+
function foo(x: 0 | 1 | null) { if (!x) {} }
968992
`,
969993
errors: [
970994
{
@@ -1027,6 +1051,28 @@ if (y) {
10271051
},
10281052
],
10291053
},
1054+
{
1055+
messageId: 'conditionErrorNullableNumber',
1056+
line: 5,
1057+
column: 46,
1058+
suggestions: [
1059+
{
1060+
messageId: 'conditionFixCompareNullish',
1061+
output:
1062+
' function foo(x: 0 | 1 | null) { if (x == null) {} }',
1063+
},
1064+
{
1065+
messageId: 'conditionFixDefaultZero',
1066+
output:
1067+
' function foo(x: 0 | 1 | null) { if (!(x ?? 0)) {} }',
1068+
},
1069+
{
1070+
messageId: 'conditionFixCastBoolean',
1071+
output:
1072+
' function foo(x: 0 | 1 | null) { if (!Boolean(x)) {} }',
1073+
},
1074+
],
1075+
},
10301076
],
10311077
}),
10321078

0 commit comments

Comments
 (0)