| 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 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
function isInDoStatement(node) { |
| 27 |
if (!node) return false; |
| 28 |
|
| 29 |
if (node.type === "DoExpression") return true; |
| 30 |
|
| 31 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 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 |
); |