@@ -80,25 +80,23 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
8080 } ,
8181
8282 ImportDeclaration ( node , context ) {
83- const { source, specifiers } = node ;
8483 const { state } = context ;
85- const { value } = source ;
8684
87- if ( value === pkg . name ) {
88- const { currentScript } = state ;
85+ if ( node . source . value === pkg . name ) {
86+ state . componentIdentifierName = getComponentsIdentifiersNames ( node . specifiers ) ;
8987
90- state . componentIdentifierName = getComponentsIdentifiersNames ( specifiers ) ;
88+ const transformed = transformImportDeclaration ( { node , filename } ) ;
9189
92- const transformedImportDeclaration = transformImportDeclaration ( { node, filename } ) ;
93-
94- if ( currentScript !== 'module' ) {
90+ if ( state . currentScript !== 'module' ) {
9591 // NOTE: We store current node in AST walker state.
9692 // And will remove it from instance & append it to module in the "clean-up" walk after this one.
97- state . pkgImportDeclaration = transformedImportDeclaration ;
93+ state . pkgImportDeclaration = transformed ;
9894 }
9995
100- return transformedImportDeclaration ;
96+ return transformed ;
10197 }
98+
99+ return node ;
102100 } ,
103101
104102 ExportNamedDeclaration ( node , context ) {
@@ -204,33 +202,11 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
204202 instance = instance ? ( visit ( instance , state ) as SvelteAST . Script ) : null ;
205203
206204 if ( ! module ) {
207- const {
208- pkgImportDeclaration,
209- defineMetaFromExportConstMeta,
210- storiesComponentImportDeclaration,
211- } = state ;
212-
213- let body : ESTreeAST . Program [ 'body' ] = [ ] ;
214-
215- // WARN: This scope is bug prone
216-
217- if ( pkgImportDeclaration ) {
218- body . push ( pkgImportDeclaration ) ;
219- }
220-
221- if ( storiesComponentImportDeclaration ) {
222- body . push ( storiesComponentImportDeclaration ) ;
223- }
224-
225- if ( defineMetaFromExportConstMeta ) {
226- body . push ( defineMetaFromExportConstMeta ) ;
227- }
228-
229205 module = createASTScript ( {
230206 module : true ,
231207 content : {
232208 type : 'Program' ,
233- body,
209+ body : [ ] ,
234210 sourceType : 'module' ,
235211 } ,
236212 } ) ;
@@ -264,27 +240,23 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
264240 return { ...rest , content, context : scriptContext } ;
265241 } ,
266242
267- Program ( node , _context ) {
268- let { body, ...rest } = node ;
269- const { currentScript, pkgImportDeclaration } = state ;
270-
271- if ( pkgImportDeclaration && currentScript === 'instance' ) {
243+ Program ( node , context ) {
244+ if ( context . state . pkgImportDeclaration && context . state . currentScript === 'instance' ) {
272245 let instanceBody : ESTreeAST . Program [ 'body' ] = [ ] ;
273246
274- for ( const declaration of body ) {
247+ for ( const declaration of node . body ) {
275248 if ( declaration . type === 'ImportDeclaration' ) {
276- const { specifiers, source } = declaration ;
277-
278- if ( source . value === pkg . name ) {
249+ if ( declaration . source . value === pkg . name ) {
279250 continue ;
280251 }
281252
282- const { storiesComponentIdentifier } = state ;
283- const storiesComponentSpecifier = specifiers . find (
253+ const { storiesComponentIdentifier } = context . state ;
254+ const storiesComponentSpecifier = declaration . specifiers . find (
284255 ( s ) => s . local . name === storiesComponentIdentifier ?. name
285256 ) ;
286257
287258 if ( storiesComponentSpecifier ) {
259+ // NOTE: We are storing it in the state, so later we will move from instance tag to module tag
288260 state . storiesComponentImportDeclaration = declaration ;
289261 continue ;
290262 }
@@ -301,18 +273,28 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
301273 instanceBody . push ( declaration ) ;
302274 }
303275
304- body = instanceBody ;
276+ node . body = instanceBody ;
305277 }
306278
307- if ( currentScript === 'module' ) {
308- const { defineMetaFromComponentMeta } = state ;
279+ if ( state . currentScript === 'module' ) {
280+ if ( state . storiesComponentImportDeclaration ) {
281+ node . body . unshift ( state . storiesComponentImportDeclaration ) ;
282+ }
283+
284+ if ( state . pkgImportDeclaration ) {
285+ node . body . unshift ( state . pkgImportDeclaration ) ;
286+ }
287+
288+ if ( state . defineMetaFromExportConstMeta ) {
289+ node . body . push ( state . defineMetaFromExportConstMeta ) ;
290+ }
309291
310- if ( defineMetaFromComponentMeta ) {
311- body . push ( defineMetaFromComponentMeta ) ;
292+ if ( state . defineMetaFromComponentMeta ) {
293+ node . body . push ( state . defineMetaFromComponentMeta ) ;
312294 }
313295 }
314296
315- return { ...rest , body } ;
297+ return { ...node , body : node . body } ;
316298 } ,
317299
318300 Fragment ( node , context ) {
0 commit comments