1
- /** @import { AssignmentExpression, BinaryOperator, Expression, Node, Pattern } from 'estree' */
1
+ /** @import { AssignmentExpression, AssignmentOperator, BinaryOperator, Expression, Node, Pattern } from 'estree' */
2
2
/** @import { SvelteNode } from '#compiler' */
3
3
/** @import { Context, ServerTransformState } from '../types.js' */
4
4
import * as b from '../../../../utils/builders.js' ;
@@ -25,9 +25,10 @@ export function AssignmentExpression(node, context) {
25
25
let unchanged = 0 ;
26
26
27
27
const assignments = extract_paths ( node . left ) . map ( ( path ) => {
28
- const assignment = b . assignment ( '=' , path . node , path . expression ?. ( rhs ) ) ;
28
+ const value = path . expression ?. ( rhs ) ;
29
+ const assignment = b . assignment ( '=' , path . node , value ) ;
29
30
30
- return serialize_assignment ( assignment , context , ( ) => {
31
+ return serialize_assignment ( '=' , path . node , value , context , ( ) => {
31
32
unchanged += 1 ;
32
33
return assignment ;
33
34
} ) ;
@@ -53,42 +54,44 @@ export function AssignmentExpression(node, context) {
53
54
return sequence ;
54
55
}
55
56
56
- return serialize_assignment ( node , context , context . next ) ;
57
+ return serialize_assignment ( node . operator , node . left , node . right , context , context . next ) ;
57
58
}
58
59
59
60
/**
60
- * @param {AssignmentExpression } node
61
+ * @param {AssignmentOperator } operator
62
+ * @param {Pattern } left
63
+ * @param {Expression } right
61
64
* @param {import('zimmerframe').Context<SvelteNode, ServerTransformState> } context
62
65
* @param {() => any } fallback
63
66
* @returns {Expression }
64
67
*/
65
- function serialize_assignment ( node , context , fallback ) {
66
- let left = node . left ;
68
+ function serialize_assignment ( operator , left , right , context , fallback ) {
69
+ let object = left ;
67
70
68
- while ( left . type === 'MemberExpression' ) {
71
+ while ( object . type === 'MemberExpression' ) {
69
72
// @ts -expect-error
70
- left = left . object ;
73
+ object = object . object ;
71
74
}
72
75
73
- if ( left . type !== 'Identifier' || ! is_store_name ( left . name ) ) {
76
+ if ( object . type !== 'Identifier' || ! is_store_name ( object . name ) ) {
74
77
return fallback ( ) ;
75
78
}
76
79
77
- const name = left . name . slice ( 1 ) ;
80
+ const name = object . name . slice ( 1 ) ;
78
81
79
82
if ( ! context . state . scope . get ( name ) ) {
80
83
return fallback ( ) ;
81
84
}
82
85
83
- if ( left === node . left ) {
86
+ if ( object === left ) {
84
87
const value =
85
- node . operator === '='
86
- ? /** @type {Expression } */ ( context . visit ( node . right ) )
88
+ operator === '='
89
+ ? /** @type {Expression } */ ( context . visit ( right ) )
87
90
: // turn something like x += 1 into x = x + 1
88
91
b . binary (
89
- /** @type {BinaryOperator } */ ( node . operator . slice ( 0 , - 1 ) ) ,
90
- serialize_get_binding ( node . left , context . state ) ,
91
- /** @type {Expression } */ ( context . visit ( node . right ) )
92
+ /** @type {BinaryOperator } */ ( operator . slice ( 0 , - 1 ) ) ,
93
+ serialize_get_binding ( left , context . state ) ,
94
+ /** @type {Expression } */ ( context . visit ( right ) )
92
95
) ;
93
96
94
97
return b . call ( '$.store_set' , b . id ( name ) , value ) ;
@@ -97,12 +100,12 @@ function serialize_assignment(node, context, fallback) {
97
100
return b . call (
98
101
'$.mutate_store' ,
99
102
b . assignment ( '??=' , b . id ( '$$store_subs' ) , b . object ( [ ] ) ) ,
100
- b . literal ( left . name ) ,
103
+ b . literal ( object . name ) ,
101
104
b . id ( name ) ,
102
105
b . assignment (
103
- node . operator ,
104
- /** @type {Pattern } */ ( context . visit ( node . left ) ) ,
105
- /** @type {Expression } */ ( context . visit ( node . right ) )
106
+ operator ,
107
+ /** @type {Pattern } */ ( context . visit ( left ) ) ,
108
+ /** @type {Expression } */ ( context . visit ( right ) )
106
109
)
107
110
) ;
108
111
}
0 commit comments