Skip to content

Commit 23e2d40

Browse files
committed
use $$props directly in runes mode
1 parent 01a2117 commit 23e2d40

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,29 @@ export function serialize_get_binding(node, state) {
6565
return binding.expression;
6666
}
6767

68-
if (binding.kind === 'prop' && binding.node.name === '$$props') {
69-
// Special case for $$props which only exists in the old world
70-
return node;
71-
}
68+
if (binding.kind === 'prop') {
69+
if (binding.node.name === '$$props') {
70+
// Special case for $$props which only exists in the old world
71+
// TODO this probably should have a 'prop' binding kind
72+
return node;
73+
}
7274

73-
if (
74-
binding.kind === 'prop' &&
75-
!(state.analysis.immutable ? binding.reassigned : binding.mutated) &&
76-
!binding.initial &&
77-
!state.analysis.accessors
78-
) {
79-
return b.call(node);
75+
if (
76+
state.analysis.runes &&
77+
!state.analysis.accessors &&
78+
!binding.reassigned &&
79+
!binding.initial
80+
) {
81+
return b.member(b.id('$$props'), node);
82+
}
83+
84+
if (
85+
!(state.analysis.immutable ? binding.reassigned : binding.mutated) &&
86+
!binding.initial &&
87+
!state.analysis.accessors
88+
) {
89+
return b.call(node);
90+
}
8091
}
8192

8293
if (binding.kind === 'legacy_reactive_import') {

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

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -165,36 +165,27 @@ export const javascript_visitors_runes = {
165165

166166
for (const property of declarator.id.properties) {
167167
if (property.type === 'Property') {
168-
assert.ok(property.key.type === 'Identifier' || property.key.type === 'Literal');
169-
let name;
170-
if (property.key.type === 'Identifier') {
171-
name = property.key.name;
172-
} else if (property.key.type === 'Literal') {
173-
name = /** @type {string} */ (property.key.value).toString();
174-
} else {
175-
throw new Error('unreachable');
176-
}
168+
const key = /** @type {import('estree').Identifier | import('estree').Literal} */ (
169+
property.key
170+
);
171+
const name = key.type === 'Identifier' ? key.name : /** @type {string} */ (key.value);
177172

178173
seen.push(name);
179174

180-
if (property.value.type === 'Identifier') {
181-
const binding = /** @type {import('#compiler').Binding} */ (
182-
state.scope.get(property.value.name)
183-
);
184-
declarations.push(
185-
b.declarator(property.value, get_props_method(binding, state, name))
186-
);
187-
} else if (property.value.type === 'AssignmentPattern') {
188-
assert.equal(property.value.left.type, 'Identifier');
189-
const binding = /** @type {import('#compiler').Binding} */ (
190-
state.scope.get(property.value.left.name)
191-
);
192-
declarations.push(
193-
b.declarator(
194-
property.value.left,
195-
get_props_method(binding, state, name, property.value.right)
196-
)
197-
);
175+
let id = property.value;
176+
let initial = undefined;
177+
178+
if (property.value.type === 'AssignmentPattern') {
179+
id = property.value.left;
180+
initial = property.value.right;
181+
}
182+
183+
assert.equal(id.type, 'Identifier');
184+
185+
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
186+
187+
if (binding.reassigned || state.analysis.accessors || initial) {
188+
declarations.push(b.declarator(id, get_props_method(binding, state, name, initial)));
198189
}
199190
} else {
200191
// RestElement

0 commit comments

Comments
 (0)