-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathcontext.rs
184 lines (148 loc) · 4.92 KB
/
context.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
use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc};
use oxc_diagnostics::{OxcDiagnostic, Severity};
use oxc_semantic::{AstNodes, JSDocFinder, ScopeTree, Semantic, SymbolTable};
use oxc_span::{SourceType, Span};
use oxc_syntax::module_record::ModuleRecord;
use crate::{
disable_directives::{DisableDirectives, DisableDirectivesBuilder},
fixer::{Fix, Message, RuleFixer},
javascript_globals::GLOBALS,
AllowWarnDeny, OxlintConfig, OxlintEnv, OxlintGlobals, OxlintSettings,
};
#[derive(Clone)]
pub struct LintContext<'a> {
semantic: Rc<Semantic<'a>>,
diagnostics: RefCell<Vec<Message<'a>>>,
disable_directives: Rc<DisableDirectives<'a>>,
/// Whether or not to apply code fixes during linting.
fix: bool,
file_path: Rc<Path>,
eslint_config: Arc<OxlintConfig>,
// states
current_rule_name: &'static str,
severity: Severity,
}
impl<'a> LintContext<'a> {
pub fn new(file_path: Box<Path>, semantic: Rc<Semantic<'a>>) -> Self {
let disable_directives =
DisableDirectivesBuilder::new(semantic.source_text(), semantic.trivias().clone())
.build();
Self {
semantic,
diagnostics: RefCell::new(vec![]),
disable_directives: Rc::new(disable_directives),
fix: false,
file_path: file_path.into(),
eslint_config: Arc::new(OxlintConfig::default()),
current_rule_name: "",
severity: Severity::Warning,
}
}
#[must_use]
pub fn with_fix(mut self, fix: bool) -> Self {
self.fix = fix;
self
}
#[must_use]
pub fn with_eslint_config(mut self, eslint_config: &Arc<OxlintConfig>) -> Self {
self.eslint_config = Arc::clone(eslint_config);
self
}
#[must_use]
pub fn with_rule_name(mut self, name: &'static str) -> Self {
self.current_rule_name = name;
self
}
#[must_use]
pub fn with_severity(mut self, severity: AllowWarnDeny) -> Self {
self.severity = Severity::from(severity);
self
}
pub fn semantic(&self) -> &Rc<Semantic<'a>> {
&self.semantic
}
pub fn disable_directives(&self) -> &DisableDirectives<'a> {
&self.disable_directives
}
/// Source code of the file being linted.
pub fn source_text(&self) -> &'a str {
self.semantic().source_text()
}
/// Get a snippet of source text covered by the given [`Span`]. For details,
/// see [`Span::source_text`].
pub fn source_range(&self, span: Span) -> &'a str {
span.source_text(self.semantic().source_text())
}
pub fn source_type(&self) -> &SourceType {
self.semantic().source_type()
}
pub fn file_path(&self) -> &Path {
&self.file_path
}
pub fn settings(&self) -> &OxlintSettings {
&self.eslint_config.settings
}
pub fn globals(&self) -> &OxlintGlobals {
&self.eslint_config.globals
}
pub fn env(&self) -> &OxlintEnv {
&self.eslint_config.env
}
pub fn env_contains_var(&self, var: &str) -> bool {
for env in self.env().iter() {
let env = GLOBALS.get(env).unwrap_or(&GLOBALS["builtin"]);
if env.get(var).is_some() {
return true;
}
}
false
}
/* Diagnostics */
pub fn into_message(self) -> Vec<Message<'a>> {
self.diagnostics.borrow().iter().cloned().collect::<Vec<_>>()
}
fn add_diagnostic(&self, message: Message<'a>) {
if !self.disable_directives.contains(self.current_rule_name, message.start()) {
let mut message = message;
if message.error.severity != self.severity {
message.error = message.error.with_severity(self.severity);
}
self.diagnostics.borrow_mut().push(message);
}
}
/// Report a lint rule violation.
///
/// Use [`LintContext::diagnostic_with_fix`] to provide an automatic fix.
pub fn diagnostic(&self, diagnostic: OxcDiagnostic) {
self.add_diagnostic(Message::new(diagnostic, None));
}
/// Report a lint rule violation and provide an automatic fix.
pub fn diagnostic_with_fix<F: FnOnce(RuleFixer<'_, 'a>) -> Fix<'a>>(
&self,
diagnostic: OxcDiagnostic,
fix: F,
) {
if self.fix {
let fixer = RuleFixer::new(self);
self.add_diagnostic(Message::new(diagnostic, Some(fix(fixer))));
} else {
self.diagnostic(diagnostic);
}
}
pub fn nodes(&self) -> &AstNodes<'a> {
self.semantic().nodes()
}
pub fn scopes(&self) -> &ScopeTree {
self.semantic().scopes()
}
pub fn symbols(&self) -> &SymbolTable {
self.semantic().symbols()
}
pub fn module_record(&self) -> &ModuleRecord {
self.semantic().module_record()
}
/* JSDoc */
pub fn jsdoc(&self) -> &JSDocFinder<'a> {
self.semantic().jsdoc()
}
}