1
1
import type {
2
- BlockStatement , CallExpression , File , FunctionExpression , ObjectProperty , VariableDeclaration ,
2
+ BlockStatement , CallExpression , File , FunctionExpression , Node , ObjectProperty , VariableDeclaration ,
3
3
} from '@babel/types'
4
4
import type MagicString from 'magic-string'
5
5
import type { ParseResult } from '@babel/parser'
@@ -17,33 +17,35 @@ const getRenderFnStart = (ast: ParseResult<File>): number => {
17
17
&& node . declarations [ 0 ] . id . type === 'Identifier'
18
18
&& node . declarations [ 0 ] . id . name === 'render' ,
19
19
)
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 )
22
22
throw new Error ( '[unplugin-vue-components:directive] Cannot find render function position.' )
23
23
return start + 1
24
24
}
25
25
26
26
export 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"' )
29
29
30
- const { parse } = await importModule ( '@babel/parser' ) as typeof import ( '@babel/parser' )
30
+ const { parse } = await importModule < typeof import ( '@babel/parser' ) > ( '@babel/parser' )
31
31
const ast = parse ( code , {
32
32
sourceType : 'module' ,
33
33
} )
34
+
34
35
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 )
39
41
} ,
40
42
} )
41
43
42
44
const results : ResolveResult [ ] = [ ]
43
45
for ( const node of nodes ) {
44
46
const { callee, arguments : args } = node
45
47
// _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' )
47
49
continue
48
50
49
51
// { directives: [] }
0 commit comments