From b89ec98f1bc3e249ff39bb6094c9654b6e02dbaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 3 Sep 2018 23:02:54 +0200 Subject: [PATCH 1/3] Support optional calls in no-unused-expressions Closes https://github.com/babel/babel-eslint/issues/643 --- rules/no-unused-expressions.js | 16 +++++++++++++++- tests/rules/no-unused-expressions.js | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/rules/no-unused-expressions.js b/rules/no-unused-expressions.js index 94628cb..eb1955a 100644 --- a/rules/no-unused-expressions.js +++ b/rules/no-unused-expressions.js @@ -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) ); diff --git a/tests/rules/no-unused-expressions.js b/tests/rules/no-unused-expressions.js index a8156ec..bd89208 100644 --- a/tests/rules/no-unused-expressions.js +++ b/tests/rules/no-unused-expressions.js @@ -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: [] }, ] }); From 36e6336b380866e62becce37baf127a4be5317ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Tue, 4 Sep 2018 09:52:12 +0200 Subject: [PATCH 2/3] Move optional call test to valid section --- tests/rules/no-unused-expressions.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/rules/no-unused-expressions.js b/tests/rules/no-unused-expressions.js index bd89208..08f7402 100644 --- a/tests/rules/no-unused-expressions.js +++ b/tests/rules/no-unused-expressions.js @@ -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: [ @@ -138,7 +139,6 @@ 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 { 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: [] }, ] }); From e7a3aa84ae6193d6b946ac6cdeb8cabd7c975e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Tue, 4 Sep 2018 19:42:07 +0200 Subject: [PATCH 3/3] Use consistent semicolons in babel-specific tests --- tests/rules/no-unused-expressions.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/rules/no-unused-expressions.js b/tests/rules/no-unused-expressions.js index 08f7402..8ecbf68 100644 --- a/tests/rules/no-unused-expressions.js +++ b/tests/rules/no-unused-expressions.js @@ -74,12 +74,12 @@ ruleTester.run("no-unused-expressions", rule, { }, // Babel-specific test cases. - "let a = do { if (foo) { foo.bar } }", - "let a = do { foo }", + "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 { (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?.();", ],