-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathdisable_directives.rs
391 lines (348 loc) · 13.3 KB
/
disable_directives.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use oxc_ast::Trivias;
use oxc_span::Span;
use rust_lapper::{Interval, Lapper};
use rustc_hash::FxHashMap;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum DisabledRule<'a> {
All,
Single(&'a str),
}
/// A comment which disables one or more specific rules
pub struct DisableRuleComment<'a> {
/// Span of the comment
pub span: Span,
/// Rules disabled by the comment
pub rules: Vec<&'a str>,
}
pub struct DisableDirectives<'a> {
/// All the disabled rules with their corresponding covering spans
intervals: Lapper<u32, DisabledRule<'a>>,
/// Spans of comments that disable all rules
disable_all_comments: Vec<Span>,
/// All comments that disable one or more specific rules
disable_rule_comments: Vec<DisableRuleComment<'a>>,
}
impl<'a> DisableDirectives<'a> {
pub fn contains(&self, rule_name: &'static str, start: u32) -> bool {
self.intervals.find(start, start + 1).any(|interval| {
interval.val == DisabledRule::All
// Our rule name currently does not contain the prefix.
// For example, this will match `@typescript-eslint/no-var-requires` given
// our rule_name is `no-var-requires`.
|| matches!(interval.val, DisabledRule::Single(name) if name.contains(rule_name))
})
}
pub fn disable_all_comments(&self) -> &Vec<Span> {
&self.disable_all_comments
}
pub fn disable_rule_comments(&self) -> &Vec<DisableRuleComment<'a>> {
&self.disable_rule_comments
}
}
pub struct DisableDirectivesBuilder<'a> {
source_text: &'a str,
trivias: Trivias,
/// All the disabled rules with their corresponding covering spans
intervals: Lapper<u32, DisabledRule<'a>>,
/// Start of `eslint-disable` or `oxlint-disable`
disable_all_start: Option<u32>,
/// Start of `eslint-disable` or `oxlint-disable` rule_name`
disable_start_map: FxHashMap<&'a str, u32>,
/// Spans of comments that disable all rules
disable_all_comments: Vec<Span>,
/// All comments that disable one or more specific rules
disable_rule_comments: Vec<DisableRuleComment<'a>>,
}
impl<'a> DisableDirectivesBuilder<'a> {
pub fn new(source_text: &'a str, trivias: Trivias) -> Self {
Self {
source_text,
trivias,
intervals: Lapper::new(vec![]),
disable_all_start: None,
disable_start_map: FxHashMap::default(),
disable_all_comments: vec![],
disable_rule_comments: vec![],
}
}
pub fn build(mut self) -> DisableDirectives<'a> {
self.build_impl();
DisableDirectives {
intervals: self.intervals,
disable_all_comments: self.disable_all_comments,
disable_rule_comments: self.disable_rule_comments,
}
}
fn add_interval(&mut self, start: u32, stop: u32, val: DisabledRule<'a>) {
self.intervals.insert(Interval { start, stop, val });
}
#[allow(clippy::cast_possible_truncation)] // for `as u32`
fn build_impl(&mut self) {
let source_len = self.source_text.len() as u32;
// This algorithm iterates through the comments and builds all intervals
// for matching disable and enable pairs.
// Wrongly ordered matching pairs are not taken into consideration.
for (_, span) in self.trivias.clone().comments() {
let text = span.source_text(self.source_text);
let text = text.trim_start();
if let Some(text) =
text.strip_prefix("eslint-disable").or_else(|| text.strip_prefix("oxlint-disable"))
{
// `eslint-disable`
if text.trim().is_empty() {
if self.disable_all_start.is_none() {
self.disable_all_start = Some(span.end);
}
self.disable_all_comments.push(span);
continue;
}
// `eslint-disable-next-line`
if let Some(text) = text.strip_prefix("-next-line") {
// Get the span up to the next new line
let stop = self.source_text[span.end as usize..]
.lines()
.take(2)
.fold(span.end, |acc, line| acc + line.len() as u32);
if text.trim().is_empty() {
self.add_interval(span.end, stop, DisabledRule::All);
self.disable_all_comments.push(span);
} else {
// `eslint-disable-next-line rule_name1, rule_name2`
let mut rules = vec![];
Self::get_rule_names(text, |rule_name| {
self.add_interval(span.end, stop, DisabledRule::Single(rule_name));
rules.push(rule_name);
});
self.disable_rule_comments.push(DisableRuleComment { span, rules });
}
continue;
}
// `eslint-disable-line`
if let Some(text) = text.strip_prefix("-line") {
// Get the span between the preceding newline to this comment
let start = self.source_text[..=span.start as usize]
.lines()
.next_back()
.map_or(0, |line| span.start - (line.len() as u32 - 1));
let stop = span.start;
// `eslint-disable-line`
if text.trim().is_empty() {
self.add_interval(start, stop, DisabledRule::All);
self.disable_all_comments.push(span);
} else {
// `eslint-disable-line rule-name1, rule-name2`
let mut rules = vec![];
Self::get_rule_names(text, |rule_name| {
self.add_interval(start, stop, DisabledRule::Single(rule_name));
rules.push(rule_name);
});
self.disable_rule_comments.push(DisableRuleComment { span, rules });
}
continue;
}
// `eslint-disable rule-name1, rule-name2`
let mut rules = vec![];
Self::get_rule_names(text, |rule_name| {
self.disable_start_map.entry(rule_name).or_insert(span.end);
rules.push(rule_name);
});
self.disable_rule_comments.push(DisableRuleComment { span, rules });
continue;
}
if let Some(text) =
text.strip_prefix("eslint-enable").or_else(|| text.strip_prefix("oxlint-enable"))
{
// `eslint-enable`
if text.trim().is_empty() {
if let Some(start) = self.disable_all_start.take() {
self.add_interval(start, span.start, DisabledRule::All);
}
} else {
// `eslint-enable rule-name1, rule-name2`
Self::get_rule_names(text, |rule_name| {
if let Some(start) = self.disable_start_map.remove(rule_name) {
self.add_interval(start, span.start, DisabledRule::Single(rule_name));
}
});
}
continue;
}
}
// Lone `eslint-disable`
if let Some(start) = self.disable_all_start {
self.add_interval(start, source_len, DisabledRule::All);
}
// Lone `eslint-disable rule_name`
let disable_start_map = self.disable_start_map.drain().collect::<Vec<_>>();
for (rule_name, start) in disable_start_map {
self.add_interval(start, source_len, DisabledRule::Single(rule_name));
}
}
fn get_rule_names<F: FnMut(&'a str)>(text: &'a str, cb: F) {
if let Some(text) = text.split_terminator("--").next() {
text.split(',').map(str::trim).for_each(cb);
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
for prefix in ["eslint", "oxlint"] {
// [Disabling Rules](https://eslint.org/docs/latest/use/configure/rules#disabling-rules)
// Using configuration comments
let pass = vec![
// To disable rule warnings in a part of a file, use block comments in the following format:
format!(
"
/* {prefix}-disable */
debugger;
/* {prefix}-enable */
"
),
// You can also disable or enable warnings for specific rules:
format!(
"
/* {prefix}-disable no-debugger, no-console */
debugger;
/* {prefix}-enable no-debugger, no-console */
"
),
// To disable rule warnings in an entire file, put a /* eslint-disable */ block comment at the top of the file:
format!(
"
/* {prefix}-disable */
debugger;
"
),
// You can also disable or enable specific rules for an entire file:
format!(
"
/* {prefix}-disable no-debugger */
debugger;
"
),
// To ensure that a rule is never applied (regardless of any future enable/disable lines):
// This is not supported.
// "
// /* eslint no-debugger: \"off\" */
// debugger;
// "),
// To disable all rules on a specific line, use a line or block comment in one of the following formats:
format!(
"debugger; // {prefix}-disable-line
debugger; // {prefix}-disable-line
// {prefix}-disable-next-line
debugger;
/* {prefix}-disable-next-line */
debugger;
debugger; /* {prefix}-disable-line */
"
),
// To disable a specific rule on a specific line:
format!(
"
debugger; // {prefix}-disable-line no-debugger
// {prefix}-disable-next-line no-debugger
debugger;
debugger; /* {prefix}-disable-line no-debugger */
/* {prefix}-disable-next-line no-debugger */
debugger;
"
),
// To disable multiple rules on a specific line:
format!(
"
debugger; // {prefix}-disable-line no-debugger, quotes, semi
// {prefix}-disable-next-line no-debugger, quotes, semi
debugger;
debugger; /* {prefix}-disable-line no-debugger, quotes, semi */
/* {prefix}-disable-next-line no-debugger, quotes, semi */
debugger;
/* {prefix}-disable-next-line
no-debugger,
quotes,
semi
*/
debugger;
"
),
// To disable all rules twice:
format!(
"
/* {prefix}-disable */
debugger;
/* {prefix}-disable */
debugger;
"
),
// To disable a rule twice:
format!(
"
/* {prefix}-disable no-debugger */
debugger;
/* {prefix}-disable no-debugger */
debugger;
"
),
// Comment descriptions
format!(
"
// {prefix}-disable-next-line no-debugger -- Here's a description about why this configuration is necessary.
debugger;
/* {prefix}-disable-next-line no-debugger --
* Here's a very long description about why this configuration is necessary
* along with some additional information
**/
debugger;
"
),
];
let fail = vec![
"debugger".to_string(),
format!(
"
debugger; // {prefix}-disable-line no-alert
// {prefix}-disable-next-line no-alert
debugger;
debugger; /* {prefix}-disable-line no-alert */
/* {prefix}-disable-next-line no-alert */
debugger;
"
),
format!(
"
debugger; // {prefix}-disable-line no-alert, quotes, semi
// {prefix}-disable-next-line no-alert, quotes, semi
debugger;
debugger; /* {prefix}-disable-line no-alert, quotes, semi */
/* {prefix}-disable-next-line no-alert, quotes, semi */
debugger;
/* {prefix}-disable-next-line
no-alert,
quotes,
semi
*/
debugger;
"
),
format!(
"
/* {prefix}-disable-next-line no-debugger --
* Here's a very long description about why this configuration is necessary
* along with some additional information
**/
debugger;
debugger;
"
),
format!(
"
// {prefix}-disable-next-line no-debugger
debugger;
debugger;
"
),
];
Tester::new("no-debugger", pass, fail).test();
}
}