Skip to content

Commit

Permalink
[fix] Migration fixes (#6096)
Browse files Browse the repository at this point in the history
* handle error without message

* handle GET without body (which is wrong, but people could have that)

* automigrate status 200 in load

* changeset
  • Loading branch information
dummdidumm authored Aug 20, 2022
1 parent b9d709a commit c8a6108
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-ducks-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-migrate': patch
---

Handle Error without message, handle status 200, handle missing body
22 changes: 16 additions & 6 deletions packages/migrate/migrations/routes/migrate_page_js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ export function migrate_page(content, filename) {
return; // nothing to do
}

if (keys === 'props') {
automigration(value, file.code, dedent(nodes.props.getText()));
if (
keys === 'props' ||
((keys === 'status' || keys === 'props status') &&
Number(nodes.status.getText()) === 200)
) {
automigration(value, file.code, dedent(nodes.props?.getText() || ''));
return;
}

Expand All @@ -85,10 +89,16 @@ export function migrate_page(content, filename) {
if (nodes.error) {
const message = is_string_like(nodes.error)
? nodes.error.getText()
: is_new(nodes.error, 'Error') && nodes.error.arguments[0].getText();

if (message) {
automigration(node, file.code, `throw error(${status || 500}, ${message});`);
: is_new(nodes.error, 'Error')
? /** @type {string | undefined} */ (nodes.error.arguments[0]?.getText())
: false;

if (message !== false) {
automigration(
node,
file.code,
`throw error(${status || 500}${message ? `, ${message}` : ''});`
);
imports.add('error');
return;
}
Expand Down
56 changes: 56 additions & 0 deletions packages/migrate/migrations/routes/migrate_page_js/samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,29 @@ export function load({ session }) {
}
```
## Error constructor with no arguments
```js before
export function load({ session }) {
if (!session.user?.admin) {
return {
status: 403,
error: new Error()
};
}
}
```
```js after
import { error } from '@sveltejs/kit';

export function load({ session }) {
if (!session.user?.admin) {
throw error(403);
}
}
```
## Error status with no error
```js before
Expand Down Expand Up @@ -322,3 +345,36 @@ export function load() {
return;
}
```
## A load function that returns props and status 200
```js before
export function load() {
return {
status: 200,
props: {}
};
}
```
```js after
export function load() {
return {};
}
```
## A load function that returns status 200
```js before
export function load() {
return {
status: 200
};
}
```
```js after
export function load() {
return ;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function migrate_page_server(content, filename) {
return;
}

automigration(value, file.code, dedent(nodes.body.getText()));
automigration(value, file.code, dedent(nodes.body?.getText() || ''));
});
}

Expand Down
16 changes: 16 additions & 0 deletions packages/migrate/migrations/routes/migrate_page_server/samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,19 @@ export function load() {
return;
}
```

## A function that wrongfully has no body

```js before
export function GET() {
return {
status: 200
};
}
```

```js after
export function load() {
return ;
}
```

0 comments on commit c8a6108

Please sign in to comment.