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

chore: better cookie migration #11273

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function load({ cookies }) {
}
```

`svelte-migrate` will add comments highlighting the locations that need to be adjusted.
`svelte-migrate` will add the `path` option automatically where possible using `'.'` as the `path` value (which reflects the previous default behavior), but it's a good opportunity to review these locations and adjust them where it makes sense.

## Top-level promises are no longer awaited

Expand Down
26 changes: 20 additions & 6 deletions packages/migrate/migrations/sveltekit-2/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,26 @@ function add_cookie_note(file_path, source) {
continue;
}

calls.push(() =>
call.replaceWithText((writer) => {
writer.setIndentationLevel(0); // prevent ts-morph from being unhelpful and adding its own indentation
writer.write('/* @migration task: add path argument */ ' + call.getText());
})
);
if (options_arg) {
const text = source.getText();
const indent = text.substring(text.lastIndexOf('\n', call.getStart()) + 1, call.getStart());
calls.push(() => {
// Additional hoop-jumping necessary to not mix tabs and spaces
options_arg.addPropertyAssignment({
name: 'path',
initializer: "'.'",
leadingTrivia: (writer) => {
writer.setIndentationLevel(0);
},
trailingTrivia: (writer) => {
writer.setIndentationLevel(0);
}
});
call.formatText({ convertTabsToSpaces: !indent.includes('\t') });
});
} else {
calls.push(() => call.insertArguments(name === 'delete' ? 1 : 2, ["{ path: '.' }"]));
}
}

for (const call of calls) {
Expand Down
43 changes: 24 additions & 19 deletions packages/migrate/migrations/sveltekit-2/tsjs-samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,33 @@ throw error();
error();
```

## Notes cookie migration
## Migrates cookies

```js before
export function load({ cookies }) {
cookies.set('foo', 'bar');
}
```

```js after
export function load({ cookies }) {
/* @migration task: add path argument */ cookies.set('foo', 'bar');
}
```

## Notes cookie migration with multiple occurences

```js before
export function load({ cookies }) {
cookies.delete('foo');
cookies.delete('x');
cookies.set('x', 'y', { z: '' });
cookies.serialize('x', 'y', {});
cookies.set('x', 'y', {
z: ''
});
}
```

```js after
export function load({ cookies }) {
/* @migration task: add path argument */ cookies.delete('foo');
/* @migration task: add path argument */ cookies.set('x', 'y', { z: '' });
cookies.delete('x', { path: '.' });
cookies.set('x', 'y', {
z: '',
path: '.'
});
cookies.serialize('x', 'y', {
path: '.'
});
cookies.set('x', 'y', {
z: '',
path: '.'
});
}
```

Expand All @@ -81,12 +81,17 @@ export function load({ cookies }) {
```js before
export function load(event) {
event.cookies.set('x', 'y');
event.cookies.delete('x', { x: '' });
}
```

```js after
export function load(event) {
/* @migration task: add path argument */ event.cookies.set('x', 'y');
event.cookies.set('x', 'y', { path: '.' });
event.cookies.delete('x', {
x: '',
path: '.'
});
}
```

Expand Down
Loading