Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Migration fixes #6096

Merged
merged 4 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ;
}
```