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(codegen): print annotation comment inside parens for new and call expressions #4290

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
20 changes: 17 additions & 3 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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}");
}

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_codegen/tests/integration/pure_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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