diff --git a/.changeset/ninety-snakes-melt.md b/.changeset/ninety-snakes-melt.md deleted file mode 100644 index 62a1b282b65f..000000000000 --- a/.changeset/ninety-snakes-melt.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'svelte-migrate': patch ---- - -Suggest props destructuring if possible diff --git a/packages/migrate/migrations/routes/index.js b/packages/migrate/migrations/routes/index.js index 0597f00c3e55..14b55a393691 100644 --- a/packages/migrate/migrations/routes/index.js +++ b/packages/migrate/migrations/routes/index.js @@ -148,12 +148,7 @@ export async function migrate() { renamed += svelte_ext; - const { module, main, ext } = migrate_scripts( - content, - bare, - is_error_page, - move_to_directory - ); + const { module, main, ext } = migrate_scripts(content, is_error_page, move_to_directory); if (move_to_directory) { const dir = path.dirname(renamed); diff --git a/packages/migrate/migrations/routes/migrate_scripts/index.js b/packages/migrate/migrations/routes/migrate_scripts/index.js index 043257c56c46..3b1ac905cc4f 100644 --- a/packages/migrate/migrations/routes/migrate_scripts/index.js +++ b/packages/migrate/migrations/routes/migrate_scripts/index.js @@ -9,21 +9,16 @@ import { except_str } from '../utils.js'; import * as TASKS from '../tasks.js'; -import MagicString from 'magic-string'; /** * @param {string} content - * @param {string} filename * @param {boolean} is_error * @param {boolean} moved */ -export function migrate_scripts(content, filename, is_error, moved) { +export function migrate_scripts(content, is_error, moved) { /** @type {string | null} */ let module = null; - const match = /__layout(?:-([^.@]+))?/.exec(filename); - const data_name = match?.[1] ? `LayoutData.${match[1]}` : match ? `LayoutData` : 'PageData'; - let ext = '.js'; // instance script @@ -36,10 +31,6 @@ export function migrate_scripts(content, filename, is_error, moved) { contents = adjust_imports(contents); } - if (/lang(?:uage)?=(['"])(ts|typescript)\1/.test(attrs)) { - ext = '.ts'; - } - if (/context=(['"])module\1/.test(attrs)) { // special case — load is no longer supported in error if (is_error) { @@ -51,6 +42,10 @@ export function migrate_scripts(content, filename, is_error, moved) { return `${body}${whitespace}`; } + if (/lang(?:uage)?=(['"])(ts|typescript)\1/.test(attrs)) { + ext = '.ts'; + } + module = dedent(contents.replace(/^\n/, '')); const declared = find_declarations(contents); @@ -93,22 +88,8 @@ export function migrate_scripts(content, filename, is_error, moved) { } if (!is_error && /export/.test(contents)) { - let prefix = `\n${indent}${error('Add data prop', TASKS.PAGE_DATA_PROP)}`; - const props = get_props(contents); - if (props) { - prefix += `\n${comment( - `${indent}Suggestion (check code before using, and possibly convert to data.X access later):\n` + - `${indent}${ - ext === '.ts' - ? `import type { ${data_name} } from './$types';` - : `/** @type {import('./$types').${data_name}} */` - }\n` + - `${indent}export let data${ext === '.ts' ? `: ${data_name}` : ''};\n` + - `${indent}$: ({ ${props.join(', ')} } = data);`, - indent - )}`; - } - contents = `${prefix}\n${contents}`; + contents = `\n${indent}${error('Add data prop', TASKS.PAGE_DATA_PROP)}\n${contents}`; + // Possible TODO: migrate props to data.prop, or suggest $: ({propX, propY, ...} = data); } return `${contents}${whitespace}`; @@ -174,74 +155,3 @@ function find_declarations(content) { return declared; } - -/** - * @param {string} content - */ -function get_props(content) { - try { - const ast = ts.createSourceFile( - 'filename.ts', - content, - ts.ScriptTarget.Latest, - true, - ts.ScriptKind.TS - ); - - const code = new MagicString(content); - let give_up = false; - /** @type {string[] } */ - let exports = []; - - /** @param {ts.Node} node */ - function walk(node) { - if ( - ts.isExportDeclaration(node) || - ((ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) && hasExportKeyword(node)) - ) { - give_up = true; - return; - } - - if (ts.isVariableStatement(node)) { - if (hasExportKeyword(node)) { - const isLet = node.declarationList.flags === ts.NodeFlags.Let; - if (isLet) { - ts.forEachChild(node.declarationList, (node) => { - if (ts.isVariableDeclaration(node)) { - if (ts.isIdentifier(node.name)) { - const name = node.name.getText(); - if (name === 'data') { - give_up = true; - return; - } else { - exports.push(name); - } - } else { - give_up = true; - return; - } - } - }); - } else { - give_up = true; - return; - } - } - } - } - - ts.forEachChild(ast, walk); - if (give_up) return; - return exports; - } catch (e) { - return; - } -} - -/** - * @param {ts.Node} node - */ -export function hasExportKeyword(node) { - return node.modifiers?.find((x) => x.kind == ts.SyntaxKind.ExportKeyword); -} diff --git a/packages/migrate/migrations/routes/migrate_scripts/index.spec.js b/packages/migrate/migrations/routes/migrate_scripts/index.spec.js index 5c51d1ec8f01..f26c1f1cdb08 100644 --- a/packages/migrate/migrations/routes/migrate_scripts/index.spec.js +++ b/packages/migrate/migrations/routes/migrate_scripts/index.spec.js @@ -7,7 +7,6 @@ for (const sample of read_samples(import.meta.url)) { test(sample.description, () => { const actual = migrate_scripts( sample.before, - sample.filename ?? 'some-page.svelte', sample.description.includes('error'), sample.description.includes('moved') ); diff --git a/packages/migrate/migrations/routes/migrate_scripts/samples.md b/packages/migrate/migrations/routes/migrate_scripts/samples.md index 986c44326f56..6f57fb1f4c6d 100644 --- a/packages/migrate/migrations/routes/migrate_scripts/samples.md +++ b/packages/migrate/migrations/routes/migrate_scripts/samples.md @@ -77,10 +77,6 @@ ```svelte after @@ -88,8 +84,6 @@ ## Module context with moved imports -> file: __layout.svelte - ```svelte before - @@ -123,12 +117,8 @@ // } -