Skip to content

Commit dda9004

Browse files
authored
Revert "[feat] Suggest props destructuring if possible (#6069)"
This reverts commit b1dafb8.
1 parent 0467aea commit dda9004

File tree

5 files changed

+10
-121
lines changed

5 files changed

+10
-121
lines changed

.changeset/ninety-snakes-melt.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/migrate/migrations/routes/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,7 @@ export async function migrate() {
148148

149149
renamed += svelte_ext;
150150

151-
const { module, main, ext } = migrate_scripts(
152-
content,
153-
bare,
154-
is_error_page,
155-
move_to_directory
156-
);
151+
const { module, main, ext } = migrate_scripts(content, is_error_page, move_to_directory);
157152

158153
if (move_to_directory) {
159154
const dir = path.dirname(renamed);

packages/migrate/migrations/routes/migrate_scripts/index.js

Lines changed: 7 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,16 @@ import {
99
except_str
1010
} from '../utils.js';
1111
import * as TASKS from '../tasks.js';
12-
import MagicString from 'magic-string';
1312

1413
/**
1514
* @param {string} content
16-
* @param {string} filename
1715
* @param {boolean} is_error
1816
* @param {boolean} moved
1917
*/
20-
export function migrate_scripts(content, filename, is_error, moved) {
18+
export function migrate_scripts(content, is_error, moved) {
2119
/** @type {string | null} */
2220
let module = null;
2321

24-
const match = /__layout(?:-([^.@]+))?/.exec(filename);
25-
const data_name = match?.[1] ? `LayoutData.${match[1]}` : match ? `LayoutData` : 'PageData';
26-
2722
let ext = '.js';
2823

2924
// instance script
@@ -36,10 +31,6 @@ export function migrate_scripts(content, filename, is_error, moved) {
3631
contents = adjust_imports(contents);
3732
}
3833

39-
if (/lang(?:uage)?=(['"])(ts|typescript)\1/.test(attrs)) {
40-
ext = '.ts';
41-
}
42-
4334
if (/context=(['"])module\1/.test(attrs)) {
4435
// special case — load is no longer supported in error
4536
if (is_error) {
@@ -51,6 +42,10 @@ export function migrate_scripts(content, filename, is_error, moved) {
5142
return `<script${attrs}>${body}</script>${whitespace}`;
5243
}
5344

45+
if (/lang(?:uage)?=(['"])(ts|typescript)\1/.test(attrs)) {
46+
ext = '.ts';
47+
}
48+
5449
module = dedent(contents.replace(/^\n/, ''));
5550

5651
const declared = find_declarations(contents);
@@ -93,22 +88,8 @@ export function migrate_scripts(content, filename, is_error, moved) {
9388
}
9489

9590
if (!is_error && /export/.test(contents)) {
96-
let prefix = `\n${indent}${error('Add data prop', TASKS.PAGE_DATA_PROP)}`;
97-
const props = get_props(contents);
98-
if (props) {
99-
prefix += `\n${comment(
100-
`${indent}Suggestion (check code before using, and possibly convert to data.X access later):\n` +
101-
`${indent}${
102-
ext === '.ts'
103-
? `import type { ${data_name} } from './$types';`
104-
: `/** @type {import('./$types').${data_name}} */`
105-
}\n` +
106-
`${indent}export let data${ext === '.ts' ? `: ${data_name}` : ''};\n` +
107-
`${indent}$: ({ ${props.join(', ')} } = data);`,
108-
indent
109-
)}`;
110-
}
111-
contents = `${prefix}\n${contents}`;
91+
contents = `\n${indent}${error('Add data prop', TASKS.PAGE_DATA_PROP)}\n${contents}`;
92+
// Possible TODO: migrate props to data.prop, or suggest $: ({propX, propY, ...} = data);
11293
}
11394

11495
return `<script${attrs}>${contents}</script>${whitespace}`;
@@ -174,74 +155,3 @@ function find_declarations(content) {
174155

175156
return declared;
176157
}
177-
178-
/**
179-
* @param {string} content
180-
*/
181-
function get_props(content) {
182-
try {
183-
const ast = ts.createSourceFile(
184-
'filename.ts',
185-
content,
186-
ts.ScriptTarget.Latest,
187-
true,
188-
ts.ScriptKind.TS
189-
);
190-
191-
const code = new MagicString(content);
192-
let give_up = false;
193-
/** @type {string[] } */
194-
let exports = [];
195-
196-
/** @param {ts.Node} node */
197-
function walk(node) {
198-
if (
199-
ts.isExportDeclaration(node) ||
200-
((ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) && hasExportKeyword(node))
201-
) {
202-
give_up = true;
203-
return;
204-
}
205-
206-
if (ts.isVariableStatement(node)) {
207-
if (hasExportKeyword(node)) {
208-
const isLet = node.declarationList.flags === ts.NodeFlags.Let;
209-
if (isLet) {
210-
ts.forEachChild(node.declarationList, (node) => {
211-
if (ts.isVariableDeclaration(node)) {
212-
if (ts.isIdentifier(node.name)) {
213-
const name = node.name.getText();
214-
if (name === 'data') {
215-
give_up = true;
216-
return;
217-
} else {
218-
exports.push(name);
219-
}
220-
} else {
221-
give_up = true;
222-
return;
223-
}
224-
}
225-
});
226-
} else {
227-
give_up = true;
228-
return;
229-
}
230-
}
231-
}
232-
}
233-
234-
ts.forEachChild(ast, walk);
235-
if (give_up) return;
236-
return exports;
237-
} catch (e) {
238-
return;
239-
}
240-
}
241-
242-
/**
243-
* @param {ts.Node} node
244-
*/
245-
export function hasExportKeyword(node) {
246-
return node.modifiers?.find((x) => x.kind == ts.SyntaxKind.ExportKeyword);
247-
}

packages/migrate/migrations/routes/migrate_scripts/index.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ for (const sample of read_samples(import.meta.url)) {
77
test(sample.description, () => {
88
const actual = migrate_scripts(
99
sample.before,
10-
sample.filename ?? 'some-page.svelte',
1110
sample.description.includes('error'),
1211
sample.description.includes('moved')
1312
);

packages/migrate/migrations/routes/migrate_scripts/samples.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,13 @@
7777
```svelte after
7878
<script>
7979
throw new Error("@migration task: Add data prop (https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292707)");
80-
// Suggestion (check code before using, and possibly convert to data.X access later):
81-
// /** @type {import('./$types').PageData} */
82-
// export let data;
83-
// $: ({ a } = data);
8480
8581
export let a;
8682
</script>
8783
```
8884

8985
## Module context with moved imports
9086

91-
> file: __layout.svelte
92-
9387
```svelte before
9488
<script context="module">
9589
import Foo from './Foo.svelte';
@@ -102,7 +96,7 @@
10296
}
10397
</script>
10498
105-
<script lang="ts">
99+
<script>
106100
export let sry;
107101
</script>
108102
@@ -123,12 +117,8 @@
123117
// }
124118
</script>
125119
126-
<script lang="ts">
120+
<script>
127121
throw new Error("@migration task: Add data prop (https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292707)");
128-
// Suggestion (check code before using, and possibly convert to data.X access later):
129-
// import type { LayoutData } from './$types';
130-
// export let data: LayoutData;
131-
// $: ({ sry } = data);
132122
133123
export let sry;
134124
</script>

0 commit comments

Comments
 (0)