Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: false positive for optional call expressions in no-unused-expressions #448

Merged
merged 1 commit into from Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .README/rules/no-unused-expressions.md
@@ -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
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
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()',
},
],
};