Skip to content

Commit

Permalink
fix(codegen): print annotation comment inside parens for new and call…
Browse files Browse the repository at this point in the history
… expressions
  • Loading branch information
Boshen committed Jul 16, 2024
1 parent ee16668 commit c42ebba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
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");
}

0 comments on commit c42ebba

Please sign in to comment.