Skip to content

Commit

Permalink
fix: add lang="ts" attribute during migration if needed (#14222)
Browse files Browse the repository at this point in the history
* fix: add `lang="ts"` attribute during migration if needed

fixes #14219

* fix
  • Loading branch information
dummdidumm authored Nov 8, 2024
1 parent 438de04 commit 31e6bbb
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-clocks-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: add `lang="ts"` attribute during migration if needed
10 changes: 9 additions & 1 deletion packages/svelte/src/compiler/migrate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,12 @@ export function migrate(source, { filename, use_ts } = {}) {
analysis.uses_props ||
state.has_svelte_self;

const need_ts_tag =
state.uses_ts &&
(!parsed.instance || !parsed.instance.attributes.some((attr) => attr.name === 'lang'));

if (!parsed.instance && need_script) {
str.appendRight(0, '<script>');
str.appendRight(0, need_ts_tag ? '<script lang="ts">' : '<script>');
}

if (state.has_svelte_self && filename) {
Expand Down Expand Up @@ -326,6 +330,10 @@ export function migrate(source, { filename, use_ts } = {}) {
props_declaration = `\n${indent}${props_declaration}`;
str.appendRight(insertion_point, props_declaration);
}

if (parsed.instance && need_ts_tag) {
str.appendRight(parsed.instance.start + '<script'.length, ' lang="ts"');
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
use_ts: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
// script tag but no lang="ts", because for example only imports present
</script>

<slot />
10 changes: 10 additions & 0 deletions packages/svelte/tests/migrate/samples/slot-use_ts-2/output.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
interface Props {
children?: import('svelte').Snippet;
}
let { children }: Props = $props();
// script tag but no lang="ts", because for example only imports present
</script>

{@render children?.()}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
use_ts: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/** @type {ShouldNotUseTSBecauseImUsingJsDoc} */
export let data;
</script>

<slot />
13 changes: 13 additions & 0 deletions packages/svelte/tests/migrate/samples/slot-use_ts-3/output.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
/**
* @typedef {Object} Props
* @property {ShouldNotUseTSBecauseImUsingJsDoc} data
* @property {import('svelte').Snippet} [children]
*/
/** @type {Props} */
let { data, children } = $props();
</script>

{@render children?.()}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script lang="ts">
interface Props {
children?: import('svelte').Snippet;
}
Expand Down

0 comments on commit 31e6bbb

Please sign in to comment.