From 5b9df52a2d359def375262338bf4750d9377b897 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 12 Dec 2023 15:21:51 +0100 Subject: [PATCH 1/2] chore: better cookie migration '.' is equivalent to the current default behavior, therefore use that instead of just adding a comment --- .../30-migrating-to-sveltekit-2.md | 2 +- .../migrate/migrations/sveltekit-2/migrate.js | 26 ++++++++--- .../migrations/sveltekit-2/tsjs-samples.md | 43 +++++++++++-------- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md b/documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md index 6cda9848e788..7f0aece76b66 100644 --- a/documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md +++ b/documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md @@ -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 diff --git a/packages/migrate/migrations/sveltekit-2/migrate.js b/packages/migrate/migrations/sveltekit-2/migrate.js index e956baa40601..6aba4c24de24 100644 --- a/packages/migrate/migrations/sveltekit-2/migrate.js +++ b/packages/migrate/migrations/sveltekit-2/migrate.js @@ -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) { diff --git a/packages/migrate/migrations/sveltekit-2/tsjs-samples.md b/packages/migrate/migrations/sveltekit-2/tsjs-samples.md index 499808ad7b24..968f567b63ae 100644 --- a/packages/migrate/migrations/sveltekit-2/tsjs-samples.md +++ b/packages/migrate/migrations/sveltekit-2/tsjs-samples.md @@ -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: '.' + }); } ``` @@ -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: '.' + }); } ``` From 0882cb5d8724b9ea62f691909b5511c135759ce5 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 12 Dec 2023 15:29:57 +0100 Subject: [PATCH 2/2] this lint rule is annoying --- packages/migrate/migrations/sveltekit-2/migrate.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/migrate/migrations/sveltekit-2/migrate.js b/packages/migrate/migrations/sveltekit-2/migrate.js index 6aba4c24de24..f4ddd34f42d3 100644 --- a/packages/migrate/migrations/sveltekit-2/migrate.js +++ b/packages/migrate/migrations/sveltekit-2/migrate.js @@ -208,7 +208,7 @@ function add_cookie_note(file_path, source) { // Additional hoop-jumping necessary to not mix tabs and spaces options_arg.addPropertyAssignment({ name: 'path', - initializer: `'.'`, + initializer: "'.'", leadingTrivia: (writer) => { writer.setIndentationLevel(0); }, @@ -219,7 +219,7 @@ function add_cookie_note(file_path, source) { call.formatText({ convertTabsToSpaces: !indent.includes('\t') }); }); } else { - calls.push(() => call.insertArguments(name === 'delete' ? 1 : 2, [`{ path: '.' }`])); + calls.push(() => call.insertArguments(name === 'delete' ? 1 : 2, ["{ path: '.' }"])); } }