UNPKG

7.25 kBJavaScriptView Raw
1/**
2 * @fileoverview A module that filters reported problems based on `eslint-disable` and `eslint-enable` comments
3 * @author Teddy Katz
4 */
5
6"use strict";
7
8const lodash = require("lodash");
9
10/**
11 * Compares the locations of two objects in a source file
12 * @param {{line: number, column: number}} itemA The first object
13 * @param {{line: number, column: number}} itemB The second object
14 * @returns {number} A value less than 1 if itemA appears before itemB in the source file, greater than 1 if
15 * itemA appears after itemB in the source file, or 0 if itemA and itemB have the same location.
16 */
17function compareLocations(itemA, itemB) {
18 return itemA.line - itemB.line || itemA.column - itemB.column;
19}
20
21/**
22 * This is the same as the exported function, except that it
23 * doesn't handle disable-line and disable-next-line directives, and it always reports unused
24 * disable directives.
25 * @param {Object} options options for applying directives. This is the same as the options
26 * for the exported function, except that `reportUnusedDisableDirectives` is not supported
27 * (this function always reports unused disable directives).
28 * @returns {{problems: Problem[], unusedDisableDirectives: Problem[]}} An object with a list
29 * of filtered problems and unused eslint-disable directives
30 */
31function applyDirectives(options) {
32 const problems = [];
33 let nextDirectiveIndex = 0;
34 let currentGlobalDisableDirective = null;
35 const disabledRuleMap = new Map();
36
37 // enabledRules is only used when there is a current global disable directive.
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 // no default
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 source: null,
98 nodeType: null
99 }));
100
101 return { problems, unusedDisableDirectives };
102}
103
104/**
105 * Given a list of directive comments (i.e. metadata about eslint-disable and eslint-enable comments) and a list
106 * of reported problems, determines which problems should be reported.
107 * @param {Object} options Information about directives and problems
108 * @param {{
109 * type: ("disable"|"enable"|"disable-line"|"disable-next-line"),
110 * ruleId: (string|null),
111 * line: number,
112 * column: number
113 * }} options.directives Directive comments found in the file, with one-based columns.
114 * Two directive comments can only have the same location if they also have the same type (e.g. a single eslint-disable
115 * comment for two different rules is represented as two directives).
116 * @param {{ruleId: (string|null), line: number, column: number}[]} options.problems
117 * A list of problems reported by rules, sorted by increasing location in the file, with one-based columns.
118 * @param {boolean} options.reportUnusedDisableDirectives If `true`, adds additional problems for unused directives
119 * @returns {{ruleId: (string|null), line: number, column: number}[]}
120 * A list of reported problems that were not disabled by the directive comments.
121 */
122module.exports = options => {
123 const blockDirectives = options.directives
124 .filter(directive => directive.type === "disable" || directive.type === "enable")
125 .map(directive => Object.assign({}, directive, { unprocessedDirective: directive }))
126 .sort(compareLocations);
127
128 const lineDirectives = lodash.flatMap(options.directives, directive => {
129 switch (directive.type) {
130 case "disable":
131 case "enable":
132 return [];
133
134 case "disable-line":
135 return [
136 { type: "disable", line: directive.line, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive },
137 { type: "enable", line: directive.line + 1, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive }
138 ];
139
140 case "disable-next-line":
141 return [
142 { type: "disable", line: directive.line + 1, column: 1, ruleId: directive.ruleId, unprocessedDirective: directive },
143 { type: "enable", line: directive.line + 2, column: 0, ruleId: directive.ruleId, unprocessedDirective: directive }
144 ];
145
146 default:
147 throw new TypeError(`Unrecognized directive type '${directive.type}'`);
148 }
149 }).sort(compareLocations);
150
151 const blockDirectivesResult = applyDirectives({ problems: options.problems, directives: blockDirectives });
152 const lineDirectivesResult = applyDirectives({ problems: blockDirectivesResult.problems, directives: lineDirectives });
153
154 return options.reportUnusedDisableDirectives
155 ? lineDirectivesResult.problems
156 .concat(blockDirectivesResult.unusedDisableDirectives)
157 .concat(lineDirectivesResult.unusedDisableDirectives)
158 .sort(compareLocations)
159 : lineDirectivesResult.problems;
160};