Is there a TSLint/ESLint rule to prevent (boolean === true) type of comparisons?
Asked Answered
D

2

6

Is there a TSLint/ESLint rule to prevent unnecessary boolean comparison like:

if (result === false) {
  // do something
} 
Doyenne answered 5/2, 2016 at 9:39 Comment(2)
I'm not aware of a built-in rule or a custom rule that does this. However, sounds like a reasonable feature request!Aretina
I've submitted a rule request to core eslint: github.com/eslint/eslint/issues/9743Erysipelas
M
4

TSLint supports rule no-boolean-literal-compare which does just this.

"no-boolean-literal-compare": true in the rules array will enable this feature.

Documentation link: https://palantir.github.io/tslint/rules/no-boolean-literal-compare/

Mechanist answered 21/10, 2019 at 20:39 Comment(0)
E
2

You can currently accomplish this with ESLint:

"no-restricted-syntax": [
    "error",
     {
        "selector": "BinaryExpression[operator=/^(==|===|!=|!==)$/][left.raw=/^(true|false)$/], BinaryExpression[operator=/^(==|===|!=|!==)$/][right.raw=/^(true|false)$/]",
        "message": "Don't compare for equality against boolean literals"
     }
]

The selector disallows usage of ==, ===, !=, and !== when either (or both) of the operands are boolean literals.


Source

Erysipelas answered 21/12, 2017 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.