-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathlinter.rs
409 lines (371 loc) · 14.3 KB
/
linter.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
use log::debug;
use std::{
fs,
path::{Path, PathBuf},
rc::Rc,
sync::Arc,
};
use oxc_allocator::Allocator;
use oxc_diagnostics::{Error, NamedSource, Severity};
use oxc_linter::{
partial_loader::{
AstroPartialLoader, JavaScriptSource, SveltePartialLoader, VuePartialLoader,
LINT_PARTIAL_LOADER_EXT,
},
LintContext, Linter,
};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::{SourceType, VALID_EXTENSIONS};
use ropey::Rope;
use tower_lsp::lsp_types::{
self, DiagnosticRelatedInformation, DiagnosticSeverity, Position, Range, Url,
};
#[derive(Debug)]
struct ErrorWithPosition {
pub start_pos: Position,
pub end_pos: Position,
pub miette_err: Error,
pub fixed_content: Option<FixedContent>,
pub labels_with_pos: Vec<LabeledSpanWithPosition>,
}
#[derive(Debug)]
struct LabeledSpanWithPosition {
pub start_pos: Position,
pub end_pos: Position,
pub message: Option<String>,
}
impl ErrorWithPosition {
pub fn new(
error: Error,
text: &str,
fixed_content: Option<FixedContent>,
start: usize,
) -> Self {
let labels = error.labels().map_or(vec![], Iterator::collect);
let labels_with_pos: Vec<LabeledSpanWithPosition> = labels
.iter()
.map(|labeled_span| LabeledSpanWithPosition {
start_pos: offset_to_position(labeled_span.offset() + start, text)
.unwrap_or_default(),
end_pos: offset_to_position(
labeled_span.offset() + start + labeled_span.len(),
text,
)
.unwrap_or_default(),
message: labeled_span.label().map(ToString::to_string),
})
.collect();
let start_pos = labels_with_pos[0].start_pos;
let end_pos = labels_with_pos[labels_with_pos.len() - 1].end_pos;
Self { miette_err: error, start_pos, end_pos, labels_with_pos, fixed_content }
}
fn to_lsp_diagnostic(&self, path: &PathBuf) -> lsp_types::Diagnostic {
let severity = match self.miette_err.severity() {
Some(Severity::Error) => Some(lsp_types::DiagnosticSeverity::ERROR),
_ => Some(lsp_types::DiagnosticSeverity::WARNING),
};
let related_information = Some(
self.labels_with_pos
.iter()
.map(|labeled_span| lsp_types::DiagnosticRelatedInformation {
location: lsp_types::Location {
uri: lsp_types::Url::from_file_path(path).unwrap(),
range: lsp_types::Range {
start: lsp_types::Position {
line: labeled_span.start_pos.line,
character: labeled_span.start_pos.character,
},
end: lsp_types::Position {
line: labeled_span.end_pos.line,
character: labeled_span.end_pos.character,
},
},
},
message: labeled_span.message.clone().unwrap_or_default(),
})
.collect(),
);
let range = related_information.as_ref().map_or(
Range { start: self.start_pos, end: self.end_pos },
|infos: &Vec<DiagnosticRelatedInformation>| {
let mut ret_range = Range {
start: Position { line: u32::MAX, character: u32::MAX },
end: Position { line: u32::MAX, character: u32::MAX },
};
for info in infos {
if cmp_range(&ret_range, &info.location.range) == std::cmp::Ordering::Greater {
ret_range = info.location.range;
}
}
ret_range
},
);
let message = self.miette_err.help().map_or_else(
|| self.miette_err.to_string(),
|help| format!("{}\nhelp: {}", self.miette_err, help),
);
lsp_types::Diagnostic {
range,
severity,
code: None,
message,
source: Some("oxc".into()),
code_description: None,
related_information,
tags: None,
data: None,
}
}
fn into_diagnostic_report(self, path: &PathBuf) -> DiagnosticReport {
DiagnosticReport {
diagnostic: self.to_lsp_diagnostic(path),
fixed_content: self.fixed_content,
}
}
}
#[derive(Debug, Clone)]
pub struct DiagnosticReport {
pub diagnostic: lsp_types::Diagnostic,
pub fixed_content: Option<FixedContent>,
}
#[derive(Debug)]
struct ErrorReport {
pub error: Error,
pub fixed_content: Option<FixedContent>,
}
#[derive(Debug, Clone)]
pub struct FixedContent {
pub code: String,
pub range: Range,
}
pub struct IsolatedLintHandler {
linter: Arc<Linter>,
}
impl IsolatedLintHandler {
pub fn new(linter: Arc<Linter>) -> Self {
Self { linter }
}
pub fn run_single(
&self,
path: &Path,
content: Option<String>,
) -> Option<Vec<DiagnosticReport>> {
if Self::is_wanted_ext(path) {
Some(Self::lint_path(&self.linter, path, content).map_or(vec![], |(p, errors)| {
let mut diagnostics: Vec<DiagnosticReport> =
errors.into_iter().map(|e| e.into_diagnostic_report(&p)).collect();
// a diagnostics connected from related_info to original diagnostic
let mut inverted_diagnostics = vec![];
for d in &diagnostics {
let Some(ref related_info) = d.diagnostic.related_information else {
continue;
};
let related_information = Some(vec![DiagnosticRelatedInformation {
location: lsp_types::Location {
uri: lsp_types::Url::from_file_path(path).unwrap(),
range: d.diagnostic.range,
},
message: "original diagnostic".to_string(),
}]);
for r in related_info {
if r.location.range == d.diagnostic.range {
continue;
}
inverted_diagnostics.push(DiagnosticReport {
diagnostic: lsp_types::Diagnostic {
range: r.location.range,
severity: Some(DiagnosticSeverity::HINT),
code: None,
message: r.message.clone(),
source: Some("oxc".into()),
code_description: None,
related_information: related_information.clone(),
tags: None,
data: None,
},
fixed_content: None,
});
}
}
diagnostics.append(&mut inverted_diagnostics);
diagnostics
}))
} else {
None
}
}
fn is_wanted_ext(path: &Path) -> bool {
let extensions = get_valid_extensions();
path.extension().map_or(false, |ext| extensions.contains(&ext.to_string_lossy().as_ref()))
}
fn get_source_type_and_text(
path: &Path,
source_text: Option<String>,
ext: &str,
) -> Option<(SourceType, String)> {
let source_type = SourceType::from_path(path);
let not_supported_yet =
source_type.as_ref().is_err_and(|_| !LINT_PARTIAL_LOADER_EXT.contains(&ext));
if not_supported_yet {
debug!("extension {ext} not supported yet.");
return None;
}
let source_type = source_type.unwrap_or_default();
let source_text = source_text.map_or_else(
|| fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read {path:?}")),
|source_text| source_text,
);
Some((source_type, source_text))
}
fn may_need_extract_js_content<'a>(
source_text: &'a str,
ext: &str,
) -> Option<Vec<JavaScriptSource<'a>>> {
match ext {
"vue" => Some(VuePartialLoader::new(source_text).parse()),
"astro" => Some(AstroPartialLoader::new(source_text).parse()),
"svelte" => Some(SveltePartialLoader::new(source_text).parse()),
_ => None,
}
}
fn lint_path(
linter: &Linter,
path: &Path,
source_text: Option<String>,
) -> Option<(PathBuf, Vec<ErrorWithPosition>)> {
let ext = path.extension().and_then(std::ffi::OsStr::to_str)?;
let (source_type, original_source_text) =
Self::get_source_type_and_text(path, source_text, ext)?;
let javascript_sources = Self::may_need_extract_js_content(&original_source_text, ext)
.unwrap_or_else(|| {
vec![JavaScriptSource { source_text: &original_source_text, source_type, start: 0 }]
});
debug!("lint {path:?}");
let mut diagnostics = vec![];
for source in javascript_sources {
let JavaScriptSource { source_text: javascript_source_text, source_type, start } =
source;
let allocator = Allocator::default();
let ret = Parser::new(&allocator, javascript_source_text, source_type)
.allow_return_outside_function(true)
.parse();
if !ret.errors.is_empty() {
let reports = ret
.errors
.into_iter()
.map(|diagnostic| ErrorReport {
error: Error::from(diagnostic),
fixed_content: None,
})
.collect();
return Some(Self::wrap_diagnostics(path, &original_source_text, reports, start));
};
let program = allocator.alloc(ret.program);
let semantic_ret = SemanticBuilder::new(javascript_source_text, source_type)
.with_trivias(ret.trivias)
.with_check_syntax_error(true)
.build(program);
if !semantic_ret.errors.is_empty() {
let reports = semantic_ret
.errors
.into_iter()
.map(|diagnostic| ErrorReport {
error: Error::from(diagnostic),
fixed_content: None,
})
.collect();
return Some(Self::wrap_diagnostics(path, &original_source_text, reports, start));
};
let lint_ctx = LintContext::new(
path.to_path_buf().into_boxed_path(),
Rc::new(semantic_ret.semantic),
);
let result = linter.run(lint_ctx);
let reports = result
.into_iter()
.map(|msg| {
let fixed_content = msg.fix.map(|f| FixedContent {
code: f.content.to_string(),
range: Range {
start: offset_to_position(
f.span.start as usize + start,
javascript_source_text,
)
.unwrap_or_default(),
end: offset_to_position(
f.span.end as usize + start,
javascript_source_text,
)
.unwrap_or_default(),
},
});
ErrorReport { error: Error::from(msg.error), fixed_content }
})
.collect::<Vec<ErrorReport>>();
let (_, errors_with_position) =
Self::wrap_diagnostics(path, &original_source_text, reports, start);
diagnostics.extend(errors_with_position);
}
Some((path.to_path_buf(), diagnostics))
}
fn wrap_diagnostics(
path: &Path,
source_text: &str,
reports: Vec<ErrorReport>,
start: usize,
) -> (PathBuf, Vec<ErrorWithPosition>) {
let source = Arc::new(NamedSource::new(path.to_string_lossy(), source_text.to_owned()));
let diagnostics = reports
.into_iter()
.map(|report| {
ErrorWithPosition::new(
report.error.with_source_code(Arc::clone(&source)),
source_text,
report.fixed_content,
start,
)
})
.collect();
(path.to_path_buf(), diagnostics)
}
}
fn get_valid_extensions() -> Vec<&'static str> {
VALID_EXTENSIONS
.iter()
.chain(LINT_PARTIAL_LOADER_EXT.iter())
.copied()
.collect::<Vec<&'static str>>()
}
#[allow(clippy::cast_possible_truncation)]
fn offset_to_position(offset: usize, source_text: &str) -> Option<Position> {
let rope = Rope::from_str(source_text);
let line = rope.try_byte_to_line(offset).ok()?;
let first_char_of_line = rope.try_line_to_char(line).ok()?;
// Original offset is byte, but Rope uses char offset
let offset = rope.try_byte_to_char(offset).ok()?;
let column = offset - first_char_of_line;
Some(Position::new(line as u32, column as u32))
}
pub struct ServerLinter {
linter: Arc<Linter>,
}
impl ServerLinter {
pub fn new() -> Self {
let linter = Linter::default().with_fix(true);
Self { linter: Arc::new(linter) }
}
pub fn new_with_linter(linter: Linter) -> Self {
Self { linter: Arc::new(linter) }
}
pub fn run_single(&self, uri: &Url, content: Option<String>) -> Option<Vec<DiagnosticReport>> {
IsolatedLintHandler::new(Arc::clone(&self.linter))
.run_single(&uri.to_file_path().unwrap(), content)
}
}
fn cmp_range(first: &Range, other: &Range) -> std::cmp::Ordering {
match first.start.cmp(&other.start) {
std::cmp::Ordering::Equal => first.end.cmp(&other.end),
o => o,
}
}