@@ -9,21 +9,16 @@ import {
99 except_str
1010} from '../utils.js' ;
1111import * 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 = / _ _ l a y o u t (?: - ( [ ^ . @ ] + ) ) ? / . 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 ( / l a n g (?: u a g e ) ? = ( [ ' " ] ) ( t s | t y p e s c r i p t ) \1/ . test ( attrs ) ) {
40- ext = '.ts' ;
41- }
42-
4334 if ( / c o n t e x t = ( [ ' " ] ) m o d u l e \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 ( / l a n g (?: u a g e ) ? = ( [ ' " ] ) ( t s | t y p e s c r i p t ) \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 && / e x p o r t / . 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- }
0 commit comments