1 | "use strict";
|
2 |
|
3 | const ruleComposer = require('eslint-rule-composer');
|
4 | const eslint = require('eslint');
|
5 | const rule = new eslint.Linter().getRules().get('no-unused-expressions');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function isFinalStatementInBlockStatement(node) {
|
13 | const parent = node.parent;
|
14 | return /^(?: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 | function isInDoStatement(node) {
|
25 | if (!node) return false;
|
26 |
|
27 | if (node.type === 'DoExpression') return true;
|
28 |
|
29 |
|
30 | if (
|
31 | node.type === 'IfStatement' &&
|
32 | node.parent &&
|
33 | node.parent.type === 'IfStatement'
|
34 | ) {
|
35 | return isInDoStatement(node.parent);
|
36 | }
|
37 |
|
38 | if (isFinalStatementInBlockStatement(node)) {
|
39 | return isInDoStatement(node.parent.parent);
|
40 | }
|
41 |
|
42 | return false;
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function isOptionalCallExpression(node) {
|
51 | return (
|
52 | !!node &&
|
53 | node.type === 'ExpressionStatement' &&
|
54 | node.expression.type === 'OptionalCallExpression'
|
55 | );
|
56 | }
|
57 |
|
58 | module.exports = ruleComposer.filterReports(
|
59 | rule,
|
60 | (problem, metadata) =>
|
61 | !isInDoStatement(problem.node) && !isOptionalCallExpression(problem.node)
|
62 | );
|
63 |
|