Skip to content

Commit

Permalink
refactor(rust): merge duplicate branches and reuse common logic (roll…
Browse files Browse the repository at this point in the history
…down#3311)

### Description

The branches `if matches!(importee.exports_kind, ExportsKind::CommonJs)`
and `if rec.meta.contains(ImportRecordMeta::IS_REQUIRE_UNUSED)` share
the same logic, so we can merge them.

Additionally, both branches use the same `wrap_ref_call_expr`, which can
be defined outside the branches for reuse.
  • Loading branch information
shulaoda authored Jan 8, 2025
1 parent 18da9cb commit de99c45
Showing 1 changed file with 28 additions and 44 deletions.
72 changes: 28 additions & 44 deletions crates/rolldown/src/module_finalizers/scope_hoisting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,56 +661,40 @@ impl<'me, 'ast> ScopeHoistingFinalizer<'me, 'ast> {
// `init_xxx`
let wrap_ref_expr = self
.finalized_expr_for_symbol_ref(importee_linking_info.wrapper_ref.unwrap(), false);
if matches!(importee.exports_kind, ExportsKind::CommonJs) {
// `init_xxx()`
Some(ast::Expression::CallExpression(self.snippet.builder.alloc_call_expression(

// `init_xxx()`
let wrap_ref_call_expr =
ast::Expression::CallExpression(self.snippet.builder.alloc_call_expression(
SPAN,
wrap_ref_expr,
NONE,
self.snippet.builder.vec(),
false,
)))
));

if matches!(importee.exports_kind, ExportsKind::CommonJs)
|| rec.meta.contains(ImportRecordMeta::IS_REQUIRE_UNUSED)
{
// `init_xxx()`
Some(wrap_ref_call_expr)
} else {
if rec.meta.contains(ImportRecordMeta::IS_REQUIRE_UNUSED) {
// `init_xxx()`
Some(ast::Expression::CallExpression(
self.snippet.builder.alloc_call_expression(
SPAN,
wrap_ref_expr,
NONE,
self.snippet.builder.vec(),
false,
),
))
} else {
// `xxx_exports`
let namespace_object_ref_expr =
self.finalized_expr_for_symbol_ref(importee.namespace_object_ref, false);
// `__toCommonJS`
let to_commonjs_expr = self.finalized_expr_for_runtime_symbol("__toCommonJS");
// `init_xxx()`
let wrap_ref_call_expr =
ast::Expression::CallExpression(self.snippet.builder.alloc_call_expression(
SPAN,
wrap_ref_expr,
NONE,
self.snippet.builder.vec(),
false,
));

// `__toCommonJS(xxx_exports)`
let to_commonjs_call_expr =
ast::Expression::CallExpression(self.snippet.builder.alloc_call_expression(
SPAN,
to_commonjs_expr,
NONE,
self.snippet.builder.vec1(ast::Argument::from(namespace_object_ref_expr)),
false,
));

// `(init_xxx(), __toCommonJS(xxx_exports))`
Some(self.snippet.seq2_in_paren_expr(wrap_ref_call_expr, to_commonjs_call_expr))
}
// `xxx_exports`
let namespace_object_ref_expr =
self.finalized_expr_for_symbol_ref(importee.namespace_object_ref, false);
// `__toCommonJS`
let to_commonjs_expr = self.finalized_expr_for_runtime_symbol("__toCommonJS");
// `__toCommonJS(xxx_exports)`
let to_commonjs_call_expr =
ast::Expression::CallExpression(self.snippet.builder.alloc_call_expression(
SPAN,
to_commonjs_expr,
NONE,
self.snippet.builder.vec1(ast::Argument::from(namespace_object_ref_expr)),
false,
));

// `(init_xxx(), __toCommonJS(xxx_exports))`
Some(self.snippet.seq2_in_paren_expr(wrap_ref_call_expr, to_commonjs_call_expr))
}
}
}
Expand Down

0 comments on commit de99c45

Please sign in to comment.