Skip to content

Commit

Permalink
follow esbuild behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Sep 26, 2024
1 parent 5b590c5 commit 49f34f5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
6 changes: 5 additions & 1 deletion crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ impl<'a> Codegen<'a> {
}
}

pub fn has_annotation_comments(&self, start: u32) -> bool {
pub fn has_comment(&self, start: u32) -> bool {
self.comments.contains_key(&start)
}

pub fn has_annotation_comment(&self, start: u32) -> bool {
let Some(source_text) = self.source_text else { return false };
self.comments.get(&start).is_some_and(|comments| {
comments.iter().any(|comment| Self::is_annotation_comment(comment, source_text))
Expand Down
32 changes: 11 additions & 21 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ impl<'a> Gen for VariableDeclaration<'a> {
&& p.start_of_annotation_comment.is_none()
&& matches!(self.kind, VariableDeclarationKind::Const)
&& matches!(self.declarations.first(), Some(VariableDeclarator { init: Some(init), .. }) if init.is_function())
&& p.has_annotation_comments(self.span.start)
&& p.has_annotation_comment(self.span.start)
{
p.start_of_annotation_comment = Some(self.span.start);
}
Expand Down Expand Up @@ -834,7 +834,7 @@ impl<'a> Gen for ExportNamedDeclaration<'a> {
if matches!(var_decl.kind, VariableDeclarationKind::Const) =>
{
if matches!(var_decl.declarations.first(), Some(VariableDeclarator { init: Some(init), .. }) if init.is_function())
&& p.has_annotation_comments(self.span.start)
&& p.has_annotation_comment(self.span.start)
{
p.start_of_annotation_comment = Some(self.span.start);
}
Expand Down Expand Up @@ -1368,7 +1368,7 @@ impl<'a> GenExpr for CallExpression<'a> {
fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) {
let is_export_default = p.start_of_default_export == p.code_len();
let mut wrap = precedence >= Precedence::New || ctx.intersects(Context::FORBID_CALL);
if p.has_annotation_comments(self.span.start) && precedence >= Precedence::Postfix {
if p.has_annotation_comment(self.span.start) && precedence >= Precedence::Postfix {
wrap = true;
}

Expand All @@ -1386,12 +1386,8 @@ impl<'a> GenExpr for CallExpression<'a> {
type_parameters.print(p, ctx);
}
p.print_char(b'(');
let has_comment = (self.span.end > 0
&& p.has_non_annotation_comment(self.span.end - 1))
|| self
.arguments
.iter()
.any(|item| p.has_non_annotation_comment(item.span().start));
let has_comment = (self.span.end > 0 && p.has_comment(self.span.end - 1))
|| self.arguments.iter().any(|item| p.has_comment(item.span().start));
if has_comment {
p.indent();
p.print_list_with_comments(&self.arguments, ctx);
Expand Down Expand Up @@ -1965,12 +1961,9 @@ impl<'a> GenExpr for SequenceExpression<'a> {
impl<'a> GenExpr for ImportExpression<'a> {
fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) {
let wrap = precedence >= Precedence::New || ctx.intersects(Context::FORBID_CALL);
let has_comment = (self.span.end > 0 && p.has_non_annotation_comment(self.span.end - 1))
|| p.has_non_annotation_comment(self.source.span().start)
|| self
.arguments
.first()
.is_some_and(|argument| p.has_non_annotation_comment(argument.span().start));
let has_comment = (self.span.end > 0 && p.has_comment(self.span.end - 1))
|| p.has_comment(self.source.span().start)
|| self.arguments.first().is_some_and(|argument| p.has_comment(argument.span().start));

p.wrap(wrap, |p| {
p.add_source_mapping(self.span.start);
Expand Down Expand Up @@ -2067,7 +2060,7 @@ impl<'a> GenExpr for ChainExpression<'a> {
impl<'a> GenExpr for NewExpression<'a> {
fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) {
let mut wrap = precedence >= self.precedence();
if p.has_annotation_comments(self.span.start) && precedence >= Precedence::Postfix {
if p.has_annotation_comment(self.span.start) && precedence >= Precedence::Postfix {
wrap = true;
}
p.wrap(wrap, |p| {
Expand All @@ -2077,11 +2070,8 @@ impl<'a> GenExpr for NewExpression<'a> {
p.print_str("new ");
self.callee.print_expr(p, Precedence::New, Context::FORBID_CALL);
p.print_char(b'(');
let has_comment = p.has_non_annotation_comment(self.span.end - 1)
|| self
.arguments
.iter()
.any(|item| p.has_non_annotation_comment(item.span().start));
let has_comment = p.has_comment(self.span.end - 1)
|| self.arguments.iter().any(|item| p.has_comment(item.span().start));
if has_comment {
p.indent();
p.print_list_with_comments(&self.arguments, ctx);
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_codegen/tests/integration/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(clippy::missing_panics_doc)]
pub mod esbuild;
pub mod inner_comments;
pub mod jsdoc;
pub mod pure_comments;
pub mod tester;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ const builtInSymbols = new Set(
)

----------
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')
);

########## 14
(/* @__PURE__ */ new Foo()).bar();
Expand Down

0 comments on commit 49f34f5

Please sign in to comment.