diff --git a/crates/oxc_codegen/src/annotation_comment.rs b/crates/oxc_codegen/src/annotation_comment.rs index ede9470f53a3f..e417c640b52fa 100644 --- a/crates/oxc_codegen/src/annotation_comment.rs +++ b/crates/oxc_codegen/src/annotation_comment.rs @@ -34,6 +34,22 @@ pub fn get_leading_annotate_comment( } pub fn print_comment(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("//"); diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index 509046add1ad1..96254e8952093 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -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> { @@ -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, } } diff --git a/crates/oxc_codegen/tests/mod.rs b/crates/oxc_codegen/tests/mod.rs index 1eddff5081632..b949eadcdb792 100644 --- a/crates/oxc_codegen/tests/mod.rs +++ b/crates/oxc_codegen/tests/mod.rs @@ -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]