@@ -19,46 +19,11 @@ function compareLocations(itemA, itemB) {
19
19
}
20
20
21
21
/**
22
- * Given a list of directive comments (i.e. metadata about eslint-disable and eslint-enable comments) and a list
23
- * of reported problems, determines which problems should be reported.
24
- * @param {Object } options Information about directives and problems
25
- * @param {{
26
- * type: ("disable"|"enable"|"disable-line"|"disable-next-line"),
27
- * ruleId: (string|null),
28
- * line: number,
29
- * column: number
30
- * }} options.directives Directive comments found in the file, with one-based columns.
31
- * Two directive comments can only have the same location if they also have the same type (e.g. a single eslint-disable
32
- * comment for two different rules is represented as two directives).
33
- * @param {{ruleId: (string|null), line: number, column: number}[] } options.problems
34
- * A list of problems reported by rules, sorted by increasing location in the file, with one-based columns.
35
- * @returns {{ruleId: (string|null), line: number, column: number}[] }
36
- * A list of reported problems that were not disabled by the directive comments.
22
+ * This is the same as the exported function, except that it doesn't handle disable-line and disable-next-line directives.
23
+ * @param {Object } options options (see the exported function)
24
+ * @returns {Problem[] } Filtered problems (see the exported function)
37
25
*/
38
- module . exports = options => {
39
- const processedDirectives = lodash . flatMap ( options . directives , directive => {
40
- switch ( directive . type ) {
41
- case "disable" :
42
- case "enable" :
43
- return [ directive ] ;
44
-
45
- case "disable-line" :
46
- return [
47
- { type : "disable" , line : directive . line , column : 1 , ruleId : directive . ruleId } ,
48
- { type : "enable" , line : directive . line + 1 , column : 1 , ruleId : directive . ruleId }
49
- ] ;
50
-
51
- case "disable-next-line" :
52
- return [
53
- { type : "disable" , line : directive . line + 1 , column : 1 , ruleId : directive . ruleId } ,
54
- { type : "enable" , line : directive . line + 2 , column : 1 , ruleId : directive . ruleId }
55
- ] ;
56
-
57
- default :
58
- throw new TypeError ( `Unrecognized directive type '${ directive . type } '` ) ;
59
- }
60
- } ) . sort ( compareLocations ) ;
61
-
26
+ function applyDirectives ( options ) {
62
27
const problems = [ ] ;
63
28
let nextDirectiveIndex = 0 ;
64
29
let globalDisableActive = false ;
@@ -71,10 +36,10 @@ module.exports = options => {
71
36
72
37
for ( const problem of options . problems ) {
73
38
while (
74
- nextDirectiveIndex < processedDirectives . length &&
75
- compareLocations ( processedDirectives [ nextDirectiveIndex ] , problem ) <= 0
39
+ nextDirectiveIndex < options . directives . length &&
40
+ compareLocations ( options . directives [ nextDirectiveIndex ] , problem ) <= 0
76
41
) {
77
- const directive = processedDirectives [ nextDirectiveIndex ++ ] ;
42
+ const directive = options . directives [ nextDirectiveIndex ++ ] ;
78
43
79
44
switch ( directive . type ) {
80
45
case "disable" :
@@ -112,4 +77,55 @@ module.exports = options => {
112
77
}
113
78
114
79
return problems ;
80
+ }
81
+
82
+ /**
83
+ * Given a list of directive comments (i.e. metadata about eslint-disable and eslint-enable comments) and a list
84
+ * of reported problems, determines which problems should be reported.
85
+ * @param {Object } options Information about directives and problems
86
+ * @param {{
87
+ * type: ("disable"|"enable"|"disable-line"|"disable-next-line"),
88
+ * ruleId: (string|null),
89
+ * line: number,
90
+ * column: number
91
+ * }} options.directives Directive comments found in the file, with one-based columns.
92
+ * Two directive comments can only have the same location if they also have the same type (e.g. a single eslint-disable
93
+ * comment for two different rules is represented as two directives).
94
+ * @param {{ruleId: (string|null), line: number, column: number}[] } options.problems
95
+ * A list of problems reported by rules, sorted by increasing location in the file, with one-based columns.
96
+ * @returns {{ruleId: (string|null), line: number, column: number}[] }
97
+ * A list of reported problems that were not disabled by the directive comments.
98
+ */
99
+ module . exports = options => {
100
+ const blockDirectives = options . directives
101
+ . filter ( directive => directive . type === "disable" || directive . type === "enable" )
102
+ . sort ( compareLocations ) ;
103
+
104
+ const lineDirectives = lodash . flatMap ( options . directives , directive => {
105
+ switch ( directive . type ) {
106
+ case "disable" :
107
+ case "enable" :
108
+ return [ ] ;
109
+
110
+ case "disable-line" :
111
+ return [
112
+ { type : "disable" , line : directive . line , column : 1 , ruleId : directive . ruleId } ,
113
+ { type : "enable" , line : directive . line + 1 , column : 0 , ruleId : directive . ruleId }
114
+ ] ;
115
+
116
+ case "disable-next-line" :
117
+ return [
118
+ { type : "disable" , line : directive . line + 1 , column : 1 , ruleId : directive . ruleId } ,
119
+ { type : "enable" , line : directive . line + 2 , column : 0 , ruleId : directive . ruleId }
120
+ ] ;
121
+
122
+ default :
123
+ throw new TypeError ( `Unrecognized directive type '${ directive . type } '` ) ;
124
+ }
125
+ } ) . sort ( compareLocations ) ;
126
+
127
+ const problemsAfterBlockDirectives = applyDirectives ( { problems : options . problems , directives : blockDirectives } ) ;
128
+ const problemsAfterLineDirectives = applyDirectives ( { problems : problemsAfterBlockDirectives , directives : lineDirectives } ) ;
129
+
130
+ return problemsAfterLineDirectives . sort ( compareLocations ) ;
115
131
} ;
0 commit comments