1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const nodes_1 = require("../utils/nodes");
|
23 | const equivalence_1 = require("../utils/equivalence");
|
24 | const conditions_1 = require("../utils/conditions");
|
25 | const docs_url_1 = require("../utils/docs-url");
|
26 | const MESSAGE = "Remove this conditional structure or edit its code blocks so that they're not all the same.";
|
27 | const MESSAGE_CONDITIONAL_EXPRESSION = 'This conditional operation returns the same value whether the condition is "true" or "false".';
|
28 | const rule = {
|
29 | meta: {
|
30 | type: 'problem',
|
31 | docs: {
|
32 | description: 'All branches in a conditional structure should not have exactly the same implementation',
|
33 | category: 'Possible Errors',
|
34 | recommended: 'error',
|
35 | url: docs_url_1.default(__filename),
|
36 | },
|
37 | },
|
38 | create(context) {
|
39 | return {
|
40 | IfStatement(node) {
|
41 | const ifStmt = node;
|
42 |
|
43 | if (!nodes_1.isIfStatement(node.parent)) {
|
44 | const { branches, endsWithElse } = conditions_1.collectIfBranches(ifStmt);
|
45 | if (endsWithElse && allDuplicated(branches)) {
|
46 | context.report({ message: MESSAGE, node: ifStmt });
|
47 | }
|
48 | }
|
49 | },
|
50 | SwitchStatement(node) {
|
51 | const switchStmt = node;
|
52 | const { branches, endsWithDefault } = conditions_1.collectSwitchBranches(switchStmt);
|
53 | if (endsWithDefault && allDuplicated(branches)) {
|
54 | context.report({ message: MESSAGE, node: switchStmt });
|
55 | }
|
56 | },
|
57 | ConditionalExpression(node) {
|
58 | const conditional = node;
|
59 | const branches = [conditional.consequent, conditional.alternate];
|
60 | if (allDuplicated(branches)) {
|
61 | context.report({ message: MESSAGE_CONDITIONAL_EXPRESSION, node: conditional });
|
62 | }
|
63 | },
|
64 | };
|
65 | function allDuplicated(branches) {
|
66 | return (branches.length > 1 &&
|
67 | branches.slice(1).every((branch, index) => {
|
68 | return equivalence_1.areEquivalent(branch, branches[index], context.getSourceCode());
|
69 | }));
|
70 | }
|
71 | },
|
72 | };
|
73 | module.exports = rule;
|
74 |
|
\ | No newline at end of file |