Skip to content

Commit a9d99d7

Browse files
authored
[feat] do uppercase http verbs migration on the fly (#6371)
Small QoL improvement for anyone who is behind a few versions and didn't do that migration yet.
1 parent d03e248 commit a9d99d7

File tree

6 files changed

+108
-2
lines changed

6 files changed

+108
-2
lines changed

.changeset/chilled-clouds-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-migrate': patch
3+
---
4+
5+
[feat] do uppercase http verbs migration on the fly

packages/migrate/migrations/routes/migrate_page_server/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
parse,
1111
rewrite_returns,
1212
rewrite_type,
13-
unwrap
13+
unwrap,
14+
uppercase_migration
1415
} from '../utils.js';
1516
import * as TASKS from '../tasks.js';
1617

@@ -19,6 +20,7 @@ const give_up = `${error('Update +page.server.js', TASKS.PAGE_ENDPOINT)}\n\n`;
1920
/**
2021
* @param {string} content
2122
* @param {string} filename
23+
* @returns {string}
2224
*/
2325
export function migrate_page_server(content, filename) {
2426
const file = parse(content);
@@ -28,6 +30,14 @@ export function migrate_page_server(content, filename) {
2830
file.exports.map.has(name)
2931
);
3032

33+
// If user didn't do the uppercase verbs migration yet, do it here on the fly.
34+
const uppercased = uppercase_migration(methods, file);
35+
if (!uppercased) {
36+
return give_up + content;
37+
} else if (uppercased !== content) {
38+
return migrate_page_server(uppercased, filename);
39+
}
40+
3141
const non_get_methods = methods.filter((name) => name !== 'GET');
3242

3343
const unmigrated = new Set(methods);

packages/migrate/migrations/routes/migrate_page_server/samples.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,25 @@ export function load() {
153153
return ;
154154
}
155155
```
156+
157+
## A get function that only returns body
158+
159+
```js before
160+
/** @type {import('./$types').RequestHandler} */
161+
export function get() {
162+
return {
163+
body: {
164+
a: 1
165+
}
166+
};
167+
}
168+
```
169+
170+
```js after
171+
/** @type {import('./$types').PageServerLoad} */
172+
export function load() {
173+
return {
174+
a: 1
175+
};
176+
}
177+
```

packages/migrate/migrations/routes/migrate_server/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ts from 'typescript';
22
import {
33
automigration,
44
dedent,
5+
uppercase_migration,
56
error,
67
get_function_node,
78
get_object_nodes,
@@ -18,7 +19,10 @@ import * as TASKS from '../tasks.js';
1819

1920
const give_up = `${error('Update +server.js', TASKS.STANDALONE_ENDPOINT)}\n\n`;
2021

21-
/** @param {string} content */
22+
/**
23+
* @param {string} content
24+
* @returns {string}
25+
*/
2226
export function migrate_server(content) {
2327
const file = parse(content);
2428
if (!file) return give_up + content;
@@ -29,6 +33,14 @@ export function migrate_server(content) {
2933
file.exports.map.has(name)
3034
);
3135

36+
// If user didn't do the uppercase verbs migration yet, do it here on the fly.
37+
const uppercased = uppercase_migration(methods, file);
38+
if (!uppercased) {
39+
return give_up + content;
40+
} else if (uppercased !== content) {
41+
return migrate_server(uppercased);
42+
}
43+
3244
const unmigrated = new Set(methods);
3345

3446
/** @type {Map<string, string>} */

packages/migrate/migrations/routes/migrate_server/samples.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,25 @@ export function GET() {
188188
return;
189189
}
190190
```
191+
192+
## A GET function that returns a JSON object
193+
194+
```js before
195+
export function get() {
196+
return {
197+
body: {
198+
a: 1
199+
}
200+
};
201+
}
202+
```
203+
204+
```js after
205+
import { json } from '@sveltejs/kit';
206+
207+
export function GET() {
208+
return json({
209+
a: 1
210+
});
211+
}
212+
```

packages/migrate/migrations/routes/utils.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,38 @@ export function rewrite_type(node, code, old_type, new_type) {
494494
code.overwrite(start, start + old_type.length, new_type);
495495
}
496496
}
497+
498+
/**
499+
* Does the HTTP verbs uppercase migration if it didn't happen yet. If a string
500+
* is returned, the migration was done or wasn't needed. If undefined is returned,
501+
* the migration is needed but couldn't be done.
502+
*
503+
* @param {string[]} methods
504+
* @param {NonNullable<ReturnType<typeof parse>>} file
505+
*/
506+
export function uppercase_migration(methods, file) {
507+
const old_methods = ['get', 'post', 'put', 'patch', 'del'].filter((name) =>
508+
file.exports.map.has(name)
509+
);
510+
511+
if (old_methods.length && !methods.length) {
512+
for (const statement of file.ast.statements) {
513+
for (const method of old_methods) {
514+
const fn = get_function_node(
515+
statement,
516+
/** @type{string} */ (file.exports.map.get(method))
517+
);
518+
if (!fn?.name) {
519+
return;
520+
}
521+
file.code.overwrite(
522+
fn.name.getStart(),
523+
fn.name.getEnd(),
524+
method === 'del' ? 'DELETE' : method.toUpperCase()
525+
);
526+
}
527+
}
528+
}
529+
530+
return file.code.toString();
531+
}

0 commit comments

Comments
 (0)