Skip to content

Commit

Permalink
[fix] more robust uppercase migration (#7033)
Browse files Browse the repository at this point in the history
Previously, any other top level statement such as an import wrongfully failed the uppercase migration
  • Loading branch information
dummdidumm authored Sep 26, 2022
1 parent fdb166c commit 1798dad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-roses-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-migrate': patch
---

[fix] more robust uppercase migration
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,25 @@ export function load() {
## A get function that only returns body

```js before
import something from 'somewhere';

/** @type {import('./$types').RequestHandler} */
export function get() {
return {
body: {
a: 1
a: something
}
};
}
```

```js after
import something from 'somewhere';

/** @type {import('./$types').PageServerLoad} */
export function load() {
return {
a: 1
a: something
};
}
```
11 changes: 6 additions & 5 deletions packages/migrate/migrations/routes/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,28 +504,29 @@ export function rewrite_type(node, code, old_type, new_type) {
* @param {NonNullable<ReturnType<typeof parse>>} file
*/
export function uppercase_migration(methods, file) {
const old_methods = ['get', 'post', 'put', 'patch', 'del'].filter((name) =>
file.exports.map.has(name)
const old_methods = new Set(
['get', 'post', 'put', 'patch', 'del'].filter((name) => file.exports.map.has(name))
);

if (old_methods.length && !methods.length) {
if (old_methods.size && !methods.length) {
for (const statement of file.ast.statements) {
for (const method of old_methods) {
const fn = get_function_node(
statement,
/** @type{string} */ (file.exports.map.get(method))
);
if (!fn?.name) {
return;
continue;
}
file.code.overwrite(
fn.name.getStart(),
fn.name.getEnd(),
method === 'del' ? 'DELETE' : method.toUpperCase()
);
old_methods.delete(method);
}
}
}

return file.code.toString();
return old_methods.size ? undefined : file.code.toString();
}

0 comments on commit 1798dad

Please sign in to comment.