Merge pull request babel/eslint-plugin-babel#158 from lehni/fix/optional-call-expression-in-no-unused-expressions

This commit is contained in:
Brian Ng 2018-09-05 09:20:31 -05:00
parent 2ea80c845e
commit b3ce66b1ff
2 changed files with 23 additions and 8 deletions

View File

@ -42,8 +42,22 @@ function isInDoStatement(node) {
return false; 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( module.exports = ruleComposer.filterReports(
rule, rule,
(problem, metadata) => !isInDoStatement(problem.node) (problem, metadata) =>
!isInDoStatement(problem.node) && !isOptionalCallExpression(problem.node)
); );

View File

@ -74,12 +74,13 @@ ruleTester.run("no-unused-expressions", rule, {
}, },
// Babel-specific test cases. // Babel-specific test cases.
"let a = do { if (foo) { foo.bar } }", "let a = do { if (foo) { foo.bar; } }",
"let a = do { foo }", "let a = do { foo; }",
"let a = do { let b = 2; foo; }", "let a = do { let b = 2; foo; }",
"let a = do { (foo + 1) }", "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; } } }",
"let a = do { if (foo) { if (foo.bar) { foo.bar } else if (foo.baz) { foo.baz } } }", "let a = do { if (foo) { if (foo.bar) { foo.bar; } else if (foo.baz) { foo.baz; } } }",
"foo.bar?.();",
], ],
invalid: [ invalid: [
@ -136,7 +137,7 @@ ruleTester.run("no-unused-expressions", rule, {
}, },
// Babel-specific test cases. // 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: "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" }] },
] ]