import type { NodePath } from '@babel/traverse' ; import * as t from '@babel/types' ; import type { Metadata } from '../types' ; export const hoistSheet = ( sheet , meta ) : t . Identifier => { if ( meta . state . sheets [ sheet ] ) { return meta . state . sheets [ sheet ] ; } const sheetIdentifier = meta . parentPath . scope . generateUidIdentifier ( '' ) ; const parent = meta . parentPath . findParent ( ( path ) => path . isProgram ( ) ) ; const parentBody = parent && ( parent . get ( 'body' ) as NodePath [ ] ) ; const path = parentBody && parentBody . filter ( ( path ) => ! path . isImportDeclaration ( ) ) [ 0 ] ; if ( path ) { const kind = 'const' ; const newVariable = t . variableDeclarator ( sheetIdentifier , t . stringLiteral ( sheet ) ) ; path . insertBefore ( t . variableDeclaration ( kind , [ newVariable ] ) ) . forEach ( ( newVariable ) => { meta . parentPath . scope . registerBinding ( kind , newVariable as NodePath < t . Node > ) ; } ) ; } meta . state . sheets [ sheet ] = sheetIdentifier ; return sheetIdentifier ; } ;