Skip to content

Commit cf8df0b

Browse files
authored
chore: remove binding.expression (#12530)
* add state.getters as alternative to binding.expression * on second thoughts * fix * first of many * couple more * regenerate types * more * another * more * another * another * another * remove binding.expression from client-side code * tweak * last one * comment * regenerate types * add a changeset * small tidy up * simplify * simplify * simplify and fix * simplify
1 parent 37782be commit cf8df0b

File tree

9 files changed

+154
-121
lines changed

9 files changed

+154
-121
lines changed

.changeset/four-peas-tickle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: remove internal `binding.expression` mechanism

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export function client_component(source, analysis, options) {
6565
preserve_whitespace: options.preserveWhitespace,
6666
public_state: new Map(),
6767
private_state: new Map(),
68+
getters: {},
6869
in_constructor: false,
6970

7071
// these are set inside the `Fragment` visitor, and cannot be used until then
@@ -583,6 +584,7 @@ export function client_module(analysis, options) {
583584
legacy_reactive_statements: new Map(),
584585
public_state: new Map(),
585586
private_state: new Map(),
587+
getters: {},
586588
in_constructor: false
587589
};
588590

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ export function serialize_get_binding(node, state) {
8484
return b.call(node);
8585
}
8686

87-
if (binding.expression) {
88-
return typeof binding.expression === 'function' ? binding.expression(node) : binding.expression;
87+
if (Object.hasOwn(state.getters, node.name)) {
88+
const getter = state.getters[node.name];
89+
return typeof getter === 'function' ? getter(node) : getter;
8990
}
9091

9192
if (binding.kind === 'prop' || binding.kind === 'bindable_prop') {
@@ -527,17 +528,20 @@ function get_hoistable_params(node, context) {
527528
);
528529
}
529530

531+
const expression = context.state.getters[reference];
532+
530533
if (
531534
// If it's a destructured derived binding, then we can extract the derived signal reference and use that.
532-
binding.expression !== null &&
533-
typeof binding.expression !== 'function' &&
534-
binding.expression.type === 'MemberExpression' &&
535-
binding.expression.object.type === 'CallExpression' &&
536-
binding.expression.object.callee.type === 'Identifier' &&
537-
binding.expression.object.callee.name === '$.get' &&
538-
binding.expression.object.arguments[0].type === 'Identifier'
535+
// TODO this code is bad, we need to kill it
536+
expression != null &&
537+
typeof expression !== 'function' &&
538+
expression.type === 'MemberExpression' &&
539+
expression.object.type === 'CallExpression' &&
540+
expression.object.callee.type === 'Identifier' &&
541+
expression.object.callee.name === '$.get' &&
542+
expression.object.arguments[0].type === 'Identifier'
539543
) {
540-
push_unique(b.id(binding.expression.object.arguments[0].name));
544+
push_unique(b.id(expression.object.arguments[0].name));
541545
} else if (
542546
// If we are referencing a simple $$props value, then we need to reference the object property instead
543547
(binding.kind === 'prop' || binding.kind === 'bindable_prop') &&

0 commit comments

Comments
 (0)