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

Support optional calls in no-unused-expressions #158

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?.();",
lehni marked this conversation as resolved.
Show resolved Hide resolved

],
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