Unverified Commit 52613867 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

turbopack: align purity codegen with batched bindings

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 4b3806b2
......@@ -76,12 +76,15 @@ impl EsmBindingsBuilder {
namespace_member: Option<RcStr>,
ast_path: AstPathId,
) {
self.bindings.entry(reference).or_default().push(EsmBinding {
export,
namespace_member,
ast_path,
keep_this: false,
});
self.bindings
.entry(reference)
.or_default()
.push(EsmBinding {
export,
namespace_member,
ast_path,
keep_this: false,
});
}
/// Where possible, bind the namespace to `this` when the named import is called.
......@@ -156,24 +159,25 @@ impl EsmBindings {
keep_this,
} in bindings
{
let no_side_effects =
if let (ReferencedAsset::Some(module), Some(export)) = (&imported_module, export) {
match purity_cache.entry((export.clone(), namespace_member.clone())) {
std::collections::hash_map::Entry::Vacant(entry) => {
let pure = *is_export_no_side_effects(
**module,
export.clone(),
namespace_member.clone(),
)
.await?;
entry.insert(pure);
pure
}
std::collections::hash_map::Entry::Occupied(entry) => *entry.get(),
let no_side_effects = if let (ReferencedAsset::Some(module), Some(export)) =
(&imported_module, export)
{
match purity_cache.entry((export.clone(), namespace_member.clone())) {
std::collections::hash_map::Entry::Vacant(entry) => {
let pure = *is_export_no_side_effects(
**module,
export.clone(),
namespace_member.clone(),
)
.await?;
entry.insert(pure);
pure
}
} else {
false
};
std::collections::hash_map::Entry::Occupied(entry) => *entry.get(),
}
} else {
false
};
has_no_side_effects |= no_side_effects;
let imported_ident = match &imported_module {
ReferencedAsset::None => ImportedIdent::None,
......@@ -252,8 +256,18 @@ impl EsmBindings {
let is_invocation_of = |path| {
let kind = trie.get(path);
(
matches!(kind, Some(swc_core::ecma::visit::AstParentKind::Callee(CalleeField::Expr))),
matches!(kind, Some(swc_core::ecma::visit::AstParentKind::TaggedTpl(TaggedTplField::Tag))),
matches!(
kind,
Some(swc_core::ecma::visit::AstParentKind::Callee(
CalleeField::Expr
))
),
matches!(
kind,
Some(swc_core::ecma::visit::AstParentKind::TaggedTpl(
TaggedTplField::Tag
))
),
)
};
// Only a directly replaced callee/tag may need a `this`-less accessor.
......@@ -264,7 +278,9 @@ impl EsmBindings {
usage_path = walk_up_through(
trie,
usage_path,
swc_core::ecma::visit::AstParentKind::MemberExpr(MemberExprField::Obj),
swc_core::ecma::visit::AstParentKind::MemberExpr(
MemberExprField::Obj,
),
swc_core::ecma::visit::AstParentKind::Expr(ExprField::Member),
);
}
......@@ -272,7 +288,9 @@ impl EsmBindings {
let next = walk_up_through(
trie,
usage_path,
swc_core::ecma::visit::AstParentKind::ParenExpr(ParenExprField::Expr),
swc_core::ecma::visit::AstParentKind::ParenExpr(
ParenExprField::Expr,
),
swc_core::ecma::visit::AstParentKind::Expr(ExprField::Paren),
);
if next == usage_path {
......@@ -283,7 +301,9 @@ impl EsmBindings {
let (is_call, is_tag) = is_invocation_of(usage_path);
let in_var_initializer = matches!(
trie.get(usage_path),
Some(swc_core::ecma::visit::AstParentKind::VarDeclarator(VarDeclaratorField::Init))
Some(swc_core::ecma::visit::AstParentKind::VarDeclarator(
VarDeclaratorField::Init
))
);
// Namespace members retain their object as the `this` receiver.
let preserve_this = *keep_this || namespace_member.is_some();
......@@ -296,7 +316,8 @@ impl EsmBindings {
ast_path,
visit_mut_expr,
|expr: &mut Expr| {
if no_side_effects && (is_call || is_tag || in_var_initializer) {
if no_side_effects && (is_call || is_tag || in_var_initializer)
{
generated_comments.add_leading(
expr.span().lo,
Comment {
......@@ -312,7 +333,8 @@ impl EsmBindings {
}
match &imported_ident {
ImportedIdent::Module(imported_ident) => {
*expr = imported_ident.as_expr(expr.span(), in_invocation);
*expr =
imported_ident.as_expr(expr.span(), in_invocation);
}
ImportedIdent::None => {
*expr = *Expr::undefined(expr.span());
......
......@@ -23,10 +23,8 @@ parenthesizedAlias();
__turbopack_context__.s([
"annotated",
0,
()=>annotated,
"unannotated",
0,
()=>unannotated
]);
const annotated = /*#__NO_SIDE_EFFECTS__*/ function() {};
......@@ -37,7 +35,6 @@ function unannotated() {}
__turbopack_context__.s([
"namespace",
0,
()=>__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2f$no$2d$side$2d$effects$2d$import$2f$input$2f$library$2e$js__$5b$test$5d$__$28$ecmascript$29$__
]);
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2f$no$2d$side$2d$effects$2d$import$2f$input$2f$library$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/no-side-effects-import/input/library.js [test] (ecmascript)");
......
......@@ -3,6 +3,6 @@
"sources": [],
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/no-side-effects-import/input/index.js"],"sourcesContent":["import { annotated, unannotated } from './library'\nimport { namespace } from './namespace-reexport'\n\nannotated()\nannotated`tagged`\nnamespace.annotated()\n\nconst alias = annotated\nalias()\n\n// prettier-ignore\nconst parenthesizedAlias = (annotated)\nparenthesizedAlias()\n\nunannotated()\nunannotated`tagged`\n"],"names":["annotated","alias","parenthesizedAlias"],"mappings":";AAAA;AACA;;;cAEA,IAAA,qOAAS;cACT,IAAA,qOAAS,CAAA,CAAC,MAAM,CAAC;cACjB,mPAAS,CAACA,SAAS;AAEnB,MAAMC,iCAAQ,qOAAS;AACvBA;AAEA,kBAAkB;AAClB,MAAMC,8CAAsB,qOAAS;AACrCA;AAEA,IAAA,uOAAW;AACX,IAAA,uOAAW,CAAA,CAAC,MAAM,CAAC"}},
{"offset": {"line": 23, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/no-side-effects-import/input/library.js"],"sourcesContent":["export const annotated = /*#__NO_SIDE_EFFECTS__*/ function () {}\nexport function unannotated() {}\n"],"names":["annotated","unannotated"],"mappings":";;;;;;;;AAAO,MAAMA,YAAY,sBAAsB,GAAG,YAAa;AACxD,SAASC,eAAe"}},
{"offset": {"line": 37, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/no-side-effects-import/input/namespace-reexport.js"],"sourcesContent":["export * as namespace from './library'\n"],"names":[],"mappings":";;;;;AAAA"}}]
{"offset": {"line": 23, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/no-side-effects-import/input/library.js"],"sourcesContent":["export const annotated = /*#__NO_SIDE_EFFECTS__*/ function () {}\nexport function unannotated() {}\n"],"names":["annotated","unannotated"],"mappings":";;;;;;AAAO,MAAMA,YAAY,sBAAsB,GAAG,YAAa;AACxD,SAASC,eAAe"}},
{"offset": {"line": 35, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic/no-side-effects-import/input/namespace-reexport.js"],"sourcesContent":["export * as namespace from './library'\n"],"names":[],"mappings":";;;;AAAA"}}]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment