/
mod.rs
436 lines (386 loc) · 14.4 KB
/
mod.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use ignore::gitignore::Gitignore;
use std::{env, io::BufWriter, time::Instant, vec::Vec};
use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler};
use oxc_linter::{
partial_loader::LINT_PARTIAL_LOADER_EXT, LintOptions, LintService, LintServiceOptions, Linter,
};
use oxc_span::VALID_EXTENSIONS;
use crate::{
command::{LintOptions as CliLintOptions, OutputFormat, OutputOptions, WarningOptions},
walk::{Extensions, Walk},
CliRunResult, LintResult, Runner,
};
pub struct LintRunner {
options: CliLintOptions,
}
impl Runner for LintRunner {
type Options = CliLintOptions;
fn new(options: Self::Options) -> Self {
Self { options }
}
fn run(self) -> CliRunResult {
if self.options.list_rules {
let mut stdout = BufWriter::new(std::io::stdout());
Linter::print_rules(&mut stdout);
return CliRunResult::None;
}
let CliLintOptions {
paths,
filter,
warning_options,
ignore_options,
fix_options,
enable_plugins,
config,
tsconfig,
output_options,
..
} = self.options;
let mut paths = paths;
let provided_path_count = paths.len();
let now = Instant::now();
// The ignore crate whitelists explicit paths, but priority
// should be given to the ignore file. Many users lint
// automatically and pass a list of changed files explicitly.
// To accommodate this, unless `--no-ignore` is passed,
// pre-filter the paths.
if !paths.is_empty() && !ignore_options.no_ignore {
let (ignore, _err) = Gitignore::new(&ignore_options.ignore_path);
paths.retain(|p| if p.is_dir() { true } else { !ignore.matched(p, false).is_ignore() });
}
if paths.is_empty() {
// If explicit paths were provided, but all have been
// filtered, return early.
if provided_path_count > 0 {
return CliRunResult::LintResult(LintResult {
duration: now.elapsed(),
deny_warnings: warning_options.deny_warnings,
..LintResult::default()
});
}
if let Ok(cwd) = env::current_dir() {
paths.push(cwd);
} else {
return CliRunResult::InvalidOptions {
message: "Failed to get current working directory.".to_string(),
};
}
}
let extensions = VALID_EXTENSIONS
.iter()
.chain(LINT_PARTIAL_LOADER_EXT.iter())
.copied()
.collect::<Vec<&'static str>>();
let paths =
Walk::new(&paths, &ignore_options).with_extensions(Extensions(extensions)).paths();
let number_of_files = paths.len();
let cwd = std::env::current_dir().unwrap().into_boxed_path();
let lint_options = LintOptions::default()
.with_filter(filter)
.with_config_path(config)
.with_fix(fix_options.fix)
.with_import_plugin(enable_plugins.import_plugin)
.with_jest_plugin(enable_plugins.jest_plugin)
.with_jsx_a11y_plugin(enable_plugins.jsx_a11y_plugin)
.with_nextjs_plugin(enable_plugins.nextjs_plugin)
.with_react_perf_plugin(enable_plugins.react_perf_plugin);
let linter = match Linter::from_options(lint_options) {
Ok(lint_service) => lint_service,
Err(diagnostic) => {
let handler = GraphicalReportHandler::new();
let mut err = String::new();
handler.render_report(&mut err, diagnostic.as_ref()).unwrap();
eprintln!("{err}");
return CliRunResult::InvalidOptions {
message: "Failed to parse configuration file.".to_string(),
};
}
};
if let Some(path) = tsconfig.as_ref() {
if !path.is_file() {
let path = if path.is_relative() { cwd.join(path) } else { path.clone() };
return CliRunResult::InvalidOptions {
message: format!("The tsconfig file {path:?} does not exist, Please provide a valid tsconfig file.", ),
};
}
}
let options = LintServiceOptions { cwd, paths, tsconfig };
let lint_service = LintService::new(linter, options);
let mut diagnostic_service =
Self::get_diagnostic_service(&warning_options, &output_options);
// Spawn linting in another thread so diagnostics can be printed immediately from diagnostic_service.run.
rayon::spawn({
let tx_error = diagnostic_service.sender().clone();
let lint_service = lint_service.clone();
move || {
lint_service.run(&tx_error);
}
});
diagnostic_service.run();
CliRunResult::LintResult(LintResult {
duration: now.elapsed(),
number_of_rules: lint_service.linter().number_of_rules(),
number_of_files,
number_of_warnings: diagnostic_service.warnings_count(),
number_of_errors: diagnostic_service.errors_count(),
max_warnings_exceeded: diagnostic_service.max_warnings_exceeded(),
deny_warnings: warning_options.deny_warnings,
})
}
}
impl LintRunner {
fn get_diagnostic_service(
warning_options: &WarningOptions,
output_options: &OutputOptions,
) -> DiagnosticService {
let mut diagnostic_service = DiagnosticService::default()
.with_quiet(warning_options.quiet)
.with_max_warnings(warning_options.max_warnings);
match output_options.format {
OutputFormat::Default => {}
OutputFormat::Json => diagnostic_service.set_json_reporter(),
}
diagnostic_service
}
}
#[cfg(all(test, not(target_os = "windows")))]
mod test {
use super::LintRunner;
use crate::{lint_command, CliRunResult, LintResult, Runner};
fn test(args: &[&str]) -> LintResult {
let mut new_args = vec!["--quiet"];
new_args.extend(args);
let options = lint_command().run_inner(new_args.as_slice()).unwrap().lint_options;
match LintRunner::new(options).run() {
CliRunResult::LintResult(lint_result) => lint_result,
other => panic!("{other:?}"),
}
}
fn test_invalid_options(args: &[&str]) -> String {
let mut new_args = vec!["--quiet"];
new_args.extend(args);
let options = lint_command().run_inner(new_args.as_slice()).unwrap().lint_options;
match LintRunner::new(options).run() {
CliRunResult::InvalidOptions { message } => message,
other => {
panic!("Expected InvalidOptions, got {other:?}");
}
}
}
#[test]
fn no_arg() {
let args = &[];
let result = test(args);
assert!(result.number_of_rules > 0);
assert!(result.number_of_warnings > 0);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn dir() {
let args = &["fixtures/linter"];
let result = test(args);
assert!(result.number_of_rules > 0);
assert_eq!(result.number_of_files, 2);
assert_eq!(result.number_of_warnings, 2);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn file() {
let args = &["fixtures/linter/debugger.js"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn multi_files() {
let args = &["fixtures/linter/debugger.js", "fixtures/linter/nan.js"];
let result = test(args);
assert_eq!(result.number_of_files, 2);
assert_eq!(result.number_of_warnings, 2);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn wrong_extension() {
let args = &["foo.asdf"];
let result = test(args);
assert_eq!(result.number_of_files, 0);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn ignore_pattern() {
let args =
&["--ignore-pattern", "**/*.js", "--ignore-pattern", "**/*.vue", "fixtures/linter"];
let result = test(args);
assert_eq!(result.number_of_files, 0);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
/// When a file is explicitly passed as a path and `--no-ignore`
/// is not present, the ignore file should take precedence.
/// See https://github.com/oxc-project/oxc/issues/1124
#[test]
fn ignore_file_overrides_explicit_args() {
let args = &["--ignore-path", "fixtures/linter/.customignore", "fixtures/linter/nan.js"];
let result = test(args);
assert_eq!(result.number_of_files, 0);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn ignore_file_no_ignore() {
let args = &[
"--ignore-path",
"fixtures/linter/.customignore",
"--no-ignore",
"fixtures/linter/nan.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn filter_allow_all() {
let args = &["-A", "all", "fixtures/linter"];
let result = test(args);
assert!(result.number_of_files > 0);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn filter_allow_one() {
let args = &["-D", "correctness", "-A", "no-debugger", "fixtures/linter/debugger.js"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn eslintrc_off() {
let args = &["-c", "fixtures/eslintrc_off/eslintrc.json", "fixtures/eslintrc_off/test.js"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1); // triggered by no_empty_file
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn eslintrc_no_undef() {
let args = &["-c", "fixtures/no_undef/eslintrc.json", "fixtures/no_undef/test.js"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn eslintrc_no_env() {
let args =
&["-c", "fixtures/eslintrc_env/eslintrc_no_env.json", "fixtures/eslintrc_env/test.js"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn eslintrc_with_env() {
let args = &[
"-c",
"fixtures/eslintrc_env/eslintrc_env_browser.json",
"fixtures/eslintrc_env/test.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn no_empty_allow_empty_catch() {
let args = &[
"-c",
"fixtures/no_empty_allow_empty_catch/eslintrc.json",
"-D",
"no-empty",
"fixtures/no_empty_allow_empty_catch/test.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn no_empty_disallow_empty_catch() {
let args = &[
"-c",
"fixtures/no_empty_disallow_empty_catch/eslintrc.json",
"-D",
"no-empty",
"fixtures/no_empty_disallow_empty_catch/test.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn no_console_off() {
let args =
&["-c", "fixtures/no_console_off/eslintrc.json", "fixtures/no_console_off/test.js"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn typescript_eslint() {
let args = &[
"-c",
"fixtures/typescript_eslint/eslintrc.json",
"fixtures/typescript_eslint/test.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn lint_vue_file() {
let args = &["fixtures/vue/debugger.vue"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 2);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn lint_empty_vue_file() {
let args = &["fixtures/vue/empty.vue"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn lint_astro_file() {
let args = &["fixtures/astro/debugger.astro"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 4);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn lint_svelte_file() {
let args = &["fixtures/svelte/debugger.svelte"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);
}
#[test]
fn test_tsconfig_option() {
// passed
test(&["--tsconfig", "fixtures/tsconfig/tsconfig.json"]);
// failed
assert!(test_invalid_options(&["--tsconfig", "oxc/tsconfig.json"])
.contains("oxc/tsconfig.json\" does not exist, Please provide a valid tsconfig file."));
}
}