mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(codegen): print annotation comment inside parens for new and call expressions (#4290)
This commit is contained in:
parent
107e57019c
commit
bf3d8d3e8f
3 changed files with 26 additions and 5 deletions
|
|
@ -2,7 +2,7 @@
|
|||
use std::{env, path::Path};
|
||||
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_codegen::{CodeGenerator, WhitespaceRemover};
|
||||
use oxc_codegen::{CodeGenerator, CommentOptions, WhitespaceRemover};
|
||||
use oxc_parser::Parser;
|
||||
use oxc_span::SourceType;
|
||||
use pico_args::Arguments;
|
||||
|
|
@ -35,7 +35,14 @@ fn main() -> std::io::Result<()> {
|
|||
println!("{source_text}");
|
||||
|
||||
println!("First time:");
|
||||
let printed = CodeGenerator::new().build(&ret.program).source_text;
|
||||
let printed = CodeGenerator::new()
|
||||
.enable_comment(
|
||||
&source_text,
|
||||
ret.trivias.clone(),
|
||||
CommentOptions { preserve_annotate_comments: true },
|
||||
)
|
||||
.build(&ret.program)
|
||||
.source_text;
|
||||
println!("{printed}");
|
||||
|
||||
if twice {
|
||||
|
|
@ -48,7 +55,14 @@ fn main() -> std::io::Result<()> {
|
|||
}
|
||||
return Ok(());
|
||||
}
|
||||
let printed = CodeGenerator::new().build(&ret.program).source_text;
|
||||
let printed = CodeGenerator::new()
|
||||
.enable_comment(
|
||||
&source_text,
|
||||
ret.trivias.clone(),
|
||||
CommentOptions { preserve_annotate_comments: true },
|
||||
)
|
||||
.build(&ret.program)
|
||||
.source_text;
|
||||
println!("{printed}");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1419,8 +1419,8 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for CallExpression<'a> {
|
|||
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) {
|
||||
let wrap = precedence > self.precedence() || ctx.has_forbid_call();
|
||||
let ctx = ctx.and_forbid_call(false);
|
||||
p.gen_comment(self.span.start);
|
||||
p.wrap(wrap, |p| {
|
||||
p.gen_comment(self.span.start);
|
||||
p.add_source_mapping(self.span.start);
|
||||
self.callee.gen_expr(p, self.precedence(), ctx);
|
||||
if self.optional {
|
||||
|
|
@ -2049,8 +2049,8 @@ impl<'a, const MINIFY: bool> GenExpr<MINIFY> for ChainExpression<'a> {
|
|||
|
||||
impl<'a, const MINIFY: bool> GenExpr<MINIFY> for NewExpression<'a> {
|
||||
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) {
|
||||
p.gen_comment(self.span.start);
|
||||
p.wrap(precedence > self.precedence(), |p| {
|
||||
p.gen_comment(self.span.start);
|
||||
p.add_source_mapping(self.span.start);
|
||||
p.print_str("new ");
|
||||
self.callee.gen_expr(p, Precedence::NewWithoutArgs, ctx.and_forbid_call(true));
|
||||
|
|
|
|||
|
|
@ -200,4 +200,11 @@ const builtInSymbols = new Set(
|
|||
",
|
||||
"const builtInSymbols = new Set(/*#__PURE__*/ (Object.getOwnPropertyNames(Symbol)).filter((key) => key !== \"arguments\" && key !== \"caller\"));\n",
|
||||
);
|
||||
|
||||
test_comment_helper(
|
||||
"(/* @__PURE__ */ new Foo()).bar();\n",
|
||||
"(/* @__PURE__ */ new Foo()).bar();\n",
|
||||
);
|
||||
|
||||
test_comment_helper("(/* @__PURE__ */ Foo()).bar();\n", "(/* @__PURE__ */ Foo()).bar();\n");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue