@@ -22,19 +22,18 @@ export function AssignmentExpression(node, context) {
22
22
const should_cache = value . type !== 'Identifier' ;
23
23
const rhs = should_cache ? b . id ( '$$value' ) : value ;
24
24
25
- let unchanged = 0 ;
25
+ let changed = false ;
26
26
27
27
const assignments = extract_paths ( node . left ) . map ( ( path ) => {
28
28
const value = path . expression ?. ( rhs ) ;
29
- const assignment = b . assignment ( '=' , path . node , value ) ;
30
29
31
- return serialize_assignment ( '=' , path . node , value , context , ( ) => {
32
- unchanged += 1 ;
33
- return assignment ;
34
- } ) ;
30
+ let assignment = serialize_assignment ( '=' , path . node , value , context ) ;
31
+ if ( assignment !== null ) changed = true ;
32
+
33
+ return assignment ?? b . assignment ( '=' , path . node , value ) ;
35
34
} ) ;
36
35
37
- if ( unchanged === assignments . length ) {
36
+ if ( ! changed ) {
38
37
// No change to output -> nothing to transform -> we can keep the original assignment
39
38
return context . next ( ) ;
40
39
}
@@ -54,18 +53,17 @@ export function AssignmentExpression(node, context) {
54
53
return sequence ;
55
54
}
56
55
57
- return serialize_assignment ( node . operator , node . left , node . right , context , context . next ) ;
56
+ return serialize_assignment ( node . operator , node . left , node . right , context ) || context . next ( ) ;
58
57
}
59
58
60
59
/**
61
60
* @param {AssignmentOperator } operator
62
61
* @param {Pattern } left
63
62
* @param {Expression } right
64
63
* @param {import('zimmerframe').Context<SvelteNode, ServerTransformState> } context
65
- * @param {() => any } fallback
66
- * @returns {Expression }
64
+ * @returns {Expression | null }
67
65
*/
68
- function serialize_assignment ( operator , left , right , context , fallback ) {
66
+ function serialize_assignment ( operator , left , right , context ) {
69
67
let object = left ;
70
68
71
69
while ( object . type === 'MemberExpression' ) {
@@ -74,25 +72,26 @@ function serialize_assignment(operator, left, right, context, fallback) {
74
72
}
75
73
76
74
if ( object . type !== 'Identifier' || ! is_store_name ( object . name ) ) {
77
- return fallback ( ) ;
75
+ return null ;
78
76
}
79
77
80
78
const name = object . name . slice ( 1 ) ;
81
79
82
80
if ( ! context . state . scope . get ( name ) ) {
83
- return fallback ( ) ;
81
+ return null ;
84
82
}
85
83
86
84
if ( object === left ) {
87
- const value =
88
- operator === '='
89
- ? /** @type {Expression } */ ( context . visit ( right ) )
90
- : // turn something like x += 1 into x = x + 1
91
- b . binary (
92
- /** @type {BinaryOperator } */ ( operator . slice ( 0 , - 1 ) ) ,
93
- serialize_get_binding ( left , context . state ) ,
94
- /** @type {Expression } */ ( context . visit ( right ) )
95
- ) ;
85
+ let value = /** @type {Expression } */ ( context . visit ( right ) ) ;
86
+
87
+ if ( operator !== '=' ) {
88
+ // turn `x += 1` into `x = x + 1`
89
+ value = b . binary (
90
+ /** @type {BinaryOperator } */ ( operator . slice ( 0 , - 1 ) ) ,
91
+ serialize_get_binding ( left , context . state ) ,
92
+ value
93
+ ) ;
94
+ }
96
95
97
96
return b . call ( '$.store_set' , b . id ( name ) , value ) ;
98
97
}
0 commit comments