Skip to content

Commit 4077f39

Browse files
committed
refactor(linter): send Message for DiagnosticService
1 parent 3e43756 commit 4077f39

File tree

13 files changed

+154
-139
lines changed

13 files changed

+154
-139
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_diagnostics/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ doctest = false
2020

2121
[dependencies]
2222
miette = { workspace = true }
23+
24+
oxc_allocator = { workspace = true }
25+
oxc_span = { workspace = true }

crates/oxc_diagnostics/src/lib.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ pub type Result<T> = std::result::Result<T, OxcDiagnostic>;
6161

6262
use miette::{Diagnostic, SourceCode};
6363
pub use miette::{GraphicalReportHandler, GraphicalTheme, LabeledSpan, NamedSource, SourceSpan};
64+
use oxc_allocator::{Allocator, CloneIn};
65+
use oxc_span::{Span, SPAN};
6466

6567
/// Describes an error or warning that occurred.
6668
///
6769
/// Used by all oxc tools.
68-
#[derive(Debug, Clone, Eq, PartialEq)]
70+
#[derive(Debug, Clone, PartialEq)]
6971
#[must_use]
7072
pub struct OxcDiagnostic {
7173
// `Box` the data to make `OxcDiagnostic` 8 bytes so that `Result` is small.
@@ -110,7 +112,7 @@ impl Display for OxcCode {
110112
}
111113
}
112114

113-
#[derive(Debug, Clone, Eq, PartialEq)]
115+
#[derive(Debug, Clone, PartialEq)]
114116
pub struct OxcDiagnosticInner {
115117
pub message: Cow<'static, str>,
116118
pub labels: Option<Vec<LabeledSpan>>,
@@ -335,3 +337,65 @@ impl OxcDiagnostic {
335337
Error::from(self).with_source_code(code)
336338
}
337339
}
340+
341+
342+
343+
344+
/// A completed, normalized fix ready to be applied to the source code.
345+
///
346+
/// Used internally by this module. Lint rules should use [`RuleFix`].
347+
#[derive(Debug, Clone)]
348+
#[non_exhaustive]
349+
pub struct Fix<'a> {
350+
pub content: Cow<'a, str>,
351+
/// A brief suggestion message describing the fix. Will be shown in
352+
/// editors via code actions.
353+
pub message: Option<Cow<'a, str>>,
354+
pub span: Span,
355+
}
356+
357+
impl PartialEq for Fix<'_> {
358+
fn eq(&self, other: &Self) -> bool {
359+
self.span == other.span && self.content == other.content
360+
}
361+
}
362+
363+
impl<'new> CloneIn<'new> for Fix<'_> {
364+
type Cloned = Fix<'new>;
365+
366+
fn clone_in(&self, allocator: &'new Allocator) -> Self::Cloned {
367+
Fix {
368+
content: match &self.content {
369+
Cow::Borrowed(s) => Cow::Borrowed(allocator.alloc_str(s)),
370+
Cow::Owned(s) => Cow::Owned(s.clone()),
371+
},
372+
span: self.span,
373+
message: self.message.as_ref().map(|s| match s {
374+
Cow::Borrowed(s) => Cow::Borrowed(allocator.alloc_str(s)),
375+
Cow::Owned(s) => Cow::Owned(s.clone()),
376+
}),
377+
}
378+
}
379+
}
380+
381+
impl Default for Fix<'_> {
382+
fn default() -> Self {
383+
Self::empty()
384+
}
385+
}
386+
387+
impl<'a> Fix<'a> {
388+
pub const fn delete(span: Span) -> Self {
389+
Self { content: Cow::Borrowed(""), message: None, span }
390+
}
391+
392+
pub const fn new(content: Cow<'a, str>, span: Span) -> Self {
393+
Self { content, message: None, span }
394+
}
395+
396+
/// Creates a [`Fix`] that doesn't change the source code.
397+
#[inline]
398+
pub const fn empty() -> Self {
399+
Self { content: Cow::Borrowed(""), message: None, span: SPAN }
400+
}
401+
}

crates/oxc_linter/src/fixer/fix.rs

Lines changed: 11 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{borrow::Cow, ops::Deref};
22

33
use bitflags::bitflags;
4-
use oxc_allocator::{Allocator, CloneIn};
4+
use oxc_diagnostics::Fix;
55
use oxc_span::{GetSpan, SPAN, Span};
66

77
bitflags! {
@@ -268,58 +268,6 @@ impl<'a> Deref for RuleFix<'a> {
268268
}
269269
}
270270

271-
/// A completed, normalized fix ready to be applied to the source code.
272-
///
273-
/// Used internally by this module. Lint rules should use [`RuleFix`].
274-
#[derive(Debug, Clone)]
275-
#[non_exhaustive]
276-
pub struct Fix<'a> {
277-
pub content: Cow<'a, str>,
278-
/// A brief suggestion message describing the fix. Will be shown in
279-
/// editors via code actions.
280-
pub message: Option<Cow<'a, str>>,
281-
pub span: Span,
282-
}
283-
284-
impl<'new> CloneIn<'new> for Fix<'_> {
285-
type Cloned = Fix<'new>;
286-
287-
fn clone_in(&self, allocator: &'new Allocator) -> Self::Cloned {
288-
Fix {
289-
content: match &self.content {
290-
Cow::Borrowed(s) => Cow::Borrowed(allocator.alloc_str(s)),
291-
Cow::Owned(s) => Cow::Owned(s.clone()),
292-
},
293-
span: self.span,
294-
message: self.message.as_ref().map(|s| match s {
295-
Cow::Borrowed(s) => Cow::Borrowed(allocator.alloc_str(s)),
296-
Cow::Owned(s) => Cow::Owned(s.clone()),
297-
}),
298-
}
299-
}
300-
}
301-
302-
impl Default for Fix<'_> {
303-
fn default() -> Self {
304-
Self::empty()
305-
}
306-
}
307-
308-
impl<'a> Fix<'a> {
309-
pub const fn delete(span: Span) -> Self {
310-
Self { content: Cow::Borrowed(""), message: None, span }
311-
}
312-
313-
pub fn new<T: Into<Cow<'a, str>>>(content: T, span: Span) -> Self {
314-
Self { content: content.into(), message: None, span }
315-
}
316-
317-
/// Creates a [`Fix`] that doesn't change the source code.
318-
#[inline]
319-
pub const fn empty() -> Self {
320-
Self { content: Cow::Borrowed(""), message: None, span: SPAN }
321-
}
322-
}
323271

324272
// NOTE (@DonIsaac): having these variants is effectively the same as interning
325273
// single or 0-element Vecs. I experimented with using smallvec here, but the
@@ -546,20 +494,14 @@ impl<'a> CompositeFix<'a> {
546494

547495
output.push_str(after);
548496
output.shrink_to_fit();
549-
Fix::new(output, Span::new(start, end))
497+
Fix::new(output.into(), Span::new(start, end))
550498
}
551499
}
552500

553501
#[cfg(test)]
554502
mod test {
555503
use super::*;
556504

557-
impl PartialEq for Fix<'_> {
558-
fn eq(&self, other: &Self) -> bool {
559-
self.span == other.span && self.content == other.content
560-
}
561-
}
562-
563505
impl Clone for CompositeFix<'_> {
564506
fn clone(&self) -> Self {
565507
match self {
@@ -617,7 +559,7 @@ mod test {
617559

618560
#[test]
619561
fn test_composite_push_on_none() {
620-
let f: CompositeFix = Fix::new("foo", Span::empty(4)).into();
562+
let f: CompositeFix = Fix::new("foo".into(), Span::empty(4)).into();
621563

622564
let mut none = CompositeFix::None;
623565
none.push(CompositeFix::None);
@@ -635,9 +577,9 @@ mod test {
635577

636578
#[test]
637579
fn test_composite_push_on_single() {
638-
let f1 = Fix::new("foo", Span::empty(4));
639-
let f2 = Fix::new("bar", Span::empty(5));
640-
let f3 = Fix::new("baz", Span::empty(6));
580+
let f1 = Fix::new("foo".into(), Span::empty(4));
581+
let f2 = Fix::new("bar".into(), Span::empty(5));
582+
let f3 = Fix::new("baz".into(), Span::empty(6));
641583
let single = || CompositeFix::Single(f1.clone());
642584

643585
// None.push(single) == single
@@ -650,8 +592,8 @@ mod test {
650592
assert_eq!(
651593
f,
652594
CompositeFix::Multiple(vec![
653-
Fix::new("foo", Span::empty(4)),
654-
Fix::new("bar", Span::empty(5))
595+
Fix::new("foo".into(), Span::empty(4)),
596+
Fix::new("bar".into(), Span::empty(5))
655597
])
656598
);
657599

@@ -664,9 +606,9 @@ mod test {
664606

665607
#[test]
666608
fn test_composite_push_on_multiple() {
667-
let f1 = Fix::new("foo", Span::empty(4));
668-
let f2 = Fix::new("bar", Span::empty(5));
669-
let f3 = Fix::new("baz", Span::empty(6));
609+
let f1 = Fix::new("foo".into(), Span::empty(4));
610+
let f2 = Fix::new("bar".into(), Span::empty(5));
611+
let f3 = Fix::new("baz".into(), Span::empty(6));
670612
let multiple = || CompositeFix::Multiple(vec![f1.clone(), f2.clone()]);
671613

672614
// None.push(multiple) == multiple

0 commit comments

Comments
 (0)