1
1
import * as t from '@babel/types' ;
2
- import type * as BabelCore from '@babel/core' ;
2
+ import type { PluginAPI , PluginObject , PluginPass } from '@babel/core' ;
3
3
import _template from '@babel/template' ;
4
- // @ts -expect-error
5
4
import _syntaxJsx from '@babel/plugin-syntax-jsx' ;
6
5
import { addNamed , addNamespace , isModule } from '@babel/helper-module-imports' ;
7
- import { type NodePath , type Visitor } from '@babel/traverse' ;
6
+ import type { NodePath , VisitorBase } from '@babel/traverse' ;
8
7
import ResolveType from '@vue/babel-plugin-resolve-type' ;
9
8
import { declare } from '@babel/helper-plugin-utils' ;
10
9
import transformVueJSX from './transform-vue-jsx' ;
@@ -14,20 +13,11 @@ import type { State, VueJSXPluginOptions } from './interface';
14
13
export { VueJSXPluginOptions } ;
15
14
16
15
const hasJSX = ( parentPath : NodePath < t . Program > ) => {
17
- let fileHasJSX = false ;
18
- parentPath . traverse ( {
19
- JSXElement ( path ) {
20
- // skip ts error
21
- fileHasJSX = true ;
22
- path . stop ( ) ;
23
- } ,
24
- JSXFragment ( path ) {
25
- fileHasJSX = true ;
26
- path . stop ( ) ;
27
- } ,
16
+ return t . traverseFast ( parentPath . node , ( node ) => {
17
+ if ( t . isJSXElement ( node ) || t . isJSXFragment ( node ) ) {
18
+ return t . traverseFast . stop ;
19
+ }
28
20
} ) ;
29
-
30
- return fileHasJSX ;
31
21
} ;
32
22
33
23
const JSX_ANNOTATION_REGEX = / \* ? \s * @ j s x \s + ( [ ^ \s ] + ) / ;
@@ -41,15 +31,15 @@ const syntaxJsx = /*#__PURE__*/ interopDefault(_syntaxJsx);
41
31
const template = /*#__PURE__*/ interopDefault ( _template ) ;
42
32
43
33
const plugin : (
44
- api : object ,
34
+ api : PluginAPI ,
45
35
options : VueJSXPluginOptions | null | undefined ,
46
36
dirname : string
47
- ) => BabelCore . PluginObj < State > = declare <
48
- VueJSXPluginOptions ,
49
- BabelCore . PluginObj < State >
37
+ ) => PluginObject < State & PluginPass > = declare <
38
+ State ,
39
+ VueJSXPluginOptions | null | undefined
50
40
> ( ( api , opt , dirname ) => {
51
41
const { types } = api ;
52
- let resolveType : BabelCore . PluginObj < BabelCore . PluginPass > | undefined ;
42
+ let resolveType : PluginObject < PluginPass > | undefined ;
53
43
if ( opt . resolveType ) {
54
44
if ( typeof opt . resolveType === 'boolean' ) opt . resolveType = { } ;
55
45
resolveType = ResolveType ( api , opt . resolveType , dirname ) ;
@@ -59,7 +49,7 @@ const plugin: (
59
49
name : 'babel-plugin-jsx' ,
60
50
inherits : /*#__PURE__*/ interopDefault ( syntaxJsx ) ,
61
51
visitor : {
62
- ...( resolveType ?. visitor as Visitor < State > ) ,
52
+ ...( resolveType ?. visitor as VisitorBase < State & PluginPass > ) ,
63
53
...transformVueJSX ,
64
54
...sugarFragment ,
65
55
Program : {
@@ -133,7 +123,7 @@ const plugin: (
133
123
if ( ! sourceName ) {
134
124
sourceName = addNamespace ( path , 'vue' , {
135
125
ensureLiveReference : true ,
136
- } ) ;
126
+ } ) as t . Identifier ;
137
127
}
138
128
return t . memberExpression ( sourceName , t . identifier ( name ) ) ;
139
129
} ) ;
0 commit comments