diff --git a/.README/rules/no-unused-expressions.md b/.README/rules/no-unused-expressions.md index 4dcfc2ff..1a81faf3 100644 --- a/.README/rules/no-unused-expressions.md +++ b/.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`: diff --git a/src/rules/noUnusedExpressions.js b/src/rules/noUnusedExpressions.js index 2fd292b7..2cfdf736 100644 --- a/src/rules/noUnusedExpressions.js +++ b/src/rules/noUnusedExpressions.js @@ -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); }, }; }; diff --git a/tests/rules/assertions/noUnusedExpressions.js b/tests/rules/assertions/noUnusedExpressions.js index 1d113005..67171341 100644 --- a/tests/rules/assertions/noUnusedExpressions.js +++ b/tests/rules/assertions/noUnusedExpressions.js @@ -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()', + }, ], };