Skip to content

Commit cbc2ca3

Browse files
authored
fix: allow to pass in TS preference to migration (#13929)
First half of sveltejs/kit#12880 - the idea is for the migration script to check for a tsconfig.json and then set it to `true` if one is found
1 parent 08bc37a commit cbc2ca3

File tree

7 files changed

+33
-7
lines changed

7 files changed

+33
-7
lines changed

.changeset/giant-wombats-allow.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+
fix: allow to pass in TS preference to migration

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class MigrationError extends Error {
3939
* May throw an error if the code is too complex to migrate automatically.
4040
*
4141
* @param {string} source
42-
* @param {{filename?: string}} [options]
42+
* @param {{ filename?: string, use_ts?: boolean }} [options]
4343
* @returns {{ code: string; }}
4444
*/
45-
export function migrate(source, { filename } = {}) {
45+
export function migrate(source, { filename, use_ts } = {}) {
4646
let og_source = source;
4747
try {
4848
has_migration_task = false;
@@ -115,9 +115,12 @@ export function migrate(source, { filename } = {}) {
115115
derived_components: new Map(),
116116
derived_labeled_statements: new Set(),
117117
has_svelte_self: false,
118-
uses_ts: !!parsed.instance?.attributes.some(
119-
(attr) => attr.name === 'lang' && /** @type {any} */ (attr).value[0].data === 'ts'
120-
)
118+
uses_ts:
119+
// Some people could use jsdoc but have a tsconfig.json, so double-check file for jsdoc indicators
120+
(use_ts && !source.includes('@type {')) ||
121+
!!parsed.instance?.attributes.some(
122+
(attr) => attr.name === 'lang' && /** @type {any} */ (attr).value[0].data === 'ts'
123+
)
121124
};
122125

123126
if (parsed.module) {
Lines changed: 5 additions & 0 deletions
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+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<slot />
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
interface Props {
3+
children?: import('svelte').Snippet;
4+
}
5+
6+
let { children }: Props = $props();
7+
</script>
8+
9+
{@render children?.()}

packages/svelte/tests/migrate/test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { suite, type BaseTest } from '../suite.js';
66

77
interface ParserTest extends BaseTest {
88
skip_filename?: boolean;
9+
use_ts?: boolean;
910
logs?: any[];
1011
errors?: any[];
1112
}
@@ -32,7 +33,8 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
3233
}
3334

3435
const actual = migrate(input, {
35-
filename: config.skip_filename ? undefined : `output.svelte`
36+
filename: config.skip_filename ? undefined : `output.svelte`,
37+
use_ts: config.use_ts
3638
}).code;
3739

3840
if (config.logs) {

packages/svelte/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,8 +1290,9 @@ declare module 'svelte/compiler' {
12901290
* May throw an error if the code is too complex to migrate automatically.
12911291
*
12921292
* */
1293-
export function migrate(source: string, { filename }?: {
1293+
export function migrate(source: string, { filename, use_ts }?: {
12941294
filename?: string;
1295+
use_ts?: boolean;
12951296
} | undefined): {
12961297
code: string;
12971298
};

0 commit comments

Comments
 (0)