Merge pull request babel/eslint-plugin-babel#158 from lehni/fix/optional-call-expression-in-no-unused-expressions
This commit is contained in:
parent
2ea80c845e
commit
b3ce66b1ff
@ -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)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -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" }] },
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user