fix: allow to pass in TS preference to migration (#13929)

First half of https://github.com/sveltejs/kit/issues/12880 - the idea is for the migration script to check for a tsconfig.json and then set it to `true` if one is found
pull/14032/head
Simon H 7 days ago committed by GitHub
parent 08bc37a374
commit cbc2ca36ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: allow to pass in TS preference to migration

@ -39,10 +39,10 @@ class MigrationError extends Error {
* May throw an error if the code is too complex to migrate automatically. * May throw an error if the code is too complex to migrate automatically.
* *
* @param {string} source * @param {string} source
* @param {{filename?: string}} [options] * @param {{ filename?: string, use_ts?: boolean }} [options]
* @returns {{ code: string; }} * @returns {{ code: string; }}
*/ */
export function migrate(source, { filename } = {}) { export function migrate(source, { filename, use_ts } = {}) {
let og_source = source; let og_source = source;
try { try {
has_migration_task = false; has_migration_task = false;
@ -115,7 +115,10 @@ export function migrate(source, { filename } = {}) {
derived_components: new Map(), derived_components: new Map(),
derived_labeled_statements: new Set(), derived_labeled_statements: new Set(),
has_svelte_self: false, has_svelte_self: false,
uses_ts: !!parsed.instance?.attributes.some( uses_ts:
// Some people could use jsdoc but have a tsconfig.json, so double-check file for jsdoc indicators
(use_ts && !source.includes('@type {')) ||
!!parsed.instance?.attributes.some(
(attr) => attr.name === 'lang' && /** @type {any} */ (attr).value[0].data === 'ts' (attr) => attr.name === 'lang' && /** @type {any} */ (attr).value[0].data === 'ts'
) )
}; };

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
use_ts: true
});

@ -0,0 +1,9 @@
<script>
interface Props {
children?: import('svelte').Snippet;
}
let { children }: Props = $props();
</script>
{@render children?.()}

@ -6,6 +6,7 @@ import { suite, type BaseTest } from '../suite.js';
interface ParserTest extends BaseTest { interface ParserTest extends BaseTest {
skip_filename?: boolean; skip_filename?: boolean;
use_ts?: boolean;
logs?: any[]; logs?: any[];
errors?: any[]; errors?: any[];
} }
@ -32,7 +33,8 @@ const { test, run } = suite<ParserTest>(async (config, cwd) => {
} }
const actual = migrate(input, { const actual = migrate(input, {
filename: config.skip_filename ? undefined : `output.svelte` filename: config.skip_filename ? undefined : `output.svelte`,
use_ts: config.use_ts
}).code; }).code;
if (config.logs) { if (config.logs) {

@ -1290,8 +1290,9 @@ declare module 'svelte/compiler' {
* May throw an error if the code is too complex to migrate automatically. * May throw an error if the code is too complex to migrate automatically.
* *
* */ * */
export function migrate(source: string, { filename }?: { export function migrate(source: string, { filename, use_ts }?: {
filename?: string; filename?: string;
use_ts?: boolean;
} | undefined): { } | undefined): {
code: string; code: string;
}; };

Loading…
Cancel
Save