Skip to content

Commit ec57acd

Browse files
committed
fix: improve code generation
1 parent a1a3e81 commit ec57acd

File tree

86 files changed

+118
-94
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+118
-94
lines changed

.changeset/odd-buckets-lie.md

Lines changed: 5 additions & 0 deletions

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,10 @@ function is_expression_async(expression) {
217217
* @param {import('estree').AssignmentExpression} node
218218
* @param {import('zimmerframe').Context<import('#compiler').SvelteNode, State>} context
219219
* @param {() => any} fallback
220+
* @param {{skip_proxy_and_freeze?: boolean}} [options]
220221
* @returns {import('estree').Expression}
221222
*/
222-
export function serialize_set_binding(node, context, fallback) {
223+
export function serialize_set_binding(node, context, fallback, options) {
223224
const { state, visit } = context;
224225

225226
if (
@@ -242,7 +243,7 @@ export function serialize_set_binding(node, context, fallback) {
242243
const value = path.expression?.(b.id(tmp_id));
243244
const assignment = b.assignment('=', path.node, value);
244245
original_assignments.push(assignment);
245-
assignments.push(serialize_set_binding(assignment, context, () => assignment));
246+
assignments.push(serialize_set_binding(assignment, context, () => assignment, options));
246247
}
247248

248249
if (assignments.every((assignment, i) => assignment === original_assignments[i])) {
@@ -288,7 +289,11 @@ export function serialize_set_binding(node, context, fallback) {
288289
if (private_state !== undefined) {
289290
if (state.in_constructor) {
290291
// See if we should wrap value in $.proxy
291-
if (context.state.analysis.runes && should_proxy_or_freeze(value)) {
292+
if (
293+
context.state.analysis.runes &&
294+
!options?.skip_proxy_and_freeze &&
295+
should_proxy_or_freeze(value)
296+
) {
292297
const assignment = fallback();
293298
if (assignment.type === 'AssignmentExpression') {
294299
assignment.right =
@@ -302,7 +307,9 @@ export function serialize_set_binding(node, context, fallback) {
302307
return b.call(
303308
'$.set',
304309
left,
305-
context.state.analysis.runes && should_proxy_or_freeze(value)
310+
context.state.analysis.runes &&
311+
!options?.skip_proxy_and_freeze &&
312+
should_proxy_or_freeze(value)
306313
? private_state.kind === 'frozen_state'
307314
? b.call('$.freeze', value)
308315
: b.call('$.proxy', value)
@@ -321,6 +328,7 @@ export function serialize_set_binding(node, context, fallback) {
321328
if (
322329
context.state.analysis.runes &&
323330
public_state !== undefined &&
331+
!options?.skip_proxy_and_freeze &&
324332
should_proxy_or_freeze(value)
325333
) {
326334
const assignment = fallback();
@@ -387,15 +395,19 @@ export function serialize_set_binding(node, context, fallback) {
387395
return b.call(
388396
'$.set',
389397
b.id(left_name),
390-
context.state.analysis.runes && should_proxy_or_freeze(value)
398+
context.state.analysis.runes &&
399+
!options?.skip_proxy_and_freeze &&
400+
should_proxy_or_freeze(value)
391401
? b.call('$.proxy', value)
392402
: value
393403
);
394404
} else if (binding.kind === 'frozen_state') {
395405
return b.call(
396406
'$.set',
397407
b.id(left_name),
398-
context.state.analysis.runes && should_proxy_or_freeze(value)
408+
context.state.analysis.runes &&
409+
!options?.skip_proxy_and_freeze &&
410+
should_proxy_or_freeze(value)
399411
? b.call('$.freeze', value)
400412
: value
401413
);
@@ -624,6 +636,5 @@ export function should_proxy_or_freeze(node) {
624636
) {
625637
return false;
626638
}
627-
628639
return true;
629640
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2604,7 +2604,10 @@ export const template_visitors = {
26042604
serialize_set_binding(
26052605
assignment,
26062606
context,
2607-
() => /** @type {import('estree').Expression} */ (context.visit(assignment))
2607+
() => /** @type {import('estree').Expression} */ (context.visit(assignment)),
2608+
{
2609+
skip_proxy_and_freeze: true,
2610+
}
26082611
)
26092612
);
26102613

packages/svelte/tests/parser-legacy/samples/action-duplicate/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
}
3232
]
3333
}
34-
}
34+
}

packages/svelte/tests/parser-legacy/samples/action-with-call/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@
7373
}
7474
]
7575
}
76-
}
76+
}

packages/svelte/tests/parser-legacy/samples/action-with-identifier/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@
3838
}
3939
]
4040
}
41-
}
41+
}

packages/svelte/tests/parser-legacy/samples/action-with-literal/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
}
4040
]
4141
}
42-
}
42+
}

packages/svelte/tests/parser-legacy/samples/action/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
}
2424
]
2525
}
26-
}
26+
}

packages/svelte/tests/parser-legacy/samples/animation/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@
7676
}
7777
]
7878
}
79-
}
79+
}

packages/svelte/tests/parser-legacy/samples/attribute-class-directive/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@
3838
}
3939
]
4040
}
41-
}
41+
}

0 commit comments

Comments
 (0)