Skip to content

fix(eslint-plugin): [strict-boolean-expressions] remove remaining (unsafe) autofixes #10548

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
merged 2 commits into from
Dec 31, 2024
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
32 changes: 0 additions & 32 deletions packages/eslint-plugin/docs/rules/strict-boolean-expressions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -168,38 +168,6 @@ You should be using `strictNullChecks` to ensure complete type-safety in your co

If for some reason you cannot turn on `strictNullChecks`, but still want to use this rule - you can use this option to allow it - but know that the behavior of this rule is _undefined_ with the compiler option turned off. We will not accept bug reports if you are using this option.

## Fixes and Suggestions

This rule provides following fixes and suggestions for particular types in boolean context:

- `boolean` - Always allowed - no fix needed.
- `string` - (when `allowString` is `false`) - Provides following suggestions:
- Change condition to check string's length (`str` → `str.length > 0`)
- Change condition to check for empty string (`str` → `str !== ""`)
- Explicitly convert value to a boolean (`str` → `Boolean(str)`)
- `number` - (when `allowNumber` is `false`):
- For `array.length` - Provides **autofix**:
- Change condition to check for 0 (`array.length` → `array.length > 0`)
- For other number values - Provides following suggestions:
- Change condition to check for 0 (`num` → `num !== 0`)
- Change condition to check for NaN (`num` → `!Number.isNaN(num)`)
- Explicitly convert value to a boolean (`num` → `Boolean(num)`)
- `object | null | undefined` - (when `allowNullableObject` is `false`) - Provides **autofix**:
- Change condition to check for null/undefined (`maybeObj` → `maybeObj != null`)
- `boolean | null | undefined` - Provides following suggestions:
- Explicitly treat nullish value the same as false (`maybeBool` → `maybeBool ?? false`)
- Change condition to check for true/false (`maybeBool` → `maybeBool === true`)
- `string | null | undefined` - Provides following suggestions:
- Change condition to check for null/undefined (`maybeStr` → `maybeStr != null`)
- Explicitly treat nullish value the same as an empty string (`maybeStr` → `maybeStr ?? ""`)
- Explicitly convert value to a boolean (`maybeStr` → `Boolean(maybeStr)`)
- `number | null | undefined` - Provides following suggestions:
- Change condition to check for null/undefined (`maybeNum` → `maybeNum != null`)
- Explicitly treat nullish value the same as 0 (`maybeNum` → `maybeNum ?? 0`)
- Explicitly convert value to a boolean (`maybeNum` → `Boolean(maybeNum)`)
- `any` and `unknown` - Provides following suggestions:
- Explicitly convert value to a boolean (`value` → `Boolean(value)`)

## When Not To Use It

If your project isn't likely to experience bugs from falsy non-boolean values being used in logical conditions, you can skip enabling this rule.
Expand Down
71 changes: 48 additions & 23 deletions packages/eslint-plugin/src/rules/strict-boolean-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export type MessageId =
| 'conditionErrorOther'
| 'conditionErrorString'
| 'conditionFixCastBoolean'
| 'conditionFixCompareArrayLengthNonzero'
| 'conditionFixCompareArrayLengthZero'
| 'conditionFixCompareEmptyString'
| 'conditionFixCompareFalse'
| 'conditionFixCompareNaN'
Expand All @@ -63,7 +65,6 @@ export default createRule<Options, MessageId>({
description: 'Disallow certain types in boolean expressions',
requiresTypeChecking: true,
},
fixable: 'code',
hasSuggestions: true,
messages: {
conditionErrorAny:
Expand Down Expand Up @@ -102,6 +103,10 @@ export default createRule<Options, MessageId>({
conditionFixCastBoolean:
'Explicitly convert value to a boolean (`Boolean(value)`)',

conditionFixCompareArrayLengthNonzero:
"Change condition to check array's length (`value.length > 0`)",
conditionFixCompareArrayLengthZero:
"Change condition to check array's length (`value.length === 0`)",
conditionFixCompareEmptyString:
'Change condition to check for empty string (`value !== ""`)',
conditionFixCompareFalse:
Expand Down Expand Up @@ -571,23 +576,33 @@ export default createRule<Options, MessageId>({
context.report({
node,
messageId: 'conditionErrorNumber',
fix: getWrappingFixer({
node: node.parent,
innerNode: node,
sourceCode: context.sourceCode,
wrap: code => `${code} === 0`,
}),
suggest: [
{
messageId: 'conditionFixCompareArrayLengthZero',
fix: getWrappingFixer({
node: node.parent,
innerNode: node,
sourceCode: context.sourceCode,
wrap: code => `${code} === 0`,
}),
},
],
});
} else {
// if (array.length)
context.report({
node,
messageId: 'conditionErrorNumber',
fix: getWrappingFixer({
node,
sourceCode: context.sourceCode,
wrap: code => `${code} > 0`,
}),
suggest: [
{
messageId: 'conditionFixCompareArrayLengthNonzero',
fix: getWrappingFixer({
node,
sourceCode: context.sourceCode,
wrap: code => `${code} > 0`,
}),
},
],
});
}
} else if (isLogicalNegationExpression(node.parent)) {
Expand Down Expand Up @@ -803,22 +818,32 @@ export default createRule<Options, MessageId>({
context.report({
node,
messageId: 'conditionErrorNullableEnum',
fix: getWrappingFixer({
node: node.parent,
innerNode: node,
sourceCode: context.sourceCode,
wrap: code => `${code} == null`,
}),
suggest: [
{
messageId: 'conditionFixCompareNullish',
fix: getWrappingFixer({
node: node.parent,
innerNode: node,
sourceCode: context.sourceCode,
wrap: code => `${code} == null`,
}),
},
],
});
} else {
context.report({
node,
messageId: 'conditionErrorNullableEnum',
fix: getWrappingFixer({
node,
sourceCode: context.sourceCode,
wrap: code => `${code} != null`,
}),
suggest: [
{
messageId: 'conditionFixCompareNullish',
fix: getWrappingFixer({
node,
sourceCode: context.sourceCode,
wrap: code => `${code} != null`,
}),
},
],
});
}
}
Expand Down
Loading
Loading