Unverified Commit 1f4fa25f authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

fix(turbopack): Stop analyzer if IIFE is recursive (#74628)

### What?

Add a check for recursive IIFE.

### Why?

Turbopack miscompiles recursive IIFEs because it does not check if IIFE recurses.

### How?

Closes https://github.com/vercel/next.js/issues/74190
parent 5f4f96c1
......@@ -22,7 +22,7 @@ indexmap = { workspace = true }
lazy_static = { workspace = true }
once_cell = { workspace = true }
patricia_tree = "0.5.5"
petgraph = { workspace = true }
petgraph = { workspace = true, features = ["serde-1"] }
ref-cast = "1.0.20"
regex = { workspace = true }
serde = { workspace = true, features = ["rc"] }
......
......@@ -10,6 +10,7 @@ use swc_core::{
ecma::{
ast::*,
atoms::js_word,
utils::contains_ident_ref,
visit::{fields::*, *},
},
};
......@@ -863,6 +864,13 @@ impl Analyzer<'_> {
let mut ast_path = ast_path
.with_guard(AstParentNodeRef::FnExpr(fn_expr, FnExprField::Ident));
self.visit_opt_ident(ident, &mut ast_path);
// We cannot analyze recursive IIFE
if let Some(ident) = ident {
if contains_ident_ref(&function.body, &ident.to_id()) {
return false;
}
}
}
{
......
_ = (...) => parent
i = (0 | ???*0*)
- *0* updated with update expression
⚠️ This value might have side effects
key = keys[i]
keys = FreeVar(Object)["keys"](namespace)
namespace = arguments[0]
parent = arguments[2]
path = arguments[1]
root = {}
[
(
"_",
Function(
2,
17,
Variable(
(
"parent",
#4,
),
),
),
),
(
"i",
Alternatives {
total_nodes: 3,
values: [
Constant(
Num(
ConstantNumber(
0.0,
),
),
),
Unknown {
original_value: None,
reason: "updated with update expression",
has_side_effects: true,
},
],
logical_property: None,
},
),
(
"key",
Member(
3,
Variable(
(
"keys",
#4,
),
),
Variable(
(
"i",
#4,
),
),
),
),
(
"keys",
MemberCall(
4,
FreeVar(
"Object",
),
Constant(
Str(
Atom(
"keys",
),
),
),
[
Variable(
(
"namespace",
#4,
),
),
],
),
),
(
"namespace",
Argument(
17,
0,
),
),
(
"parent",
Argument(
17,
2,
),
),
(
"path",
Argument(
17,
1,
),
),
(
"root",
Object {
total_nodes: 1,
parts: [],
mutable: true,
},
),
]
var root = {};
(function _(namespace, path, parent) {
for (var i = 0, keys = Object.keys(namespace); i < keys.length; i++) {
var key = keys[i];
console.debug("####", path, path ? true : false)
parent[key] = _(namespace[key], path ? `${path}.${key}` : key, parent[key] || {});
}
return parent;
})({
wrapper: {
data: {
key: {}
},
}
}, "", root);
0 -> 2 free var = FreeVar(Object)
0 -> 3 member call = Object*0*["keys"](???*1*)
- *0* Object: The global Object variable
- *1* arguments[0]
⚠️ function calls are not analysed yet
0 -> 7 free var = FreeVar(console)
0 -> 8 conditional = ???*0*
- *0* arguments[1]
⚠️ function calls are not analysed yet
0 -> 9 member call = ???*0*["debug"]("####", ???*1*, (???*2* ? true : false))
- *0* FreeVar(console)
⚠️ unknown global
⚠️ This value might have side effects
- *1* arguments[1]
⚠️ function calls are not analysed yet
- *2* arguments[1]
⚠️ function calls are not analysed yet
0 -> 12 conditional = ???*0*
- *0* arguments[1]
⚠️ function calls are not analysed yet
0 -> 14 call = (...) => parent(???*0*, (???*2* ? ???*3* : ???*10*), (???*15* | {}){truthy})
- *0* ???*1*[key]
⚠️ unknown object
- *1* arguments[0]
⚠️ function calls are not analysed yet
- *2* arguments[1]
⚠️ function calls are not analysed yet
- *3* `${???*4*}.${???*5*}`
⚠️ nested operation
- *4* arguments[1]
⚠️ function calls are not analysed yet
- *5* ???*6*[i]
⚠️ unknown object
⚠️ This value might have side effects
- *6* ???*7*(???*9*)
⚠️ unknown callee
⚠️ This value might have side effects
- *7* Object*8*["keys"]
⚠️ unsupported property on global Object
⚠️ This value might have side effects
- *8* Object: The global Object variable
- *9* arguments[0]
⚠️ function calls are not analysed yet
- *10* ???*11*[i]
⚠️ unknown object
⚠️ This value might have side effects
- *11* ???*12*(???*14*)
⚠️ unknown callee
⚠️ This value might have side effects
- *12* Object*13*["keys"]
⚠️ unsupported property on global Object
⚠️ This value might have side effects
- *13* Object: The global Object variable
- *14* arguments[0]
⚠️ function calls are not analysed yet
- *15* ???*16*[key]
⚠️ unknown object
- *16* arguments[2]
⚠️ function calls are not analysed yet
0 -> 15 unreachable = ???*0*
- *0* unreachable
⚠️ This value might have side effects
0 -> 16 call = (...) => parent({"wrapper": {"data": {"key": {}}}}, "", {})
_ = (...) => parent
i = (0 | ???*0*)
- *0* updated with update expression
⚠️ This value might have side effects
key = ???*0*
- *0* ???*1*[i]
⚠️ unknown object
⚠️ This value might have side effects
- *1* ???*2*(???*4*)
⚠️ unknown callee
⚠️ This value might have side effects
- *2* Object*3*["keys"]
⚠️ unsupported property on global Object
⚠️ This value might have side effects
- *3* Object: The global Object variable
- *4* arguments[0]
⚠️ function calls are not analysed yet
keys = ???*0*
- *0* ???*1*(???*3*)
⚠️ unknown callee
⚠️ This value might have side effects
- *1* Object*2*["keys"]
⚠️ unsupported property on global Object
⚠️ This value might have side effects
- *2* Object: The global Object variable
- *3* arguments[0]
⚠️ function calls are not analysed yet
namespace = ???*0*
- *0* arguments[0]
⚠️ function calls are not analysed yet
parent = ???*0*
- *0* arguments[2]
⚠️ function calls are not analysed yet
path = ???*0*
- *0* arguments[1]
⚠️ function calls are not analysed yet
root = {}
var root = {};
const result = (function _(namespace, path, parent) {
console.debug("####", path, path ? true : false)
for (var i = 0, keys = Object.keys(namespace); i < keys.length; i++) {
var key = keys[i];
parent[key] = _(namespace[key], path ? `${path}.${key}` : key, parent[key] || {});
}
return parent;
})({
wrapper: {
data: {
key: {}
},
}
}, "", root);
it('should evaluate IIFE correctly', () => {
expect(result).toEqual({
wrapper: {
data: {
key: {}
},
}
});
});
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