Skip to content

fix(eslint-plugin): [strict-boolean-expressions] account for truthy literals #3236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions packages/eslint-plugin/src/rules/strict-boolean-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,16 @@ export default util.createRule<Options, MessageId>({
return;
}

// Known edge case: truthy primitives and nullish values are always valid boolean expressions
if (
(options.allowNumber && is('nullish', 'truthy number')) ||
(options.allowString && is('nullish', 'truthy string'))
) {
return;
}

// string
if (is('string')) {
if (is('string') || is('truthy string')) {
if (!options.allowString) {
if (isLogicalNegationExpression(node.parent!)) {
// if (!string)
Expand Down Expand Up @@ -458,7 +466,7 @@ export default util.createRule<Options, MessageId>({
}

// number
if (is('number')) {
if (is('number') || is('truthy number')) {
if (!options.allowNumber) {
if (isArrayLengthExpression(node, typeChecker, parserServices)) {
if (isLogicalNegationExpression(node.parent!)) {
Expand Down Expand Up @@ -701,7 +709,9 @@ export default util.createRule<Options, MessageId>({
| 'nullish'
| 'boolean'
| 'string'
| 'truthy string'
| 'number'
| 'truthy number'
| 'object'
| 'any'
| 'never';
Expand Down Expand Up @@ -731,21 +741,30 @@ export default util.createRule<Options, MessageId>({
variantTypes.add('boolean');
}

if (
types.some(type => tsutils.isTypeFlagSet(type, ts.TypeFlags.StringLike))
) {
variantTypes.add('string');
const strings = types.filter(type =>
tsutils.isTypeFlagSet(type, ts.TypeFlags.StringLike),
);

if (strings.length) {
if (strings.some(type => type.isStringLiteral() && type.value !== '')) {
variantTypes.add('truthy string');
} else {
variantTypes.add('string');
}
}

if (
types.some(type =>
tsutils.isTypeFlagSet(
type,
ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike,
),
)
) {
variantTypes.add('number');
const numbers = types.filter(type =>
tsutils.isTypeFlagSet(
type,
ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike,
),
);
if (numbers.length) {
if (numbers.some(type => type.isNumberLiteral() && type.value !== 0)) {
variantTypes.add('truthy number');
} else {
variantTypes.add('number');
}
}

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,57 @@ if (x) {
tsconfigRootDir: path.join(rootPath, 'unstrict'),
},
},

`
function f(arg: 'a' | null) {
if (arg) console.log(arg);
}
`,
`
function f(arg: 'a' | 'b' | null) {
if (arg) console.log(arg);
}
`,
{
code: `
declare const x: 1 | null;
declare const y: 1;
if (x) {
}
if (y) {
}
`,
options: [
{
allowNumber: true,
},
],
},
`
function f(arg: 1 | null) {
if (arg) console.log(arg);
}
`,
`
function f(arg: 1 | 2 | null) {
if (arg) console.log(arg);
}
`,
{
code: `
declare const x: 'a' | null;
declare const y: 'a';
if (x) {
}
if (y) {
}
`,
options: [
{
allowString: true,
},
],
},
],

invalid: [
Expand Down