mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
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:
parent
81ed5885a8
commit
66b455a2b2
3 changed files with 30 additions and 0 deletions
|
|
@ -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 }>) {
|
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 {
|
match comment.kind {
|
||||||
CommentKind::SingleLine => {
|
CommentKind::SingleLine => {
|
||||||
p.print_str("//");
|
p.print_str("//");
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,7 @@ pub struct Codegen<'a, const MINIFY: bool> {
|
||||||
/// the first element of value is the start of the comment
|
/// 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.
|
/// the second element of value includes the end of the comment and comment kind.
|
||||||
move_comment_map: MoveCommentMap,
|
move_comment_map: MoveCommentMap,
|
||||||
|
latest_consumed_comment_end: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, const MINIFY: bool> Default for Codegen<'a, MINIFY> {
|
impl<'a, const MINIFY: bool> Default for Codegen<'a, MINIFY> {
|
||||||
|
|
@ -141,6 +142,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
|
||||||
quote: b'"',
|
quote: b'"',
|
||||||
sourcemap_builder: None,
|
sourcemap_builder: None,
|
||||||
move_comment_map: MoveCommentMap::default(),
|
move_comment_map: MoveCommentMap::default(),
|
||||||
|
latest_consumed_comment_end: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -421,6 +421,18 @@ const staticCacheMap = /*#__PURE__*/ new WeakMap()
|
||||||
",
|
",
|
||||||
"const staticCacheMap = /*#__PURE__*/ new WeakMap();\n",
|
"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]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue