11import type {
2- BlockStatement , CallExpression , File , FunctionExpression , ObjectProperty , VariableDeclaration ,
2+ BlockStatement , CallExpression , File , FunctionExpression , Node , ObjectProperty , VariableDeclaration ,
33} from '@babel/types'
44import type MagicString from 'magic-string'
55import type { ParseResult } from '@babel/parser'
@@ -17,33 +17,35 @@ const getRenderFnStart = (ast: ParseResult<File>): number => {
1717 && node . declarations [ 0 ] . id . type === 'Identifier'
1818 && node . declarations [ 0 ] . id . name === 'render' ,
1919 )
20- const start = ( ( ( renderFn ?. declarations [ 0 ] . init as FunctionExpression ) . body ) as BlockStatement ) . start
21- if ( start === null )
20+ const start = ( ( ( renderFn ?. declarations [ 0 ] . init as FunctionExpression ) ? .body ) as BlockStatement ) ? .start
21+ if ( start === null || start === undefined )
2222 throw new Error ( '[unplugin-vue-components:directive] Cannot find render function position.' )
2323 return start + 1
2424}
2525
2626export default async function resolveVue2 ( code : string , s : MagicString ) : Promise < ResolveResult [ ] > {
27- if ( ! isPackageExists ( '@babel/parser' ) || ! isPackageExists ( '@babel/traverse' ) )
28- throw new Error ( '[unplugin-vue-components:directive] To use Vue 2 directive you will need to install Babel first: "npm install -D @babel/parser @babel/traverse "' )
27+ if ( ! isPackageExists ( '@babel/parser' ) )
28+ throw new Error ( '[unplugin-vue-components:directive] To use Vue 2 directive you will need to install Babel first: "npm install -D @babel/parser"' )
2929
30- const { parse } = await importModule ( '@babel/parser' ) as typeof import ( '@babel/parser' )
30+ const { parse } = await importModule < typeof import ( '@babel/parser' ) > ( '@babel/parser' )
3131 const ast = parse ( code , {
3232 sourceType : 'module' ,
3333 } )
34+
3435 const nodes : CallExpression [ ] = [ ]
35- const { default : traverse } = await importModule ( '@babel/traverse' ) as typeof import ( '@babel/traverse' )
36- traverse ( ast , {
37- CallExpression ( path ) {
38- nodes . push ( path . node )
36+ const { walk } = await import ( 'estree-walker' )
37+ walk ( ast . program as any , {
38+ enter ( node : any ) {
39+ if ( ( node as Node ) . type === 'CallExpression' )
40+ nodes . push ( node )
3941 } ,
4042 } )
4143
4244 const results : ResolveResult [ ] = [ ]
4345 for ( const node of nodes ) {
4446 const { callee, arguments : args } = node
4547 // _c(_, {})
46- if ( callee . type !== 'Identifier' || callee . name !== '_c' || args [ 1 ] == null || args [ 1 ] . type !== 'ObjectExpression' )
48+ if ( callee . type !== 'Identifier' || callee . name !== '_c' || args [ 1 ] ? .type !== 'ObjectExpression' )
4749 continue
4850
4951 // { directives: [] }
0 commit comments