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

## Before 
```bash
Original:
const builtInSymbols = new Set(
  /*#__PURE__*/
  Object.getOwnPropertyNames(Symbol)
    .filter(key => key !== 'arguments' && key !== 'caller')
)


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

Minified:
const builtInSymbols=new Set(Object.getOwnPropertyNames(Symbol).filter((key)=>key!=="arguments"&&key!=="caller"))

```

## After
```bash
Original:
const builtInSymbols = new Set(
  /*#__PURE__*/
  Object.getOwnPropertyNames(Symbol)
    .filter(key => key !== 'arguments' && key !== 'caller')
)


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

Minified:
const builtInSymbols=new Set(Object.getOwnPropertyNames(Symbol).filter((key)=>key!=="arguments"&&key!=="caller"))

```
This commit is contained in:
IWANABETHATGUY 2024-07-12 23:07:43 +08:00 committed by GitHub
parent 81ed5885a8
commit 66b455a2b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 0 deletions

View file

@ -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("//");

View file

@ -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,
}
}

View file

@ -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]