diff --git a/.changeset/wise-hotels-matter.md b/.changeset/wise-hotels-matter.md new file mode 100644 index 000000000000..9f4c23edd1c6 --- /dev/null +++ b/.changeset/wise-hotels-matter.md @@ -0,0 +1,5 @@ +--- +'svelte-migrate': patch +--- + +check load function input diff --git a/packages/migrate/migrations/routes/migrate_page_js/index.js b/packages/migrate/migrations/routes/migrate_page_js/index.js index aa791328872a..090939f3afda 100644 --- a/packages/migrate/migrations/routes/migrate_page_js/index.js +++ b/packages/migrate/migrations/routes/migrate_page_js/index.js @@ -7,6 +7,7 @@ import { get_object_nodes, is_new, is_string_like, + manual_migration, manual_return_migration, parse, rewrite_returns @@ -34,6 +35,8 @@ export function migrate_page(content) { for (const statement of file.ast.statements) { const fn = get_function_node(statement, name); if (fn) { + check_fn_param(fn, file.code); + /** @type {Set} */ const imports = new Set(); @@ -43,6 +46,10 @@ export function migrate_page(content) { if (nodes) { const keys = Object.keys(nodes).sort().join(' '); + if (keys === '') { + return; // nothing to do + } + if (keys === 'props') { automigration(expr, file.code, dedent(nodes.props.getText())); return; @@ -102,3 +109,54 @@ export function migrate_page(content) { // an error at the top of the file return give_up + content; } + +/** + * Check the load function parameter, and either adjust + * the property names, or add an error + * @param {ts.FunctionDeclaration | ts.FunctionExpression | ts.ArrowFunction} fn + * @param {import('magic-string').default} str + */ +function check_fn_param(fn, str) { + const param = fn.parameters[0]; + if (!param) { + return; + } + if (ts.isObjectBindingPattern(param.name)) { + for (const binding of param.name.elements) { + if ( + !ts.isIdentifier(binding.name) || + (binding.propertyName && !ts.isIdentifier(binding.propertyName)) + ) { + bail(true); + return; + } + const name = binding.propertyName + ? /** @type {ts.Identifier} */ (binding.propertyName).text + : binding.name.text; + if (['stuff', 'status', 'error'].includes(name)) { + bail(); + return; + } + if (name === 'props') { + if (binding.propertyName) { + bail(); + return; + } else { + str.overwrite(binding.name.getStart(), binding.name.getEnd(), 'data: props'); + } + } + } + } else { + // load(param) { .. } -> bail, we won't check what is used in the function body + bail(true); + } + + function bail(check = false) { + manual_migration( + fn, + str, + (check ? 'Check if you need to migrate' : 'Migrate') + ' the load function input', + TASKS.PAGE_LOAD + ); + } +} diff --git a/packages/migrate/migrations/routes/migrate_page_js/samples.md b/packages/migrate/migrations/routes/migrate_page_js/samples.md index 9dcb5194b2a5..95980cc70a16 100644 --- a/packages/migrate/migrations/routes/migrate_page_js/samples.md +++ b/packages/migrate/migrations/routes/migrate_page_js/samples.md @@ -235,4 +235,48 @@ export async function load({ fetch }) { throw new Error("@migration task: Migrate this return statement (https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292693)"); return await res.json(); } -``` \ No newline at end of file +``` + +## Renames props -> data, leaves unchanged alone + +```js before +export async function load({ props, params }) { + return {}; +} +``` + +```js after +export async function load({ data: props, params }) { + return {}; +} +``` + +## Errors on stuff + +```js before +export async function load({ stuff }) { + return {}; +} +``` + +```js after +throw new Error("@migration task: Migrate the load function input (https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292693)"); +export async function load({ stuff }) { + return {}; +} +``` + +## Bails on non-destructured param + +```js before +export async function load(input) { + return {}; +} +``` + +```js after +throw new Error("@migration task: Check if you need to migrate the load function input (https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292693)"); +export async function load(input) { + return {}; +} +``` diff --git a/packages/migrate/migrations/routes/utils.js b/packages/migrate/migrations/routes/utils.js index bc6a27563e25..e8ae2f9eb0b5 100644 --- a/packages/migrate/migrations/routes/utils.js +++ b/packages/migrate/migrations/routes/utils.js @@ -208,6 +208,17 @@ export function manual_return_migration(node, str, comment_nr, suggestion) { node = node.parent.parent.parent; } + manual_migration(node, str, 'Migrate this return statement', comment_nr, suggestion); +} + +/** + * @param {ts.Node} node + * @param {MagicString} str + * @param {string} message + * @param {string} comment_nr + * @param {string} [suggestion] + */ +export function manual_migration(node, str, message, comment_nr, suggestion) { const indent = indent_at_line(str.original, node.getStart()); let appended = ''; @@ -219,10 +230,7 @@ export function manual_return_migration(node, str, comment_nr, suggestion) { )}`; } - str.prependLeft( - node.getStart(), - error('Migrate this return statement', comment_nr) + appended + `\n${indent}` - ); + str.prependLeft(node.getStart(), error(message, comment_nr) + appended + `\n${indent}`); } /**