2 * @fileoverview A module that filters reported problems based on `eslint-disable` and `eslint-enable` comments
9 * Compares the locations of two objects in a source file
10 * @param {{line: number, column: number}} itemA The first object
11 * @param {{line: number, column: number}} itemB The second object
12 * @returns {number} A value less than 1 if itemA appears before itemB in the source file, greater than 1 if
13 * itemA appears after itemB in the source file, or 0 if itemA and itemB have the same location.
15 function compareLocations(itemA
, itemB
) {
16 return itemA
.line
- itemB
.line
|| itemA
.column
- itemB
.column
;
20 * This is the same as the exported function, except that it
21 * doesn't handle disable-line and disable-next-line directives, and it always reports unused
23 * @param {Object} options options for applying directives. This is the same as the options
24 * for the exported function, except that `reportUnusedDisableDirectives` is not supported
25 * (this function always reports unused disable directives).
26 * @returns {{problems: Problem[], unusedDisableDirectives: Problem[]}} An object with a list
27 * of filtered problems and unused eslint-disable directives
29 function applyDirectives(options
) {
31 let nextDirectiveIndex
= 0;
32 let currentGlobalDisableDirective
= null;
33 const disabledRuleMap
= new Map();
35 // enabledRules is only used when there is a current global disable directive.
36 const enabledRules
= new Set();
37 const usedDisableDirectives
= new Set();
39 for (const problem
of options
.problems
) {
41 nextDirectiveIndex
< options
.directives
.length
&&
42 compareLocations(options
.directives
[nextDirectiveIndex
], problem
) <= 0
44 const directive
= options
.directives
[nextDirectiveIndex
++];
46 switch (directive
.type
) {
48 if (directive
.ruleId
=== null) {
49 currentGlobalDisableDirective
= directive
;
50 disabledRuleMap
.clear();
52 } else if (currentGlobalDisableDirective
) {
53 enabledRules
.delete(directive
.ruleId
);
54 disabledRuleMap
.set(directive
.ruleId
, directive
);
56 disabledRuleMap
.set(directive
.ruleId
, directive
);
61 if (directive
.ruleId
=== null) {
62 currentGlobalDisableDirective
= null;
63 disabledRuleMap
.clear();
64 } else if (currentGlobalDisableDirective
) {
65 enabledRules
.add(directive
.ruleId
);
66 disabledRuleMap
.delete(directive
.ruleId
);
68 disabledRuleMap
.delete(directive
.ruleId
);
76 if (disabledRuleMap
.has(problem
.ruleId
)) {
77 usedDisableDirectives
.add(disabledRuleMap
.get(problem
.ruleId
));
78 } else if (currentGlobalDisableDirective
&& !enabledRules
.has(problem
.ruleId
)) {
79 usedDisableDirectives
.add(currentGlobalDisableDirective
);
81 problems
.push(problem
);
85 const unusedDisableDirectives
= options
.directives
86 .filter(directive
=> directive
.type
=== "disable" && !usedDisableDirectives
.has(directive
))
89 message
: directive
.ruleId
90 ? `Unused eslint-disable directive (no problems were reported from '${directive.ruleId}').`
91 : "Unused eslint-disable directive (no problems were reported).",
92 line
: directive
.unprocessedDirective
.line
,
93 column
: directive
.unprocessedDirective
.column
,
94 severity
: options
.reportUnusedDisableDirectives
=== "warn" ? 1 : 2,
98 return { problems
, unusedDisableDirectives
};
102 * Given a list of directive comments (i.e. metadata about eslint-disable and eslint-enable comments) and a list
103 * of reported problems, determines which problems should be reported.
104 * @param {Object} options Information about directives and problems
106 * type: ("disable"|"enable"|"disable-line"|"disable-next-line"),
107 * ruleId: (string|null),
110 * }} options.directives Directive comments found in the file, with one-based columns.
111 * Two directive comments can only have the same location if they also have the same type (e.g. a single eslint-disable
112 * comment for two different rules is represented as two directives).
113 * @param {{ruleId: (string|null), line: number, column: number}[]} options.problems
114 * A list of problems reported by rules, sorted by increasing location in the file, with one-based columns.
115 * @param {"off" | "warn" | "error"} options.reportUnusedDisableDirectives If `"warn"` or `"error"`, adds additional problems for unused directives
116 * @returns {{ruleId: (string|null), line: number, column: number}[]}
117 * A list of reported problems that were not disabled by the directive comments.
119 module
.exports
= ({ directives
, problems
, reportUnusedDisableDirectives
= "off" }) => {
120 const blockDirectives
= directives
121 .filter(directive
=> directive
.type
=== "disable" || directive
.type
=== "enable")
122 .map(directive
=> Object
.assign({}, directive
, { unprocessedDirective
: directive
}))
123 .sort(compareLocations
);
126 * Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.
127 * TODO(stephenwade): Replace this with array.flatMap when we drop support for Node v10
128 * @param {any[]} array The array to process
129 * @param {Function} fn The function to use
130 * @returns {any[]} The result array
132 function flatMap(array
, fn
) {
133 const mapped
= array
.map(fn
);
134 const flattened
= [].concat(...mapped
);
139 const lineDirectives
= flatMap(directives
, directive
=> {
140 switch (directive
.type
) {
147 { type
: "disable", line
: directive
.line
, column
: 1, ruleId
: directive
.ruleId
, unprocessedDirective
: directive
},
148 { type
: "enable", line
: directive
.line
+ 1, column
: 0, ruleId
: directive
.ruleId
, unprocessedDirective
: directive
}
151 case "disable-next-line":
153 { type
: "disable", line
: directive
.line
+ 1, column
: 1, ruleId
: directive
.ruleId
, unprocessedDirective
: directive
},
154 { type
: "enable", line
: directive
.line
+ 2, column
: 0, ruleId
: directive
.ruleId
, unprocessedDirective
: directive
}
158 throw new TypeError(`Unrecognized directive type '${directive.type}'`);
160 }).sort(compareLocations
);
162 const blockDirectivesResult
= applyDirectives({
164 directives
: blockDirectives
,
165 reportUnusedDisableDirectives
167 const lineDirectivesResult
= applyDirectives({
168 problems
: blockDirectivesResult
.problems
,
169 directives
: lineDirectives
,
170 reportUnusedDisableDirectives
173 return reportUnusedDisableDirectives
!== "off"
174 ? lineDirectivesResult
.problems
175 .concat(blockDirectivesResult
.unusedDisableDirectives
)
176 .concat(lineDirectivesResult
.unusedDisableDirectives
)
177 .sort(compareLocations
)
178 : lineDirectivesResult
.problems
;