diff --git a/crates/oxc_ast/src/trivia.rs b/crates/oxc_ast/src/trivia.rs index 26620c42a..92fd695ba 100644 --- a/crates/oxc_ast/src/trivia.rs +++ b/crates/oxc_ast/src/trivia.rs @@ -1,11 +1,14 @@ //! Trivias such as comments +use oxc_span::Span; use std::collections::BTreeMap; -use oxc_span::Span; - /// A vec of trivias from the lexer, tupled by (span.start, span.end). -pub type Trivias = Vec<(u32, u32, CommentKind)>; +#[derive(Debug, Default)] +pub struct Trivias { + pub comments: Vec<(u32, u32, CommentKind)>, + pub irregular_whitespaces: Vec, +} /// Trivias such as comments /// @@ -15,11 +18,15 @@ pub type Trivias = Vec<(u32, u32, CommentKind)>; pub struct TriviasMap { /// Keyed by span.start comments: BTreeMap, + irregular_whitespaces: Vec, } impl From for TriviasMap { fn from(trivias: Trivias) -> Self { - Self { comments: trivias.iter().map(|t| (t.0, Comment::new(t.1, t.2))).collect() } + Self { + comments: trivias.comments.iter().map(|t| (t.0, Comment::new(t.1, t.2))).collect(), + irregular_whitespaces: trivias.irregular_whitespaces, + } } } @@ -91,4 +98,8 @@ impl TriviasMap { pub fn comments_spans(&self) -> impl Iterator + '_ { self.comments().iter().map(|(start, comment)| (*comment, Span::new(*start, comment.end))) } + + pub fn irregular_whitespaces(&self) -> &Vec { + &self.irregular_whitespaces + } } diff --git a/crates/oxc_parser/src/lexer/mod.rs b/crates/oxc_parser/src/lexer/mod.rs index 46de999bd..ac74bfb38 100644 --- a/crates/oxc_parser/src/lexer/mod.rs +++ b/crates/oxc_parser/src/lexer/mod.rs @@ -356,6 +356,8 @@ impl<'a> Lexer<'a> { Kind::Ident } c if is_irregular_whitespace(c) => { + self.trivia_builder + .add_irregular_whitespace(self.current.token.start, self.offset()); self.consume_char(); Kind::WhiteSpace } @@ -389,6 +391,11 @@ impl<'a> Lexer<'a> { Kind::Comment } + /// Section 12.1 Irregular White Space + fn skip_irregular_whitespace(&mut self) -> Kind { + Kind::WhiteSpace + } + /// Section 12.4 Multi Line Comment fn skip_multi_line_comment(&mut self) -> Kind { while let Some(c) = self.current.chars.next() { @@ -1319,6 +1326,7 @@ const ERR: ByteHandler = |lexer| { // const SPS: ByteHandler = |lexer| { + lexer.skip_irregular_whitespace(); lexer.consume_char(); Kind::WhiteSpace }; diff --git a/crates/oxc_parser/src/lexer/trivia_builder.rs b/crates/oxc_parser/src/lexer/trivia_builder.rs index 1cc84602b..d13a607c2 100644 --- a/crates/oxc_parser/src/lexer/trivia_builder.rs +++ b/crates/oxc_parser/src/lexer/trivia_builder.rs @@ -1,4 +1,5 @@ use oxc_ast::{CommentKind, Trivias}; +use oxc_span::Span; #[derive(Debug, Default)] pub struct TriviaBuilder { @@ -12,11 +13,15 @@ impl TriviaBuilder { /// skip leading `//` pub fn add_single_line_comment(&mut self, start: u32, end: u32) { - self.trivias.push((start + 2, end, CommentKind::SingleLine)); + self.trivias.comments.push((start + 2, end, CommentKind::SingleLine)); } /// skip leading `/*` and trailing `*/` pub fn add_multi_line_comment(&mut self, start: u32, end: u32) { - self.trivias.push((start + 2, end - 2, CommentKind::MultiLine)); + self.trivias.comments.push((start + 2, end - 2, CommentKind::MultiLine)); + } + + pub fn add_irregular_whitespace(&mut self, start: u32, end: u32) { + self.trivias.irregular_whitespaces.push(Span::new(start, end)); } } diff --git a/crates/oxc_prettier/src/lib.rs b/crates/oxc_prettier/src/lib.rs index 2bdfc4c66..1198c6f50 100644 --- a/crates/oxc_prettier/src/lib.rs +++ b/crates/oxc_prettier/src/lib.rs @@ -80,7 +80,7 @@ impl<'a> Prettier<'a> { allocator, source_text, options, - trivias: trivias.into_iter().peekable(), + trivias: trivias.comments.into_iter().peekable(), nodes: vec![], group_id_builder: GroupIdBuilder::default(), args: PrettierArgs::default(),