Skip to content

Commit

Permalink
fix(eslint-plugin): [strict-boolean-expressions] account for truthy l…
Browse files Browse the repository at this point in the history
…iterals (#3236)
  • Loading branch information
Josh Goldberg committed Apr 5, 2021
1 parent 0221476 commit 0913f40
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
49 changes: 34 additions & 15 deletions packages/eslint-plugin/src/rules/strict-boolean-expressions.ts
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
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

0 comments on commit 0913f40

Please sign in to comment.