Skip to content

Commit 9d01f02

Browse files
committed
chore: refactor migration composition logic
1 parent 35ebbe6 commit 9d01f02

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

packages/svelte/src/compiler/migrate/index.js

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,42 @@ export function migrate(source) {
102102
state = { ...state, scope: analysis.template.scope };
103103
walk(parsed.fragment, state, template);
104104

105+
let insertion_point = parsed.instance
106+
? /** @type {number} */ (parsed.instance.content.start)
107+
: 0;
108+
109+
const need_script =
110+
state.legacy_imports.size > 0 ||
111+
state.derived_components.size > 0 ||
112+
state.script_insertions.size > 0 ||
113+
state.props.length > 0 ||
114+
analysis.uses_rest_props ||
115+
analysis.uses_props;
116+
117+
if (!parsed.instance && need_script) {
118+
const to_prepend = '<script>';
119+
str.appendRight(0, to_prepend);
120+
}
121+
105122
const specifiers = [...state.legacy_imports].map((imported) => {
106123
const local = state.names[imported];
107124
return imported === local ? imported : `${imported} as ${local}`;
108125
});
109126

110-
const legacy_import = `import { ${specifiers.join(', ')} } from 'svelte/legacy';`;
111-
let added_legacy_import = false;
127+
const legacy_import = `import { ${specifiers.join(', ')} } from 'svelte/legacy';\n`;
128+
129+
if (state.legacy_imports.size > 0) {
130+
str.appendRight(insertion_point, `\n${indent}${legacy_import}`);
131+
}
132+
133+
if (state.script_insertions.size > 0) {
134+
str.appendRight(
135+
insertion_point,
136+
`\n${indent}${[...state.script_insertions].join(`\n${indent}`)}`
137+
);
138+
}
139+
140+
insertion_point = state.props_insertion_point;
112141

113142
if (state.props.length > 0 || analysis.uses_rest_props || analysis.uses_props) {
114143
const has_many_props = state.props.length > 3;
@@ -138,7 +167,7 @@ export function migrate(source) {
138167

139168
if (state.has_props_rune) {
140169
// some render tags or forwarded event attributes to add
141-
str.appendRight(state.props_insertion_point, ` ${props},`);
170+
str.appendRight(insertion_point, ` ${props},`);
142171
} else {
143172
const uses_ts = parsed.instance?.attributes.some(
144173
(attr) => attr.name === 'lang' && /** @type {any} */ (attr).value[0].data === 'ts'
@@ -177,20 +206,8 @@ export function migrate(source) {
177206
props_declaration = `${props_declaration} = $props();`;
178207
}
179208

180-
if (parsed.instance) {
181-
props_declaration = `\n${indent}${props_declaration}`;
182-
str.appendRight(state.props_insertion_point, props_declaration);
183-
} else {
184-
const imports = state.legacy_imports.size > 0 ? `${indent}${legacy_import}\n` : '';
185-
const script_insertions =
186-
state.script_insertions.size > 0
187-
? `\n${indent}${[...state.script_insertions].join(`\n${indent}`)}`
188-
: '';
189-
str.prepend(
190-
`<script>\n${imports}${indent}${props_declaration}${script_insertions}\n</script>\n\n`
191-
);
192-
added_legacy_import = true;
193-
}
209+
props_declaration = `\n${indent}${props_declaration}`;
210+
str.appendRight(insertion_point, props_declaration);
194211
}
195212
}
196213

@@ -235,24 +252,10 @@ export function migrate(source) {
235252
}
236253
}
237254

238-
if (state.legacy_imports.size > 0 && !added_legacy_import) {
239-
const script_insertions =
240-
state.script_insertions.size > 0
241-
? `\n${indent}${[...state.script_insertions].join(`\n${indent}`)}`
242-
: '';
243-
244-
if (parsed.instance) {
245-
str.appendRight(
246-
/** @type {number} */ (parsed.instance.content.start),
247-
`\n${indent}${legacy_import}${script_insertions}\n`
248-
);
249-
} else {
250-
str.prepend(
251-
`<script>\n${indent}${legacy_import}\n${indent}${script_insertions}\n</script>\n\n`
252-
);
253-
}
255+
256+
if (!parsed.instance && need_script) {
257+
str.appendRight(insertion_point, '\n</script>\n\n');
254258
}
255-
256259
return { code: str.toString() };
257260
} catch (e) {
258261
// eslint-disable-next-line no-console
@@ -843,22 +846,6 @@ function get_node_range(source, node) {
843846
return { start, end };
844847
}
845848

846-
/**
847-
* @param {AST.OnDirective} last
848-
* @param {State} state
849-
*/
850-
function generate_event_name(last, state) {
851-
const scope =
852-
(last.expression && state.analysis.template.scopes.get(last.expression)) || state.scope;
853-
854-
let name = 'event';
855-
if (!scope.get(name)) return name;
856-
857-
let i = 1;
858-
while (scope.get(`${name}${i}`)) i += 1;
859-
return `${name}${i}`;
860-
}
861-
862849
/**
863850
* @param {Identifier} node
864851
* @param {State} state

packages/svelte/tests/migrate/samples/event-handlers-with-alias/output.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
import { createBubbler as createBubbler_1, handlers as handlers_1, preventDefault as preventDefault_1, stopPropagation as stopPropagation_1, stopImmediatePropagation as stopImmediatePropagation_1, self as self_1, trusted as trusted_1, once as once_1, passive as passive_1, nonpassive as nonpassive_1 } from 'svelte/legacy';
3-
const bubble_1 = createBubbler_1();
43
4+
const bubble_1 = createBubbler_1();
55
let handlers;
66
let stopPropagation;
77
let preventDefault;
@@ -86,4 +86,4 @@
8686
})}>click me</button
8787
>
8888
<button onclick={preventDefault_1(() => (count += 1))}>click me</button>
89-
</div>
89+
</div>

packages/svelte/tests/migrate/samples/event-handlers/output.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import { createBubbler, handlers, preventDefault, stopPropagation, stopImmediatePropagation, self, trusted, once, passive, nonpassive } from 'svelte/legacy';
3-
3+
44
const bubble = createBubbler();
55
</script>
66

@@ -78,4 +78,4 @@
7878
})}>click me</button
7979
>
8080
<button onclick={preventDefault(() => (count += 1))}>click me</button>
81-
</div>
81+
</div>

0 commit comments

Comments
 (0)