Skip to content

Commit 31e6bbb

Browse files
authored
fix: add lang="ts" attribute during migration if needed (#14222)
* fix: add `lang="ts"` attribute during migration if needed fixes #14219 * fix
1 parent 438de04 commit 31e6bbb

File tree

9 files changed

+59
-2
lines changed

9 files changed

+59
-2
lines changed

.changeset/seven-clocks-pull.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: add `lang="ts"` attribute during migration if needed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,12 @@ export function migrate(source, { filename, use_ts } = {}) {
207207
analysis.uses_props ||
208208
state.has_svelte_self;
209209

210+
const need_ts_tag =
211+
state.uses_ts &&
212+
(!parsed.instance || !parsed.instance.attributes.some((attr) => attr.name === 'lang'));
213+
210214
if (!parsed.instance && need_script) {
211-
str.appendRight(0, '<script>');
215+
str.appendRight(0, need_ts_tag ? '<script lang="ts">' : '<script>');
212216
}
213217

214218
if (state.has_svelte_self && filename) {
@@ -326,6 +330,10 @@ export function migrate(source, { filename, use_ts } = {}) {
326330
props_declaration = `\n${indent}${props_declaration}`;
327331
str.appendRight(insertion_point, props_declaration);
328332
}
333+
334+
if (parsed.instance && need_ts_tag) {
335+
str.appendRight(parsed.instance.start + '<script'.length, ' lang="ts"');
336+
}
329337
}
330338

331339
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
use_ts: true
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
// script tag but no lang="ts", because for example only imports present
3+
</script>
4+
5+
<slot />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
interface Props {
3+
children?: import('svelte').Snippet;
4+
}
5+
6+
let { children }: Props = $props();
7+
// script tag but no lang="ts", because for example only imports present
8+
</script>
9+
10+
{@render children?.()}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
use_ts: true
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
/** @type {ShouldNotUseTSBecauseImUsingJsDoc} */
3+
export let data;
4+
</script>
5+
6+
<slot />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
3+
/**
4+
* @typedef {Object} Props
5+
* @property {ShouldNotUseTSBecauseImUsingJsDoc} data
6+
* @property {import('svelte').Snippet} [children]
7+
*/
8+
9+
/** @type {Props} */
10+
let { data, children } = $props();
11+
</script>
12+
13+
{@render children?.()}

packages/svelte/tests/migrate/samples/slot-use_ts/output.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script>
1+
<script lang="ts">
22
interface Props {
33
children?: import('svelte').Snippet;
44
}

0 commit comments

Comments
 (0)