Skip to content

Commit

Permalink
feat(eslint-plugin): [strict-bool-expr] add fixes and suggestions (#2847
Browse files Browse the repository at this point in the history
)
  • Loading branch information
phaux committed Mar 8, 2021
1 parent ae0271c commit 3f9e9a1
Show file tree
Hide file tree
Showing 6 changed files with 1,167 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/README.md
Expand Up @@ -169,7 +169,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
| [`@typescript-eslint/restrict-plus-operands`](./docs/rules/restrict-plus-operands.md) | When adding two variables, operands must both be of type number or of type string | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/restrict-template-expressions`](./docs/rules/restrict-template-expressions.md) | Enforce template literal expressions to be of string type | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/sort-type-union-intersection-members`](./docs/rules/sort-type-union-intersection-members.md) | Enforces that members of a type union/intersection are sorted alphabetically | | :wrench: | |
| [`@typescript-eslint/strict-boolean-expressions`](./docs/rules/strict-boolean-expressions.md) | Restricts the types allowed in boolean expressions | | | :thought_balloon: |
| [`@typescript-eslint/strict-boolean-expressions`](./docs/rules/strict-boolean-expressions.md) | Restricts the types allowed in boolean expressions | | :wrench: | :thought_balloon: |
| [`@typescript-eslint/switch-exhaustiveness-check`](./docs/rules/switch-exhaustiveness-check.md) | Exhaustiveness checking in switch with union type | | | :thought_balloon: |
| [`@typescript-eslint/triple-slash-reference`](./docs/rules/triple-slash-reference.md) | Sets preference level for triple slash directives versus ES6-style import declarations | :heavy_check_mark: | | |
| [`@typescript-eslint/type-annotation-spacing`](./docs/rules/type-annotation-spacing.md) | Require consistent spacing around type annotations | | :wrench: | |
Expand Down
32 changes: 32 additions & 0 deletions packages/eslint-plugin/docs/rules/strict-boolean-expressions.md
Expand Up @@ -155,6 +155,38 @@ 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 cast 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 cast 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 cast 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 cast value to a boolean (`maybeNum``Boolean(maybeNum)`)
- `any` and `unknown` - Provides following suggestions:
- Explicitly cast value to a boolean (`value``Boolean(value)`)

## Related To

- TSLint: [strict-boolean-expressions](https://palantir.github.io/tslint/rules/strict-boolean-expressions)
Expand Down

0 comments on commit 3f9e9a1

Please sign in to comment.