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 { ...@@ -76,12 +76,15 @@ impl EsmBindingsBuilder {
namespace_member: Option<RcStr>, namespace_member: Option<RcStr>,
ast_path: AstPathId, ast_path: AstPathId,
) { ) {
self.bindings.entry(reference).or_default().push(EsmBinding { self.bindings
export, .entry(reference)
namespace_member, .or_default()
ast_path, .push(EsmBinding {
keep_this: false, export,
}); namespace_member,
ast_path,
keep_this: false,
});
} }
/// Where possible, bind the namespace to `this` when the named import is called. /// Where possible, bind the namespace to `this` when the named import is called.
...@@ -156,24 +159,25 @@ impl EsmBindings { ...@@ -156,24 +159,25 @@ impl EsmBindings {
keep_this, keep_this,
} in bindings } in bindings
{ {
let no_side_effects = let no_side_effects = if let (ReferencedAsset::Some(module), Some(export)) =
if let (ReferencedAsset::Some(module), Some(export)) = (&imported_module, export) { (&imported_module, export)
match purity_cache.entry((export.clone(), namespace_member.clone())) { {
std::collections::hash_map::Entry::Vacant(entry) => { match purity_cache.entry((export.clone(), namespace_member.clone())) {
let pure = *is_export_no_side_effects( std::collections::hash_map::Entry::Vacant(entry) => {
**module, let pure = *is_export_no_side_effects(
export.clone(), **module,
namespace_member.clone(), export.clone(),
) namespace_member.clone(),
.await?; )
entry.insert(pure); .await?;
pure entry.insert(pure);
} pure
std::collections::hash_map::Entry::Occupied(entry) => *entry.get(),
} }
} else { std::collections::hash_map::Entry::Occupied(entry) => *entry.get(),
false }
}; } else {
false
};
has_no_side_effects |= no_side_effects; has_no_side_effects |= no_side_effects;
let imported_ident = match &imported_module { let imported_ident = match &imported_module {
ReferencedAsset::None => ImportedIdent::None, ReferencedAsset::None => ImportedIdent::None,
...@@ -252,8 +256,18 @@ impl EsmBindings { ...@@ -252,8 +256,18 @@ impl EsmBindings {
let is_invocation_of = |path| { let is_invocation_of = |path| {
let kind = trie.get(path); let kind = trie.get(path);
( (
matches!(kind, Some(swc_core::ecma::visit::AstParentKind::Callee(CalleeField::Expr))), matches!(
matches!(kind, Some(swc_core::ecma::visit::AstParentKind::TaggedTpl(TaggedTplField::Tag))), 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. // Only a directly replaced callee/tag may need a `this`-less accessor.
...@@ -264,7 +278,9 @@ impl EsmBindings { ...@@ -264,7 +278,9 @@ impl EsmBindings {
usage_path = walk_up_through( usage_path = walk_up_through(
trie, trie,
usage_path, 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), swc_core::ecma::visit::AstParentKind::Expr(ExprField::Member),
); );
} }
...@@ -272,7 +288,9 @@ impl EsmBindings { ...@@ -272,7 +288,9 @@ impl EsmBindings {
let next = walk_up_through( let next = walk_up_through(
trie, trie,
usage_path, 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), swc_core::ecma::visit::AstParentKind::Expr(ExprField::Paren),
); );
if next == usage_path { if next == usage_path {
...@@ -283,7 +301,9 @@ impl EsmBindings { ...@@ -283,7 +301,9 @@ impl EsmBindings {
let (is_call, is_tag) = is_invocation_of(usage_path); let (is_call, is_tag) = is_invocation_of(usage_path);
let in_var_initializer = matches!( let in_var_initializer = matches!(
trie.get(usage_path), 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. // Namespace members retain their object as the `this` receiver.
let preserve_this = *keep_this || namespace_member.is_some(); let preserve_this = *keep_this || namespace_member.is_some();
...@@ -296,7 +316,8 @@ impl EsmBindings { ...@@ -296,7 +316,8 @@ impl EsmBindings {
ast_path, ast_path,
visit_mut_expr, visit_mut_expr,
|expr: &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( generated_comments.add_leading(
expr.span().lo, expr.span().lo,
Comment { Comment {
...@@ -312,7 +333,8 @@ impl EsmBindings { ...@@ -312,7 +333,8 @@ impl EsmBindings {
} }
match &imported_ident { match &imported_ident {
ImportedIdent::Module(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 => { ImportedIdent::None => {
*expr = *Expr::undefined(expr.span()); *expr = *Expr::undefined(expr.span());
......
...@@ -23,10 +23,8 @@ parenthesizedAlias(); ...@@ -23,10 +23,8 @@ parenthesizedAlias();
__turbopack_context__.s([ __turbopack_context__.s([
"annotated", "annotated",
0,
()=>annotated, ()=>annotated,
"unannotated", "unannotated",
0,
()=>unannotated ()=>unannotated
]); ]);
const annotated = /*#__NO_SIDE_EFFECTS__*/ function() {}; const annotated = /*#__NO_SIDE_EFFECTS__*/ function() {};
...@@ -37,7 +35,6 @@ function unannotated() {} ...@@ -37,7 +35,6 @@ function unannotated() {}
__turbopack_context__.s([ __turbopack_context__.s([
"namespace", "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$__ ()=>__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)"); 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 @@ ...@@ -3,6 +3,6 @@
"sources": [], "sources": [],
"sections": [ "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": 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": 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": 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