Skip to content

Commit

Permalink
fix: false positive for optional call expressions in no-unused-expres…
Browse files Browse the repository at this point in the history
…sions (#448)
  • Loading branch information
bradzacher authored Jun 29, 2020
1 parent 506b18e commit a91db33
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .README/rules/no-unused-expressions.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
### `no-unused-expressions`

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

Bare type casts are useful, for example to assert the exhaustiveness of a `switch`:
Expand Down
8 changes: 6 additions & 2 deletions src/rules/noUnusedExpressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ const create = (context) => {

return {
ExpressionStatement (node) {
if (node.expression.type !== 'TypeCastExpression') {
coreChecks.ExpressionStatement(node);
if (
node.expression.type === 'TypeCastExpression' ||
node.expression.type === 'OptionalCallExpression'
) {
return;
}
coreChecks.ExpressionStatement(node);
},
};
};
Expand Down
9 changes: 9 additions & 0 deletions tests/rules/assertions/noUnusedExpressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ export default {
message: 'Expected an assignment or function call and instead saw an expression.',
}],
},
{
code: 'x?.y',
errors: [{
message: 'Expected an assignment or function call and instead saw an expression.',
}],
},
],
valid: [
{
code: '(foo: number)',
},
{
code: 'x?.y()',
},
],
};

0 comments on commit a91db33

Please sign in to comment.