Skip to content

Commit

Permalink
Support optional calls in no-unused-expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
lehni committed Sep 3, 2018
1 parent e565670 commit d0f7952
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
16 changes: 15 additions & 1 deletion rules/no-unused-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,22 @@ function isInDoStatement(node) {
return false;
}

/**
* @param {ASTNode} node - any node
* @returns {boolean} whether the given node is an optional call expression,
* see https://github.com/tc39/proposal-optional-chaining
*/
function isInOptionalCallExpression(node) {
return (
!!node &&
node.type === 'ExpressionStatement' &&
node.expression.type === 'OptionalCallExpression'
);
}

module.exports = ruleComposer.filterReports(
rule,
(problem, metadata) => !isInDoStatement(problem.node)
(problem, metadata) =>
!isInDoStatement(problem.node) && !isInOptionalCallExpression(problem.node)
);

3 changes: 2 additions & 1 deletion tests/rules/no-unused-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ ruleTester.run("no-unused-expressions", rule, {
},

// Babel-specific test cases.
{ code: "let a = do { foo; let b = 2; }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "let a = do { foo; let b = 2; }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "let a = do { if (foo) { foo.bar } else { a; bar.foo } }", errors: [{ message: "Expected an assignment or function call and instead saw an expression.", type: "ExpressionStatement" }] },
{ code: "function foo() { foo.bar?.(); }", errors: [] },

]
});

0 comments on commit d0f7952

Please sign in to comment.