Skip to content

Commit

Permalink
[feat] do uppercase http verbs migration on the fly
Browse files Browse the repository at this point in the history
Small QoL improvement for anyone who is behind a few versions and didn't do that migration yet.
  • Loading branch information
dummdidumm committed Aug 29, 2022
1 parent d0b30f4 commit c1423dd
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-clouds-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-migrate': patch
---

[feat] do uppercase http verbs migration on the fly
12 changes: 11 additions & 1 deletion packages/migrate/migrations/routes/migrate_page_server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
parse,
rewrite_returns,
rewrite_type,
unwrap
unwrap,
uppercase_migration
} from '../utils.js';
import * as TASKS from '../tasks.js';

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

// If user didn't do the uppercase verbs migration yet, do it here on the fly.
const uppercased = uppercase_migration(methods, file);
if (!uppercased) {
return give_up + content;
} else if (uppercased !== content) {
return migrate_page_server(uppercased, filename);
}

const non_get_methods = methods.filter((name) => name !== 'GET');

const unmigrated = new Set(methods);
Expand Down
22 changes: 22 additions & 0 deletions packages/migrate/migrations/routes/migrate_page_server/samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,25 @@ export function load() {
return ;
}
```

## A get function that only returns body

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

```js after
/** @type {import('./$types').PageServerLoad} */
export function load() {
return {
a: 1
};
}
```
14 changes: 13 additions & 1 deletion packages/migrate/migrations/routes/migrate_server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ts from 'typescript';
import {
automigration,
dedent,
uppercase_migration,
error,
get_function_node,
get_object_nodes,
Expand All @@ -18,7 +19,10 @@ import * as TASKS from '../tasks.js';

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

/** @param {string} content */
/**
* @param {string} content
* @returns {string}
*/
export function migrate_server(content) {
const file = parse(content);
if (!file) return give_up + content;
Expand All @@ -29,6 +33,14 @@ export function migrate_server(content) {
file.exports.map.has(name)
);

// If user didn't do the uppercase verbs migration yet, do it here on the fly.
const uppercased = uppercase_migration(methods, file);
if (!uppercased) {
return give_up + content;
} else if (uppercased !== content) {
return migrate_server(uppercased);
}

const unmigrated = new Set(methods);

/** @type {Map<string, string>} */
Expand Down
22 changes: 22 additions & 0 deletions packages/migrate/migrations/routes/migrate_server/samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,25 @@ export function GET() {
return;
}
```

## A GET function that returns a JSON object

```js before
export function get() {
return {
body: {
a: 1
}
};
}
```

```js after
import { json } from '@sveltejs/kit';

export function GET() {
return json({
a: 1
});
}
```
35 changes: 35 additions & 0 deletions packages/migrate/migrations/routes/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,38 @@ export function rewrite_type(node, code, old_type, new_type) {
code.overwrite(start, start + old_type.length, new_type);
}
}

/**
* Does the HTTP verbs uppercase migration if it didn't happen yet. If a string
* is returned, the migration was done or wasn't needed. If undefined is returned,
* the migration is needed but couldn't be done.
*
* @param {string[]} methods
* @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)
);

if (old_methods.length && !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;
}
file.code.overwrite(
fn.name.getStart(),
fn.name.getEnd(),
method === 'del' ? 'DELETE' : method.toUpperCase()
);
}
}
}

return file.code.toString();
}

0 comments on commit c1423dd

Please sign in to comment.