forked from oxc-project/oxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.rs
161 lines (127 loc) · 3.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
use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc};
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_diagnostics::Error;
use oxc_semantic::{AstNodes, JSDocFinder, ScopeTree, Semantic, SymbolTable};
use oxc_span::SourceType;
use crate::{
disable_directives::{DisableDirectives, DisableDirectivesBuilder},
fixer::{Fix, Message},
javascript_globals::GLOBALS,
ESLintEnv, ESLintSettings,
};
pub struct LintContext<'a> {
semantic: Rc<Semantic<'a>>,
diagnostics: RefCell<Vec<Message<'a>>>,
disable_directives: DisableDirectives<'a>,
/// Whether or not to apply code fixes during linting.
fix: bool,
current_rule_name: &'static str,
file_path: Box<Path>,
settings: Arc<ESLintSettings>,
env: Arc<ESLintEnv>,
}
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()).build();
Self {
semantic: Rc::clone(semantic),
diagnostics: RefCell::new(vec![]),
disable_directives,
fix: false,
current_rule_name: "",
file_path,
settings: Arc::new(ESLintSettings::default()),
env: Arc::new(ESLintEnv::default()),
}
}
#[must_use]
pub fn with_fix(mut self, fix: bool) -> Self {
self.fix = fix;
self
}
#[must_use]
pub fn with_settings(mut self, settings: &Arc<ESLintSettings>) -> Self {
self.settings = Arc::clone(settings);
self
}
#[must_use]
pub fn with_env(mut self, env: &Arc<ESLintEnv>) -> Self {
self.env = Arc::clone(env);
self
}
pub fn semantic(&self) -> &Rc<Semantic<'a>> {
&self.semantic
}
pub fn disable_directives(&self) -> &DisableDirectives<'a> {
&self.disable_directives
}
pub fn settings(&self) -> &ESLintSettings {
&self.settings
}
pub fn source_text(&self) -> &'a str {
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 envs(&self) -> &ESLintEnv {
&self.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
}
#[inline]
pub fn with_rule_name(&mut self, name: &'static str) {
self.current_rule_name = name;
}
/* Diagnostics */
pub fn into_message(self) -> Vec<Message<'a>> {
self.diagnostics.into_inner()
}
fn add_diagnostic(&self, message: Message<'a>) {
if !self.disable_directives.contains(self.current_rule_name, message.start()) {
self.diagnostics.borrow_mut().push(message);
}
}
pub fn diagnostic<T: Into<Error>>(&self, diagnostic: T) {
self.add_diagnostic(Message::new(diagnostic.into(), None));
}
pub fn diagnostic_with_fix<T, F>(&self, diagnostic: T, fix: F)
where
T: Into<Error>,
F: FnOnce() -> Fix<'a>,
{
if self.fix {
self.add_diagnostic(Message::new(diagnostic.into(), Some(fix())));
} 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()
}
#[allow(clippy::unused_self)]
pub fn codegen(&self) -> Codegen<false> {
Codegen::<false>::new("", "", CodegenOptions::default())
}
/* JSDoc */
pub fn jsdoc(&self) -> &JSDocFinder<'a> {
self.semantic().jsdoc()
}
}