Skip to content

Commit f2ce6f4

Browse files
committed
transform this references, fix failing tests
1 parent ffab0a3 commit f2ce6f4

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/ObjectExpression.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import * as b from '../../../../utils/builders.js';
44
import { get_rune } from '../../../scope.js';
55
import { should_proxy } from '../utils.js';
6+
import { walk } from 'zimmerframe';
67

78
/**
89
* @param {ObjectExpression} node
@@ -25,24 +26,47 @@ export function ObjectExpression(node, context) {
2526
});
2627
}
2728
}
28-
if (!has_runes) return;
29+
if (!has_runes) {
30+
context.next();
31+
return;
32+
}
2933
let body = [];
3034
let sources = new Map();
35+
let has_this_reference = false;
3136
let counter = 0;
37+
let to_push = [];
3238
for (let { rune, property } of reactive_properties) {
3339
const name = context.state.scope.generate(`$$${++counter}`);
34-
const deep = rune !== '$state.raw';
3540
const call = rune.match(/^\$state/) ? '$.state' : '$.derived';
3641
/** @type {Expression} */
3742
let value = /** @type {Expression} */ (context.visit(property.value.arguments[0] ?? b.void0));
43+
value = walk(value, null, {
44+
FunctionExpression() {
45+
return;
46+
},
47+
//@ts-ignore
48+
FunctionDeclaration() {
49+
return;
50+
},
51+
ObjectExpression() {
52+
return;
53+
},
54+
ThisExpression() {
55+
has_this_reference = true;
56+
return b.id('$$object');
57+
},
58+
ClassBody() {
59+
return;
60+
}
61+
});
3862
value =
3963
rune === '$derived'
4064
? b.thunk(value)
4165
: rune === '$state' && should_proxy(value, context.state.scope)
4266
? b.call('$.proxy', value)
4367
: value;
4468
sources.set(property, [name, rune]);
45-
body.push(b.let(name, b.call(call, value)));
69+
to_push.push(b.let(name, b.call(call, value)));
4670
}
4771
/** @type {(Property | SpreadElement)[]} */
4872
let properties = [];
@@ -79,6 +103,13 @@ export function ObjectExpression(node, context) {
79103
properties.push(/** @type {Property} */ (context.visit(property)));
80104
}
81105
}
82-
body.push(b.return(b.object(properties)));
106+
if (has_this_reference) {
107+
body.push(b.let('$$object', b.object(properties)));
108+
body.push(...to_push);
109+
body.push(b.return(b.id('$$object')));
110+
} else {
111+
body.push(...to_push);
112+
body.push(b.return(b.object(properties)));
113+
}
83114
return b.call(b.arrow([], b.block(body)));
84115
}

0 commit comments

Comments
 (0)