Debian Codegraph @

eslint/babel-eslint-plugin/src/rules/no-unused-expressions.cjs

raw symbols SCIP
1 const ruleComposer = require("eslint-rule-composer");
2 const eslint = require("eslint");
3
4 const rule = new eslint.Linter().getRules().get("no-unused-expressions");
5
6 /**
7 * @param {ASTNode} node - any node
8 * @returns {boolean} whether the given node is either an IfStatement or an
9 * ExpressionStatement and is the last node in the body of a BlockStatement
10 */
11 function isFinalStatementInBlockStatement(node) {
12 const parent = node.parent;
13 return (
14 /^(?:If|Expression)Statement$/.test(node.type) &&
15 parent.type === "BlockStatement" &&
16 parent.body[parent.body.length - 1] === node
17 );
18 }
19
20 /**
21 * @param {ASTNode} node - any node
22 * @returns {boolean} whether the given node represents an unbroken chain of
23 * tail ExpressionStatements and IfStatements within a DoExpression
24 * https://github.com/tc39/proposal-do-expressions
25 */
26 function isInDoStatement(node) {
27 if (!node) return false;
28
29 if (node.type === "DoExpression") return true;
30
31 // this is an `else if`
32 if (
33 node.type === "IfStatement" &&
34 node.parent &&
35 node.parent.type === "IfStatement"
36 ) {
37 return isInDoStatement(node.parent);
38 }
39
40 if (isFinalStatementInBlockStatement(node)) {
41 return isInDoStatement(node.parent.parent);
42 }
43
44 return false;
45 }
46
47 /**
48 * @param {ASTNode} node - any node
49 * @returns {boolean} whether the given node is an optional call expression,
50 * https://github.com/tc39/proposal-optional-chaining
51 */
52 function isOptionalCallExpression(node) {
53 return (
54 !!node &&
55 node.type === "ExpressionStatement" &&
56 node.expression.type === "ChainExpression" &&
57 node.expression.expression.type === "CallExpression"
58 );
59 }
60
61 module.exports = ruleComposer.filterReports(
62 rule,
63 problem =>
64 !isInDoStatement(problem.node) && !isOptionalCallExpression(problem.node),
65 );