8000 feat(eslint-plugin): [strict-boolean-expressions] add ignoreRhs optio… · ajmacd/typescript-eslint@fd6be42 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd6be42

Browse files
RetsamJamesHenry
authored andcommitted
feat(eslint-plugin): [strict-boolean-expressions] add ignoreRhs option (typescript-eslint#691)
1 parent f6f89e5 commit fd6be42

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

packages/eslint-plugin/docs/rules/strict-boolean-expressions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ while (typeof str !== 'undefined') {
5252
}
5353
```
5454

55+
## Options
56+
57+
Options may be provided as an object with:
58+
59+
- `ignoreRhs` to skip the check on the right hand side of expressions like `a && b` or `a || b` - allows these operators to be used for their short-circuiting behavior. (`false` by default).
60+
5561
## Related To
5662

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

packages/eslint-plugin/src/rules/strict-boolean-expressions.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ type ExpressionWithTest =
1313
| TSESTree.IfStatement
1414
| TSESTree.WhileStatement;
1515

16-
export default util.createRule({
16+
type Options = [
17+
{
18+
ignoreRhs?: boolean;
19+
}
20+
];
21+
22+
export default util.createRule<Options, 'strictBooleanExpression'>({
1723
name: 'strict-boolean-expressions',
1824
meta: {
1925
type: 'suggestion',
@@ -22,13 +28,27 @@ export default util.createRule({
2228
category: 'Best Practices',
2329
recommended: false,
2430
},
25-
schema: [],
31+
schema: [
32+
{
33+
type: 'object',
34+
properties: {
35+
ignoreRhs: {
36+
type: 'boolean',
37+
},
38+
},
39+
additionalProperties: false,
40+
},
41+
],
2642
messages: {
2743
strictBooleanExpression: 'Unexpected non-boolean in conditional.',
2844
},
2945
},
30-
defaultOptions: [],
31-
create(context) {
46+
defaultOptions: [
47+
{
48+
ignoreRhs: false,
49+
},
50+
],
51+
create(context, [{ ignoreRhs }]) {
3252
const service = util.getParserServices(context);
3353
const checker = service.program.getTypeChecker();
3454

@@ -65,7 +85,10 @@ export default util.createRule({
6585
function assertLocalExpressionContainsBoolean(
6686
node: TSESTree.LogicalExpression,
6787
): void {
68-
if (!isBooleanType(node.left) || !isBooleanType(node.right)) {
88+
if (
89+
!isBooleanType(node.left) ||
90+
(!ignoreRhs && !isBooleanType(node.right))
91+
) {
6992
reportNode(node);
7093
}
7194
}

packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ ruleTester.run('strict-boolean-expressions', rule, {
156156
`
157157
function foo<T extends boolean>(arg: T) { return !arg; }
158158
`,
159+
{
160+
options: [{ ignoreRhs: true }],
161+
code: `
162+
const obj = {};
163+
const bool = false;
164+
const boolOrObj = bool || obj;
165+
const boolAndObj = bool && obj;
166+
`,
167+
},
159168
],
160169

161170
invalid: [
@@ -903,5 +912,26 @@ ruleTester.run('strict-boolean-expressions', rule, {
903912
},
904913
],
905914
},
915+
{
916+
options: [{ ignoreRhs: true }],
917+
errors: [
918+
{
919+
messageId: 'strictBooleanExpression',
920+
line: 4,
921+
column: 19,
922+
},
923+
{
924+
messageId: 'strictBooleanExpression',
925+
line: 5,
926+
column: 20,
927+
},
928+
],
929+
code: `
930+
const obj = {};
931+
const bool = false;
932+
const objOrBool = obj || bool;
933+
const objAndBool = obj && bool;
934+
`,
935+
},
906936
],
907937
});

0 commit comments

Comments
 (0)
0