Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(oxc_codegen): avoid print same pure comments multiple time #4230

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/oxc_codegen/src/annotation_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ pub fn get_leading_annotate_comment<const MINIFY: bool>(
}

pub fn print_comment<const MINIFY: bool>(comment: Comment, p: &mut Codegen<{ MINIFY }>) {
// ```js
// /*#__PURE__*/
// Object.getOwnPropertyNames(Symbol)
// // ios10.x Object.getOwnPropertyNames(Symbol) can enumerate 'arguments' and 'caller'
// // but accessing them on Symbol leads to TypeError because Symbol is a strict mode
// // function
// .filter(key => key !== 'arguments' && key !== 'caller')
// .map(key => (Symbol)[key])
// .filter(isSymbol),
// ```
// in this example, `Object.getOwnPropertyNames(Symbol)` and `Object.getOwnPropertyNames(Symbol).filter()`, `Object.getOwnPropertyNames(Symbol).filter().map()`
// share the same leading comment. since they both are call expr and has same span start, we need to avoid print the same comment multiple times.
if p.latest_consumed_comment_end >= comment.span.end {
return;
}
p.latest_consumed_comment_end = comment.span.end;
match comment.kind {
CommentKind::SingleLine => {
p.print_str("//");
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub struct Codegen<'a, const MINIFY: bool> {
/// the first element of value is the start of the comment
/// the second element of value includes the end of the comment and comment kind.
move_comment_map: MoveCommentMap,
latest_consumed_comment_end: u32,
}

impl<'a, const MINIFY: bool> Default for Codegen<'a, MINIFY> {
Expand Down Expand Up @@ -141,6 +142,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
quote: b'"',
sourcemap_builder: None,
move_comment_map: MoveCommentMap::default(),
latest_consumed_comment_end: 0,
}
}

Expand Down
12 changes: 12 additions & 0 deletions crates/oxc_codegen/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ const staticCacheMap = /*#__PURE__*/ new WeakMap()
",
"const staticCacheMap = /*#__PURE__*/ new WeakMap();\n",
);

test_comment_helper(
r"
const builtInSymbols = new Set(
/*#__PURE__*/
Object.getOwnPropertyNames(Symbol)
.filter(key => key !== 'arguments' && key !== 'caller')
)

",
"const builtInSymbols = new Set(/*#__PURE__*/ Object.getOwnPropertyNames(Symbol).filter((key) => key !== \"arguments\" && key !== \"caller\"));\n",
);
}

#[test]
Expand Down
Loading