Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #158 from lehni/fix/optional-call-expression-in-no…
Browse files Browse the repository at this point in the history
…-unused-expressions
  • Loading branch information
existentialism authored Sep 5, 2018
2 parents 6d07cd2 + e7a3aa8 commit d653393
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 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 isOptionalCallExpression(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) && !isOptionalCallExpression(problem.node)
);

15 changes: 8 additions & 7 deletions tests/rules/no-unused-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ ruleTester.run("no-unused-expressions", rule, {
},

// Babel-specific test cases.
"let a = do { if (foo) { foo.bar } }",
"let a = do { foo }",
"let a = do { let b = 2; foo; }",
"let a = do { (foo + 1) }",
"let a = do { if (foo) { if (foo.bar) { foo.bar } } }",
"let a = do { if (foo) { if (foo.bar) { foo.bar } else if (foo.baz) { foo.baz } } }",
"let a = do { if (foo) { foo.bar; } }",
"let a = do { foo; }",
"let a = do { let b = 2; foo; }",
"let a = do { (foo + 1); }",
"let a = do { if (foo) { if (foo.bar) { foo.bar; } } }",
"let a = do { if (foo) { if (foo.bar) { foo.bar; } else if (foo.baz) { foo.baz; } } }",
"foo.bar?.();",

],
invalid: [
Expand Down Expand Up @@ -136,7 +137,7 @@ 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" }] },

]
Expand Down

0 comments on commit d653393

Please sign in to comment.