Skip to content

Commit 33ee958

Browse files
chore: refactor migration composition logic (#13424)
the code to compose together the instance was getting a bit messy because we were trying to add stuff all in the same place if there was no script or one by one if there was one. I changed this a bit by using an insertion_point variable that we can set to then insert stuff sequentially.
1 parent 868ddb8 commit 33ee958

File tree

3 files changed

+38
-54
lines changed

3 files changed

+38
-54
lines changed

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

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,40 @@ 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.script_insertions.size > 0 ||
112+
state.props.length > 0 ||
113+
analysis.uses_rest_props ||
114+
analysis.uses_props;
115+
116+
if (!parsed.instance && need_script) {
117+
str.appendRight(0, '<script>');
118+
}
119+
105120
const specifiers = [...state.legacy_imports].map((imported) => {
106121
const local = state.names[imported];
107122
return imported === local ? imported : `${imported} as ${local}`;
108123
});
109124

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

113140
if (state.props.length > 0 || analysis.uses_rest_props || analysis.uses_props) {
114141
const has_many_props = state.props.length > 3;
@@ -138,7 +165,7 @@ export function migrate(source) {
138165

139166
if (state.has_props_rune) {
140167
// some render tags or forwarded event attributes to add
141-
str.appendRight(state.props_insertion_point, ` ${props},`);
168+
str.appendRight(insertion_point, ` ${props},`);
142169
} else {
143170
const uses_ts = parsed.instance?.attributes.some(
144171
(attr) => attr.name === 'lang' && /** @type {any} */ (attr).value[0].data === 'ts'
@@ -177,20 +204,8 @@ export function migrate(source) {
177204
props_declaration = `${props_declaration} = $props();`;
178205
}
179206

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-
}
207+
props_declaration = `\n${indent}${props_declaration}`;
208+
str.appendRight(insertion_point, props_declaration);
194209
}
195210
}
196211

@@ -235,24 +250,9 @@ export function migrate(source) {
235250
}
236251
}
237252

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-
}
253+
if (!parsed.instance && need_script) {
254+
str.appendRight(insertion_point, '\n</script>\n\n');
254255
}
255-
256256
return { code: str.toString() };
257257
} catch (e) {
258258
// eslint-disable-next-line no-console
@@ -843,22 +843,6 @@ function get_node_range(source, node) {
843843
return { start, end };
844844
}
845845

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-
862846
/**
863847
* @param {Identifier} node
864848
* @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)