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 7774271 commit 3f8db9b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 55 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
32 changes: 1 addition & 31 deletions tasks/coverage/snapshots/minifier_test262.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,4 @@ commit: d62fa93c

minifier_test262 Summary:
AST Parsed : 43765/43765 (100.00%)
Positive Passed: 43735/43765 (99.93%)
Compress: tasks/coverage/test262/test/built-ins/Array/prototype/at/returns-undefined-for-out-of-range-index.js
Compress: tasks/coverage/test262/test/built-ins/Array/prototype/every/15.4.4.16-1-1.js
Compress: tasks/coverage/test262/test/built-ins/Array/prototype/filter/15.4.4.20-1-1.js
Compress: tasks/coverage/test262/test/built-ins/Array/prototype/forEach/15.4.4.18-1-1.js
Compress: tasks/coverage/test262/test/built-ins/Array/prototype/map/15.4.4.19-1-1.js
Compress: tasks/coverage/test262/test/built-ins/Array/prototype/reduce/15.4.4.21-9-c-1.js
Compress: tasks/coverage/test262/test/built-ins/Array/prototype/reduceRight/15.4.4.22-9-c-1.js
Compress: tasks/coverage/test262/test/built-ins/Atomics/notify/count-defaults-to-infinity-undefined.js
Compress: tasks/coverage/test262/test/built-ins/DataView/prototype/setFloat16/to-boolean-littleendian.js
Compress: tasks/coverage/test262/test/built-ins/Object/defineProperty/15.2.3.6-4-3.js
Compress: tasks/coverage/test262/test/built-ins/Object/defineProperty/15.2.3.6-4-4.js
Compress: tasks/coverage/test262/test/built-ins/String/prototype/at/returns-undefined-for-out-of-range-index.js
Compress: tasks/coverage/test262/test/built-ins/Temporal/Duration/compare/order-of-operations.js
Compress: tasks/coverage/test262/test/built-ins/TypedArray/prototype/at/returns-undefined-for-out-of-range-index.js
Compress: tasks/coverage/test262/test/built-ins/undefined/15.1.1.3-2.js
Compress: tasks/coverage/test262/test/intl402/DateTimeFormat/prototype/formatRange/date-undefined-throws.js
Compress: tasks/coverage/test262/test/intl402/DateTimeFormat/prototype/formatRangeToParts/date-undefined-throws.js
Compress: tasks/coverage/test262/test/intl402/Segmenter/prototype/segment/containing/breakable-input.js
Compress: tasks/coverage/test262/test/intl402/Segmenter/prototype/segment/containing/iswordlike.js
Compress: tasks/coverage/test262/test/intl402/Segmenter/prototype/segment/containing/unbreakable-input.js
Compress: tasks/coverage/test262/test/intl402/Segmenter/prototype/segment/containing/word-iswordlike.js
Compress: tasks/coverage/test262/test/intl402/Segmenter/prototype/segment/containing/zero-index.js
Compress: tasks/coverage/test262/test/language/block-scope/leave/outermost-binding-updated-in-catch-block-nested-block-let-declaration-unseen-outside-of-block.js
Compress: tasks/coverage/test262/test/language/comments/S7.4_A1_T1.js
Compress: tasks/coverage/test262/test/language/comments/S7.4_A2_T1.js
Compress: tasks/coverage/test262/test/language/directive-prologue/14.1-12-s.js
Compress: tasks/coverage/test262/test/language/expressions/dynamic-import/namespace/await-ns-get-str-not-found.js
Compress: tasks/coverage/test262/test/language/expressions/dynamic-import/namespace/promise-then-ns-get-str-not-found.js
Compress: tasks/coverage/test262/test/staging/Intl402/Temporal/old/non-iso-calendars.js
Compress: tasks/coverage/test262/test/staging/decorators/public-auto-accessor.js
Positive Passed: 43765/43765 (100.00%)

0 comments on commit 3f8db9b

Please sign in to comment.