Skip to content

Commit 96eacd8

Browse files
committed
model assignments as side effectful expressions
1 parent 36f50c5 commit 96eacd8

File tree

22 files changed

+7215
-8870
lines changed

22 files changed

+7215
-8870
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ impl EvalContext {
809809
}) => JsValue::WellKnownObject(WellKnownObjectKind::ImportMeta),
810810

811811
Expr::Assign(AssignExpr { op, right, .. }) => match op {
812-
AssignOp::Assign => self.eval(right),
812+
AssignOp::Assign => JsValue::effectful(self.eval(right)),
813813
_ => JsValue::unknown_empty(true, "compound assignment expression"),
814814
},
815815

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

Lines changed: 69 additions & 77 deletions
Large diffs are not rendered by default.

turbopack/crates/turbopack-ecmascript/tests/analyzer/graph/logical/graph-effects.snapshot

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,4 +689,111 @@
689689
],
690690
span: 378..393,
691691
},
692+
Conditional {
693+
condition: Effectful(
694+
2,
695+
Variable(
696+
(
697+
"r",
698+
#2,
699+
),
700+
),
701+
),
702+
kind: And {
703+
rhs_effects: [
704+
Member {
705+
obj: Variable(
706+
(
707+
"t",
708+
#2,
709+
),
710+
),
711+
prop: Constant(
712+
Str(
713+
Atom(
714+
"__esModule",
715+
),
716+
),
717+
),
718+
ast_path: [
719+
Program(
720+
Script,
721+
),
722+
Script(
723+
Body(
724+
16,
725+
),
726+
),
727+
Stmt(
728+
Decl,
729+
),
730+
Decl(
731+
Var,
732+
),
733+
VarDecl(
734+
Decls(
735+
1,
736+
),
737+
),
738+
VarDeclarator(
739+
Init,
740+
),
741+
Expr(
742+
Cond,
743+
),
744+
CondExpr(
745+
Test,
746+
),
747+
Expr(
748+
Bin,
749+
),
750+
BinExpr(
751+
Right,
752+
),
753+
Expr(
754+
Member,
755+
),
756+
],
757+
span: 436..448,
758+
},
759+
],
760+
},
761+
ast_path: [
762+
Program(
763+
Script,
764+
),
765+
Script(
766+
Body(
767+
16,
768+
),
769+
),
770+
Stmt(
771+
Decl,
772+
),
773+
Decl(
774+
Var,
775+
),
776+
VarDecl(
777+
Decls(
778+
1,
779+
),
780+
),
781+
VarDeclarator(
782+
Init,
783+
),
784+
Expr(
785+
Cond,
786+
),
787+
CondExpr(
788+
Test,
789+
),
790+
Expr(
791+
Bin,
792+
),
793+
BinExpr(
794+
Left,
795+
),
796+
],
797+
span: 425..448,
798+
},
692799
]

turbopack/crates/turbopack-ecmascript/tests/analyzer/graph/logical/graph-explained.snapshot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ d (const after eval) = !(x)
1212

1313
e (const after eval) = !(!(x))
1414

15+
n (const after eval) = ((side-effectful(r) && t["__esModule"]) ? t : {"default": t})
16+
17+
r (const after eval) = {}
18+
1519
resolve1 (const after eval) = (1 && 2 && FreeVar(global) && 3 && 4)
1620

1721
resolve2 (const after eval) = (1 && 2 && 0 && FreeVar(global) && 4)
@@ -24,6 +28,10 @@ resolve5 (const after eval) = (FreeVar(global) && false)
2428

2529
resolve6 (const after eval) = (false && FreeVar(global))
2630

31+
t (const after eval) = (???*0* | r)
32+
- *0* t
33+
⚠️ pattern without value
34+
2735
x (const after eval) = true
2836

2937
y (const after eval) = false

turbopack/crates/turbopack-ecmascript/tests/analyzer/graph/logical/graph.snapshot

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,78 @@
168168
),
169169
),
170170
),
171+
(
172+
"n",
173+
Tenary(
174+
11,
175+
Logical(
176+
6,
177+
And,
178+
[
179+
Effectful(
180+
2,
181+
Variable(
182+
(
183+
"r",
184+
#2,
185+
),
186+
),
187+
),
188+
Member(
189+
3,
190+
Variable(
191+
(
192+
"t",
193+
#2,
194+
),
195+
),
196+
Constant(
197+
Str(
198+
Atom(
199+
"__esModule",
200+
),
201+
),
202+
),
203+
),
204+
],
205+
),
206+
Variable(
207+
(
208+
"t",
209+
#2,
210+
),
211+
),
212+
Object {
213+
total_nodes: 3,
214+
parts: [
215+
KeyValue(
216+
Constant(
217+
Str(
218+
Atom(
219+
"default",
220+
),
221+
),
222+
),
223+
Variable(
224+
(
225+
"t",
226+
#2,
227+
),
228+
),
229+
),
230+
],
231+
mutable: true,
232+
},
233+
),
234+
),
235+
(
236+
"r",
237+
Object {
238+
total_nodes: 1,
239+
parts: [],
240+
mutable: true,
241+
},
242+
),
171243
(
172244
"resolve1",
173245
Logical(
@@ -308,6 +380,33 @@
308380
],
309381
),
310382
),
383+
(
384+
"t",
385+
Alternatives {
386+
total_nodes: 3,
387+
values: [
388+
Unknown {
389+
original_value: Some(
390+
Variable(
391+
(
392+
"t",
393+
#2,
394+
),
395+
),
396+
),
397+
reason: "pattern without value",
398+
has_side_effects: false,
399+
},
400+
Variable(
401+
(
402+
"r",
403+
#2,
404+
),
405+
),
406+
],
407+
logical_property: None,
408+
},
409+
),
311410
(
312411
"x",
313412
Constant(

turbopack/crates/turbopack-ecmascript/tests/analyzer/graph/logical/input.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ let resolve3 = global || true;
1414
let resolve4 = true || global;
1515
let resolve5 = global && false;
1616
let resolve6 = false && global;
17+
18+
const r = {};
19+
var t,
20+
n = (t = r) && t.__esModule ? t : { default: t }

turbopack/crates/turbopack-ecmascript/tests/analyzer/graph/logical/resolved-effects.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@
2525
0 -> 13 conditional = false
2626

2727
13 -> 14 free var = FreeVar(global)
28+
29+
0 -> 15 conditional = side-effectful({})

turbopack/crates/turbopack-ecmascript/tests/analyzer/graph/logical/resolved-explained.snapshot

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ d = false
1818

1919
e = true
2020

21+
n = ((side-effectful({}) | ???*0* | undefined | ???*2*) ? (???*3* | {}) : {"default": (???*4* | {})})
22+
- *0* ???*1*["__esModule"]
23+
⚠️ unknown object
24+
- *1* t
25+
⚠️ pattern without value
26+
- *2* unknown mutation
27+
⚠️ This value might have side effects
28+
- *3* t
29+
⚠️ pattern without value
30+
- *4* t
31+
⚠️ pattern without value
32+
33+
r = {}
34+
2135
resolve1 = (???*0* | 4)
2236
- *0* FreeVar(global)
2337
⚠️ unknown global
@@ -39,6 +53,10 @@ resolve5 = (???*0* | false){falsy}
3953

4054
resolve6 = false
4155

56+
t = (???*0* | {})
57+
- *0* t
58+
⚠️ pattern without value
59+
4260
x = true
4361

4462
y = false

0 commit comments

Comments
 (0)