diff --git a/crates/oxc_codegen/src/comment.rs b/crates/oxc_codegen/src/comment.rs index dab7e1ffd361bb..a288bbc0a617a7 100644 --- a/crates/oxc_codegen/src/comment.rs +++ b/crates/oxc_codegen/src/comment.rs @@ -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)) diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index ba06ef2ed19126..10cd716a69af38 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -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); } @@ -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); } @@ -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; } @@ -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); @@ -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); @@ -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| { @@ -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); diff --git a/crates/oxc_codegen/tests/integration/main.rs b/crates/oxc_codegen/tests/integration/main.rs index 5b57bcc09f0214..31327f9a484d02 100644 --- a/crates/oxc_codegen/tests/integration/main.rs +++ b/crates/oxc_codegen/tests/integration/main.rs @@ -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; diff --git a/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap b/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap index 51288eef6a92fe..410915d5f06da0 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap @@ -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(); diff --git a/tasks/coverage/snapshots/minifier_test262.snap b/tasks/coverage/snapshots/minifier_test262.snap index 3cad392eecf5b2..9b320b73fdbdaa 100644 --- a/tasks/coverage/snapshots/minifier_test262.snap +++ b/tasks/coverage/snapshots/minifier_test262.snap @@ -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%)