-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
lints.rs
95 lines (76 loc) · 2.62 KB
/
lints.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
#![allow(clippy::needless_update)]
use std::{fs, path::PathBuf};
use swc_common::{errors::HANDLER, input::SourceFileInput};
use swc_css_lints::{get_rules, LintConfig, LintParams, LintRule};
use swc_css_parser::{
lexer::Lexer,
parser::{Parser, ParserConfig},
};
#[testing::fixture("tests/rules/pass/**/input.css")]
fn pass(input: PathBuf) {
let config_path = input.parent().unwrap().join("config.json");
let lint_config =
serde_json::from_str::<LintConfig>(&fs::read_to_string(config_path).unwrap()).unwrap();
testing::run_test2(false, |cm, handler| -> Result<(), _> {
let config = ParserConfig {
..Default::default()
};
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
let mut parser = Parser::new(lexer, config);
let stylesheet = match parser.parse_all() {
Ok(stylesheet) => stylesheet,
Err(err) => {
err.to_diagnostics(&handler).emit();
panic!();
}
};
let mut rules = get_rules(&LintParams {
lint_config: &lint_config,
})
.unwrap();
HANDLER.set(&handler, || {
rules.lint_stylesheet(&stylesheet);
});
if handler.has_errors() {
return Err(());
}
Ok(())
})
.unwrap();
}
#[testing::fixture("tests/rules/fail/**/input.css")]
fn fail(input: PathBuf) {
let stderr_path = input.parent().unwrap().join("output.swc-stderr");
let config_path = input.parent().unwrap().join("config.json");
let lint_config =
serde_json::from_str::<LintConfig>(&fs::read_to_string(config_path).unwrap()).unwrap();
let stderr = testing::run_test2(false, |cm, handler| -> Result<(), _> {
let config = ParserConfig {
..Default::default()
};
let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
let mut parser = Parser::new(lexer, config);
let stylesheet = match parser.parse_all() {
Ok(stylesheet) => stylesheet,
Err(err) => {
err.to_diagnostics(&handler).emit();
panic!();
}
};
let mut rules = get_rules(&LintParams {
lint_config: &lint_config,
})
.unwrap();
HANDLER.set(&handler, || {
rules.lint_stylesheet(&stylesheet);
});
if !handler.has_errors() {
panic!("should error");
}
Err(())
})
.unwrap_err();
stderr.compare_to_file(&stderr_path).unwrap();
}