Skip to content

Commit 9ebf2a3

Browse files
committed
feat(linter/strict-boolean-expression): add rule
1 parent 010c88f commit 9ebf2a3

File tree

7 files changed

+465
-267
lines changed

7 files changed

+465
-267
lines changed

apps/oxlint/fixtures/tsgolint/.oxlintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"typescript/restrict-plus-operands": "error",
4040
"typescript/restrict-template-expressions": "error",
4141
"typescript/return-await": "error",
42+
"typescript/strict-boolean-expressions": "error",
4243
"typescript/switch-exhaustiveness-check": "error",
4344
"typescript/unbound-method": "error",
4445
"typescript/use-unknown-in-catch-callback-variable": "error",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Examples of incorrect code for strict-boolean-expressions rule
2+
3+
// Non-boolean values in conditions
4+
const str = 'hello';
5+
if (str) { // string is not a boolean
6+
console.log('string');
7+
}
8+
9+
const num = 42;
10+
if (num) { // number is not a boolean
11+
console.log('number');
12+
}
13+
14+
const obj = { foo: 'bar' };
15+
if (obj) { // object is not a boolean
16+
console.log('object');
17+
}
18+
19+
// Nullable booleans
20+
declare const maybeBool: boolean | null;
21+
if (maybeBool) { // nullable boolean
22+
console.log('maybe');
23+
}
24+
25+
// Undefined checks
26+
declare const maybeString: string | undefined;
27+
if (maybeString) { // should explicitly check !== undefined
28+
console.log(maybeString);
29+
}
30+
31+
// Logical operators with non-booleans
32+
const result1 = str && num;
33+
const result2 = obj || num;
34+
35+
// While loops with non-boolean
36+
while (num) {
37+
console.log('loop');
38+
break;
39+
}
40+
41+
// Do-while with non-boolean
42+
do {
43+
console.log('do');
44+
} while (str);
45+
46+
// For loop with non-boolean
47+
for (let i = 0; i; i++) {
48+
console.log('for');
49+
}
50+
51+
// Ternary with non-boolean
52+
const ternary = str ? 'yes' : 'no';
53+
54+
// Logical NOT on non-boolean
55+
const negated = !str;
56+
57+
// Mixed types in logical expressions
58+
declare const mixed: string | number;
59+
if (mixed) {
60+
console.log('mixed');
61+
}
62+
63+
// any type (should allow or warn depending on config)
64+
declare const anyValue: any;
65+
if (anyValue) {
66+
console.log('any');
67+
}

apps/oxlint/src/snapshots/fixtures__tsgolint_--type-aware no-floating-promises@oxlint.snap

Lines changed: 0 additions & 148 deletions
This file was deleted.

0 commit comments

Comments
 (0)