-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(parser): remove all duplicated comments in trivia builder (#2689)
- Loading branch information
Showing
10 changed files
with
42 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,38 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use oxc_ast::{CommentKind, Trivias}; | ||
use oxc_span::Span; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct TriviaBuilder { | ||
trivias: Trivias, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct TriviasCheckpoint { | ||
comments_len: usize, | ||
irregular_whitespaces_len: usize, | ||
// Duplicated comments can be added from rewind, use `BTreeMap` to ensure uniqueness | ||
comments: BTreeMap<u32, (u32, CommentKind)>, | ||
irregular_whitespaces: Vec<Span>, | ||
} | ||
|
||
impl TriviaBuilder { | ||
pub fn build(self) -> Trivias { | ||
self.trivias | ||
} | ||
|
||
pub fn checkpoint(&self) -> TriviasCheckpoint { | ||
TriviasCheckpoint { | ||
comments_len: self.trivias.comments.len(), | ||
irregular_whitespaces_len: self.trivias.irregular_whitespaces.len(), | ||
Trivias { | ||
comments: self | ||
.comments | ||
.into_iter() | ||
.map(|(start, (end, kind))| (start, end, kind)) | ||
.collect(), | ||
irregular_whitespaces: self.irregular_whitespaces, | ||
} | ||
} | ||
|
||
pub fn rewind(&mut self, checkpoint: TriviasCheckpoint) { | ||
self.trivias.comments.truncate(checkpoint.comments_len); | ||
self.trivias.irregular_whitespaces.truncate(checkpoint.irregular_whitespaces_len); | ||
} | ||
|
||
/// skip leading `//` | ||
pub fn add_single_line_comment(&mut self, start: u32, end: u32) { | ||
self.trivias.comments.push((start + 2, end, CommentKind::SingleLine)); | ||
// skip leading `//` | ||
self.comments.insert(start + 2, (end, CommentKind::SingleLine)); | ||
} | ||
|
||
/// skip leading `/*` and trailing `*/` | ||
pub fn add_multi_line_comment(&mut self, start: u32, end: u32) { | ||
self.trivias.comments.push((start + 2, end - 2, CommentKind::MultiLine)); | ||
// skip leading `/*` and trailing `*/` | ||
self.comments.insert(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)); | ||
self.irregular_whitespaces.push(Span::new(start, end)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
codegen_misc Summary: | ||
AST Parsed : 12/12 (100.00%) | ||
Positive Passed: 12/12 (100.00%) | ||
AST Parsed : 14/14 (100.00%) | ||
Positive Passed: 14/14 (100.00%) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Foo = ( | ||
// comment | ||
"bar" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const foo = <div | ||
// comment | ||
/>; | ||
|
||
<div- | ||
// comment | ||
/>; | ||
|
||
<div-- /* comment */ />; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
prettier_misc Summary: | ||
AST Parsed : 12/12 (100.00%) | ||
Positive Passed: 7/12 (58.33%) | ||
AST Parsed : 14/14 (100.00%) | ||
Positive Passed: 8/14 (57.14%) | ||
Expect to Parse: "pass/oxc-1740.tsx" | ||
Expect to Parse: "pass/oxc-2087.ts" | ||
Expect to Parse: "pass/oxc-2394.ts" | ||
Expect to Parse: "pass/oxc-2674.tsx" | ||
Expect to Parse: "pass/swc-1627.js" | ||
Expect to Parse: "pass/swc-8243.tsx" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters