1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const lodash = require("lodash");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function compareLocations(itemA, itemB) {
|
18 | return itemA.line - itemB.line || itemA.column - itemB.column;
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function applyDirectives(options) {
|
32 | const problems = [];
|
33 | let nextDirectiveIndex = 0;
|
34 | let currentGlobalDisableDirective = null;
|
35 | const disabledRuleMap = new Map();
|
36 |
|
37 |
|
38 | const enabledRules = new Set();
|
39 | const usedDisableDirectives = new Set();
|
40 |
|
41 | for (const problem of options.problems) {
|
42 | while (
|
43 | nextDirectiveIndex < options.directives.length &&
|
44 | compareLocations(options.directives[nextDirectiveIndex], problem) <= 0
|
45 | ) {
|
46 | const directive = options.directives[nextDirectiveIndex++];
|
47 |
|
48 | switch (directive.type) {
|
49 | case "disable":
|
50 | if (directive.ruleId === null) {
|
51 | currentGlobalDisableDirective = directive;
|
52 | disabledRuleMap.clear();
|
53 | enabledRules.clear();
|
54 | } else if (currentGlobalDisableDirective) {
|
55 | enabledRules.delete(directive.ruleId);
|
56 | disabledRuleMap.set(directive.ruleId, directive);
|
57 | } else {
|
58 | disabledRuleMap.set(directive.ruleId, directive);
|
59 | }
|
60 | break;
|
61 |
|
62 | case "enable":
|
63 | if (directive.ruleId === null) {
|
64 | currentGlobalDisableDirective = null;
|
65 | disabledRuleMap.clear();
|
66 | } else if (currentGlobalDisableDirective) {
|
67 | enabledRules.add(directive.ruleId);
|
68 | disabledRuleMap.delete(directive.ruleId);
|
69 | } else {
|
70 | disabledRuleMap.delete(directive.ruleId);
|
71 | }
|
72 | break;
|
73 |
|
74 |
|
75 | }
|
76 | }
|
77 |
|
78 | if (disabledRuleMap.has(problem.ruleId)) {
|
79 | usedDisableDirectives.add(disabledRuleMap.get(problem.ruleId));
|
80 | } else if (currentGlobalDisableDirective && !enabledRules.has(problem.ruleId)) {
|
81 | usedDisableDirectives.add(currentGlobalDisableDirective);
|
82 | } else {
|
83 | problems.push(problem);
|
84 | }
|
85 | }
|
86 |
|
87 | const unusedDisableDirectives = options.directives
|
88 | .filter(directive => directive.type === "disable" && !usedDisableDirectives.has(directive))
|
89 | .map(directive => ({
|
90 | ruleId: null,
|
91 | message: directive.ruleId
|
92 | ? `Unused eslint-disable directive (no problems were reported from '${directive.ruleId}').`
|
93 | : "Unused eslint-disable directive (no problems were reported).",
|
94 | line: directive.unprocessedDirective.line,
|
95 | column: directive.unprocessedDirective.column,
|
96 | severity: 2,
|
97 | nodeType: null
|
98 | }));
|
99 |
|
100 | return { problems, unusedDisableDirectives };
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | module.exports = options => {
|
122 | const blockDirectives = options.directives
|
123 | .filter(directive => directive.type === "disable" || directive.type === "enable")
|
124 | .map(directive => Object.assign({}, directive, { unprocessedDirective: directive }))
|
125 | .sort(compareLocations);
|
126 |
|
127 | const lineDirectives = lodash.flatMap(options.directives, directive => {
|
128 | switch (directive.type) {
|
129 | case "disable":
|
130 | case "enable":
|
131 | return [];
|
132 |
|
133 | case "disable-line":
|
134 | return [
|
135 | { type: "disable", line: directive.line, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive },
|
136 | { type: "enable", line: directive.line + 1, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive }
|
137 | ];
|
138 |
|
139 | case "disable-next-line":
|
140 | return [
|
141 | { type: "disable", line: directive.line + 1, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive },
|
142 | { type: "enable", line: directive.line + 2, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive }
|
143 | ];
|
144 |
|
145 | default:
|
146 | throw new TypeError(`Unrecognized directive type '${directive.type}'`);
|
147 | }
|
148 | }).sort(compareLocations);
|
149 |
|
150 | const blockDirectivesResult = applyDirectives({ problems: options.problems, directives: blockDirectives });
|
151 | const lineDirectivesResult = applyDirectives({ problems: blockDirectivesResult.problems, directives: lineDirectives });
|
152 |
|
153 | return options.reportUnusedDisableDirectives
|
154 | ? lineDirectivesResult.problems
|
155 | .concat(blockDirectivesResult.unusedDisableDirectives)
|
156 | .concat(lineDirectivesResult.unusedDisableDirectives)
|
157 | .sort(compareLocations)
|
158 | : lineDirectivesResult.problems;
|
159 | };
|