Skip to content

Commit e07cc5c

Browse files
authored
[turbopack] Refactor the analyzer to fix a few issues and prepare to split it up (#86265)
# Refactor Analyzer State Management in Turbopack This PR refactors the state management in the Turbopack ECMAScript analyzer to improve code organization and maintainability. The changes include: - Introduced a `LexicalContext` enum to track the current execution context (function, module, control flow) and remove most dependencies on AstPath - Moved early return stack and variable declaration kind into the `AnalyzerState` struct - Added helper methods for entering and exiting different block types - Improved handling of nested blocks and control flow - Fixed unreachable code detection in anonymous blocks which allows us to drop some redundant unreachable effects from loops - Handle more cases for nested `var` declarations These refactorings are all either fixing minor issues or regularizing state management. This is preparation for splitting this AST traversal into two pieces.
1 parent 5b62dd8 commit e07cc5c

26 files changed

+1138
-864
lines changed

turbopack/crates/turbopack-ecmascript/src/analyzer/graph.rs

Lines changed: 422 additions & 297 deletions
Large diffs are not rendered by default.

turbopack/crates/turbopack-ecmascript/src/path_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a> ApplyVisitors<'a, '_> {
113113
}
114114
return;
115115
} else {
116-
// `current_visitors` has the invariant that is must not be empty.
116+
// `current_visitors` has the invariant that it must not be empty.
117117
// When it becomes empty, we must early exit
118118
current_visitors = &visitors[nested_visitors_start..];
119119
if current_visitors.is_empty() {

turbopack/crates/turbopack-ecmascript/tests/analyzer/graph/react-dom-production/graph-explained.snapshot

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,7 +2843,7 @@ b#494 = di()
28432843

28442844
b#5 = arguments[1]
28452845

2846-
b#50 = (a["render"] | (a["displayName"] || null) | a["_payload"])
2846+
b#50 = (a["render"] | undefined | (a["displayName"] || null) | a["_payload"])
28472847

28482848
b#501 = di()
28492849

@@ -3230,7 +3230,7 @@ b#979 = arguments[1]
32303230

32313231
b#986 = arguments[1]
32323232

3233-
b#990 = a["stateNode"]
3233+
b#990 = (a["stateNode"] | undefined)
32343234

32353235
b#992 = Zg(a, 1)
32363236

@@ -3454,6 +3454,7 @@ c#361 = Bg(5, null, null, 0)
34543454

34553455
c#362 = (
34563456
| a["type"]
3457+
| undefined
34573458
| ((null !== qg) ? {"id": rg, "overflow": sg} : null)
34583459
| Bg(18, null, null, 0)
34593460
)
@@ -3687,7 +3688,7 @@ c#64 = b["getValue"]()
36873688

36883689
c#644 = arguments[2]
36893690

3690-
c#645 = (null | b | a["tail"] | c["sibling"])
3691+
c#645 = (null | undefined | b | a["tail"] | c["sibling"])
36913692

36923693
c#646 = (0 | ???*0*)
36933694
- *0* unsupported assign operation
@@ -4228,7 +4229,7 @@ d#621 = (b["pendingProps"] | M["current"] | ???*0* | ???*1*)
42284229
- *1* unsupported assign operation
42294230
⚠️ This value might have side effects
42304231

4231-
d#631 = (b["type"]["_context"] | b["memoizedState"] | (0 !== ???*0*))
4232+
d#631 = (b["type"]["_context"] | undefined | b["memoizedState"] | (0 !== ???*0*))
42324233
- *0* unsupported expression
42334234
⚠️ This value might have side effects
42344235

@@ -4241,7 +4242,7 @@ d#64 = (
42414242

42424243
d#644 = arguments[3]
42434244

4244-
d#645 = (null | c)
4245+
d#645 = (null | undefined | c)
42454246

42464247
d#646 = (0 | ???*0*)
42474248
- *0* unsupported assign operation
@@ -4287,13 +4288,13 @@ d#703 = a["tag"]
42874288

42884289
d#704 = a["tag"]
42894290

4290-
d#706 = (X | c["updateQueue"] | d["lastEffect"] | d["next"] | c["stateNode"] | U)
4291+
d#706 = (X | undefined | c["updateQueue"] | d["lastEffect"] | d["next"] | c["stateNode"] | U)
42914292

42924293
d#71 = b["type"]
42934294

42944295
d#715 = ck["bind"](null, a, b)
42954296

4296-
d#716 = (0 | ???*0*)
4297+
d#716 = (0 | undefined | ???*0*)
42974298
- *0* updated with update expression
42984299
⚠️ This value might have side effects
42994300

@@ -4357,10 +4358,11 @@ d#910 = (b["stateNode"] | undefined)
43574358

43584359
d#915 = a["pingCache"]
43594360

4360-
d#918 = a["stateNode"]
4361+
d#918 = (a["stateNode"] | undefined)
43614362

43624363
d#919 = (
43634364
| b["type"]
4365+
| undefined
43644366
| b["elementType"]
43654367
| e(d["_payload"])
43664368
| b["pendingProps"]
@@ -4491,7 +4493,7 @@ e#266 = (c["textContent"]["length"] | undefined | d | Ke(c, f))
44914493

44924494
e#275 = (d["event"] | undefined)
44934495

4494-
e#285 = (ed | gd | fd | undefined | !(0))
4496+
e#285 = (ed | undefined | gd | fd | !(0))
44954497

44964498
e#286 = arguments[4]
44974499

@@ -4619,14 +4621,15 @@ e#620 = arguments[4]
46194621

46204622
e#621 = (d["revealOrder"] | null | c | b["child"] | c["sibling"] | a)
46214623

4622-
e#631 = (b["memoizedProps"]["value"] | b["memoizedState"])
4624+
e#631 = (b["memoizedProps"]["value"] | undefined | b["memoizedState"])
46234625

46244626
e#639 = (a["memoizedProps"] | Ya(a, e) | A({}, e, {"value": undefined}) | gb(a, e))
46254627

4626-
e#646 = (a["child"] | e["sibling"])
4628+
e#646 = (a["child"] | undefined | e["sibling"])
46274629

46284630
e#647 = (
46294631
| Hh(Gh["current"])
4632+
| undefined
46304633
| 0
46314634
| ???*0*
46324635
| null
@@ -4644,7 +4647,7 @@ e#673 = (d["anchorOffset"] | undefined)
46444647

46454648
e#687 = (d["next"] | undefined | e["next"])
46464649

4647-
e#706 = (Yj | d["next"] | e["next"])
4650+
e#706 = (Yj | undefined | d["next"] | e["next"])
46484651

46494652
e#716 = (c[d] | undefined)
46504653

@@ -4687,10 +4690,11 @@ e#883 = (K | undefined)
46874690

46884691
e#9 = arguments[4]
46894692

4690-
e#918 = a["memoizedState"]
4693+
e#918 = (a["memoizedState"] | undefined)
46914694

46924695
e#919 = (
46934696
| Yf(b, H["current"])
4697+
| undefined
46944698
| Xh(null, b, d, a, e, c)
46954699
| d["_init"]
46964700
| $k(d)
@@ -4776,7 +4780,7 @@ f#169 = (???*0* | undefined)
47764780

47774781
f#175 = arguments[5]
47784782

4779-
f#176 = e["pointerId"]
4783+
f#176 = (e["pointerId"] | undefined)
47804784

47814785
f#193 = cd["transition"]
47824786

@@ -4959,6 +4963,7 @@ f#9 = arguments[5]
49594963

49604964
f#919 = (
49614965
| bi()
4966+
| undefined
49624967
| !(0)
49634968
| !(1)
49644969
| b["memoizedState"]

0 commit comments

Comments
 (0)