Skip to content

Commit c29b746

Browse files
authored
feat: migrate <svelte:element this="div"> (#11659)
1 parent 110a5a8 commit c29b746

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

.changeset/thin-years-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: migrate `<svelte:element this="div">`

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,26 @@ const template = {
427427
next();
428428
},
429429
SvelteElement(node, { state, next }) {
430+
if (node.tag.type === 'Literal') {
431+
let is_static = true;
432+
433+
let a = /** @type {number} */ (node.tag.start);
434+
let b = /** @type {number} */ (node.tag.end);
435+
let quote_mark = state.str.original[a - 1];
436+
437+
while (state.str.original[--a] !== '=') {
438+
if (state.str.original[a] === '{') {
439+
is_static = false;
440+
break;
441+
}
442+
}
443+
444+
if (is_static && state.str.original[b] === quote_mark) {
445+
state.str.prependLeft(a + 1, '{');
446+
state.str.appendRight(/** @type {number} */ (node.tag.end) + 1, '}');
447+
}
448+
}
449+
430450
handle_events(node, state);
431451
next();
432452
},

packages/svelte/src/compiler/phases/1-parse/state/element.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,13 @@ export default function tag(parser) {
280280
// TODO in 6.0, error
281281
element.tag =
282282
chunk.type === 'Text'
283-
? { type: 'Literal', value: chunk.data, raw: `'${chunk.raw}'` }
283+
? {
284+
type: 'Literal',
285+
value: chunk.data,
286+
raw: `'${chunk.raw}'`,
287+
start: chunk.start,
288+
end: chunk.end
289+
}
284290
: chunk.expression;
285291
} else {
286292
element.tag = chunk.expression;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<svelte:element this="div" />
2+
<svelte:element this='div' />
3+
<svelte:element this={"div"} />
4+
5+
<!-- we don't try to fix this bug, we just leave it as-is -->
6+
<svelte:element this="h{n}" />
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<svelte:element this={"div"} />
2+
<svelte:element this={'div'} />
3+
<svelte:element this={"div"} />
4+
5+
<!-- we don't try to fix this bug, we just leave it as-is -->
6+
<svelte:element this="h{n}" />

packages/svelte/tests/migrate/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
2121
fs.writeFileSync(`${cwd}/_actual.svelte`, actual);
2222

2323
const expected = try_read_file(`${cwd}/output.svelte`);
24-
assert.deepEqual(actual, expected);
24+
assert.deepEqual(actual.trim(), expected?.trim());
2525
}
2626
});
2727

0 commit comments

Comments
 (0)