Skip to content

Commit

Permalink
fix(parser): parse @__NO_SIDE_EFFECTS between export default and …
Browse files Browse the repository at this point in the history
…`async function`
  • Loading branch information
Boshen committed Mar 3, 2025
1 parent d7b6528 commit 536ede8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl Gen for Statement<'_> {
Self::FunctionDeclaration(decl) => {
p.print_statement_comments(decl.span.start);
if decl.pure && p.options.print_annotation_comments() {
p.print_indent();
p.print_str(NO_SIDE_EFFECTS_NEW_LINE_COMMENT);
}
p.print_indent();
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_codegen/tests/integration/pure_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ async function* d() {}
/* @__NO_SIDE_EFFECTS__ */ export function* b() {}
/* @__NO_SIDE_EFFECTS__ */ export async function c() {}
/* @__NO_SIDE_EFFECTS__ */ export async function* d() {}
export default /* @__NO_SIDE_EFFECTS__ */ async function() {}
export default /* @__NO_SIDE_EFFECTS__ */ function() {}
",
// Only "c0" and "c2" should have "no side effects" (Rollup only respects "const" and only for the first one)
r"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export async function* d() {}
/* @__NO_SIDE_EFFECTS__ */ export function* b() {}
/* @__NO_SIDE_EFFECTS__ */ export async function c() {}
/* @__NO_SIDE_EFFECTS__ */ export async function* d() {}
export default /* @__NO_SIDE_EFFECTS__ */ async function() {}
export default /* @__NO_SIDE_EFFECTS__ */ function() {}

----------
/* @__NO_SIDE_EFFECTS__ */
Expand All @@ -138,6 +140,10 @@ export function* b() {}
export async function c() {}
/* @__NO_SIDE_EFFECTS__ */
export async function* d() {}
/* @__NO_SIDE_EFFECTS__ */
export default async function() {}
/* @__NO_SIDE_EFFECTS__ */
export default function() {}

########## 7

Expand Down
12 changes: 9 additions & 3 deletions crates/oxc_parser/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ impl<'a> ParserImpl<'a> {
) -> Result<Box<'a, ExportDefaultDeclaration<'a>>> {
let exported = self.parse_keyword_identifier(Kind::Default);
let decl_span = self.start_span();
let has_no_side_effects_comment =
self.lexer.trivia_builder.previous_token_has_no_side_effects_comment();
// For tc39/proposal-decorators
// For more information, please refer to <https://babeljs.io/docs/babel-plugin-proposal-decorators#decoratorsbeforeexport>
self.eat_decorators()?;
Expand All @@ -406,9 +408,13 @@ impl<'a> ParserImpl<'a> {
}
})?
}
_ if self.at_function_with_async() => self
.parse_function_impl(FunctionKind::DefaultExport)
.map(ExportDefaultDeclarationKind::FunctionDeclaration)?,
_ if self.at_function_with_async() => {
let mut func = self.parse_function_impl(FunctionKind::DefaultExport)?;
if has_no_side_effects_comment {
func.pure = true;
}
ExportDefaultDeclarationKind::FunctionDeclaration(func)
}
_ => {
let decl = self
.parse_assignment_expression_or_higher()
Expand Down

0 comments on commit 536ede8

Please sign in to comment.