Skip to content

Commit a91db33

Browse files
authored
fix: false positive for optional call expressions in no-unused-expressions (#448)
1 parent 506b18e commit a91db33

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

.README/rules/no-unused-expressions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### `no-unused-expressions`
22

33
An extension of [ESLint's `no-unused-expressions`](https://eslint.org/docs/rules/no-unused-expressions).
4-
This rule ignores type cast expressions, but otherwise behaves the same as ESLint's
4+
This rule ignores type cast expressions and optional call expressions, but otherwise behaves the same as ESLint's
55
`no-unused-expressions`.
66

77
Bare type casts are useful, for example to assert the exhaustiveness of a `switch`:

src/rules/noUnusedExpressions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ const create = (context) => {
1010

1111
return {
1212
ExpressionStatement (node) {
13-
if (node.expression.type !== 'TypeCastExpression') {
14-
coreChecks.ExpressionStatement(node);
13+
if (
14+
node.expression.type === 'TypeCastExpression' ||
15+
node.expression.type === 'OptionalCallExpression'
16+
) {
17+
return;
1518
}
19+
coreChecks.ExpressionStatement(node);
1620
},
1721
};
1822
};

tests/rules/assertions/noUnusedExpressions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ export default {
66
message: 'Expected an assignment or function call and instead saw an expression.',
77
}],
88
},
9+
{
10+
code: 'x?.y',
11+
errors: [{
12+
message: 'Expected an assignment or function call and instead saw an expression.',
13+
}],
14+
},
915
],
1016
valid: [
1117
{
1218
code: '(foo: number)',
1319
},
20+
{
21+
code: 'x?.y()',
22+
},
1423
],
1524
};

0 commit comments

Comments
 (0)