Skip to content

Commit 611a280

Browse files
committed
fix
1 parent ca6edda commit 611a280

File tree

6 files changed

+27
-33
lines changed

6 files changed

+27
-33
lines changed

packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ export function CallExpression(node, context) {
9797

9898
context.state.analysis.props_id = {
9999
name: parent.id.name,
100-
metadata: null
100+
attr: null,
101+
suffix: null
101102
};
102103

103104
break;

packages/svelte/src/compiler/phases/2-analyze/visitors/Fragment.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ export function Fragment(node, context) {
3434

3535
// If the component has a $props.id(),
3636
// We check if the root fragment starts with a RegularElement
37-
if (context.path.length === 0 && props_id && props_id.metadata === null) {
37+
if (context.path.length === 0 && props_id) {
3838
let first_elem = search_first_elem(node);
3939

40-
/** @type {string | null} */
41-
let attr = null;
42-
/** @type {string | null} */
43-
let suffix = null;
44-
4540
if (first_elem) {
4641
// If the fragment starts with a RegularElement
4742
// We look for an attribute that starts with the $props.id()
@@ -50,14 +45,14 @@ export function Fragment(node, context) {
5045
for (const attribute of first_elem.attributes) {
5146
if (attribute.type === 'SpreadAttribute') {
5247
// reset
53-
attr = null;
54-
suffix = null;
48+
props_id.attr = null;
49+
props_id.suffix = null;
5550
} else if (
5651
attribute.type === 'Attribute' &&
5752
attribute.name.toLowerCase() !== 'class' &&
5853
attribute.name.toLowerCase() !== 'style' &&
5954
attribute.value !== true &&
60-
(attr === null || attr.length > attribute.name.length)
55+
(props_id.attr === null || props_id.attr.length > attribute.name.length)
6156
) {
6257
if (Array.isArray(attribute.value)) {
6358
const attr0 = attribute.value[0];
@@ -67,27 +62,26 @@ export function Fragment(node, context) {
6762
attr0.expression.name === props_id.name
6863
) {
6964
if (attribute.value.length === 1) {
70-
attr = attribute.name;
71-
suffix = null;
65+
props_id.attr = attribute.name;
66+
props_id.suffix = null;
7267
} else if (
7368
attribute.value[1].type === 'Text' &&
7469
!'0123456789'.includes(attribute.value[1].data[0])
7570
) {
76-
attr = attribute.name;
77-
suffix = attribute.value[1].data[0];
71+
props_id.attr = attribute.name;
72+
props_id.suffix = attribute.value[1].data[0];
7873
}
7974
}
8075
} else if (
8176
attribute.value.expression.type === 'Identifier' &&
8277
attribute.value.expression.name === props_id.name
8378
) {
84-
attr = attribute.name;
85-
suffix = null;
79+
props_id.attr = attribute.name;
80+
props_id.suffix = null;
8681
}
8782
}
8883
}
8984
}
90-
props_id.metadata = { attr, suffix };
9185
}
9286

9387
node.nodes.forEach((node) => context.visit(node, { ...context.state }));

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ export function validate_assignment(node, argument, state) {
2525
e.constant_assignment(node, 'derived state');
2626
}
2727

28-
if (binding?.node === state.analysis.props_id) {
28+
if (
29+
state.analysis.props_id &&
30+
binding?.node?.type === 'Identifier' &&
31+
binding?.node?.name === state.analysis.props_id.name
32+
) {
2933
e.constant_assignment(node, '$props.id()');
3034
}
3135

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,18 +563,15 @@ export function client_component(analysis, options) {
563563
}
564564

565565
if (analysis.props_id) {
566-
// need to be placed on first line of the component for hydration
567566
const args = [];
568-
const metadata = analysis.props_id.metadata;
569-
if (metadata) {
570-
if (metadata.attr) {
571-
args.push(b.literal(metadata.attr));
572-
if (metadata.suffix) {
573-
args.push(b.literal(metadata.suffix));
574-
}
567+
if (analysis.props_id.attr) {
568+
args.push(b.literal(analysis.props_id.attr));
569+
if (analysis.props_id.suffix) {
570+
args.push(b.literal(analysis.props_id.suffix));
575571
}
576572
}
577573

574+
// need to be placed on first line of the component for hydration
578575
component_block.body.unshift(
579576
b.const(b.id(analysis.props_id.name), b.call('$.props_id', ...args))
580577
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export function server_component(analysis, options) {
249249
component_block.body.unshift(
250250
b.const(
251251
analysis.props_id.name,
252-
b.call('$.props_id', b.id('$$payload'), analysis.props_id.metadata?.attr ? b.false : false)
252+
b.call('$.props_id', b.id('$$payload'), analysis.props_id?.attr ? b.false : false)
253253
)
254254
);
255255
}

packages/svelte/src/compiler/phases/types.d.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ export interface ComponentAnalysis extends Analysis {
4848
props_id: {
4949
/** The variable name */
5050
name: string;
51-
metadata: {
52-
/** The name of the attribute from the first RegularElement */
53-
attr: string | null;
54-
/** Any suffix separator on the attribute */
55-
suffix: string | null;
56-
} | null;
51+
/** The name of the attribute from the first RegularElement */
52+
attr: string | null;
53+
/** Any suffix separator on the attribute */
54+
suffix: string | null;
5755
} | null;
5856
/** Whether the component uses `$$restProps` */
5957
uses_rest_props: boolean;

0 commit comments

Comments
 (0)