11import * as t from '@babel/types' ;
2- import type * as BabelCore from '@babel/core' ;
2+ import type { PluginAPI , PluginObject , PluginPass } from '@babel/core' ;
33import _template from '@babel/template' ;
4- // @ts -expect-error
54import _syntaxJsx from '@babel/plugin-syntax-jsx' ;
65import { 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' ;
87import ResolveType from '@vue/babel-plugin-resolve-type' ;
98import { declare } from '@babel/helper-plugin-utils' ;
109import transformVueJSX from './transform-vue-jsx' ;
@@ -14,20 +13,11 @@ import type { State, VueJSXPluginOptions } from './interface';
1413export { VueJSXPluginOptions } ;
1514
1615const 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+ }
2820 } ) ;
29-
30- return fileHasJSX ;
3121} ;
3222
3323const JSX_ANNOTATION_REGEX = / \* ? \s * @ j s x \s + ( [ ^ \s ] + ) / ;
@@ -41,15 +31,15 @@ const syntaxJsx = /*#__PURE__*/ interopDefault(_syntaxJsx);
4131const template = /*#__PURE__*/ interopDefault ( _template ) ;
4232
4333const plugin : (
44- api : object ,
34+ api : PluginAPI ,
4535 options : VueJSXPluginOptions | null | undefined ,
4636 dirname : string
47- ) => BabelCore . PluginObj < State > = declare <
48- VueJSXPluginOptions ,
49- BabelCore . PluginObj < State >
37+ ) => PluginObject < State & PluginPass > = declare <
38+ State ,
39+ VueJSXPluginOptions | null | undefined
5040> ( ( api , opt , dirname ) => {
5141 const { types } = api ;
52- let resolveType : BabelCore . PluginObj < BabelCore . PluginPass > | undefined ;
42+ let resolveType : PluginObject < PluginPass > | undefined ;
5343 if ( opt . resolveType ) {
5444 if ( typeof opt . resolveType === 'boolean' ) opt . resolveType = { } ;
5545 resolveType = ResolveType ( api , opt . resolveType , dirname ) ;
@@ -59,7 +49,7 @@ const plugin: (
5949 name : 'babel-plugin-jsx' ,
6050 inherits : /*#__PURE__*/ interopDefault ( syntaxJsx ) ,
6151 visitor : {
62- ...( resolveType ?. visitor as Visitor < State > ) ,
52+ ...( resolveType ?. visitor as VisitorBase < State & PluginPass > ) ,
6353 ...transformVueJSX ,
6454 ...sugarFragment ,
6555 Program : {
@@ -133,7 +123,7 @@ const plugin: (
133123 if ( ! sourceName ) {
134124 sourceName = addNamespace ( path , 'vue' , {
135125 ensureLiveReference : true ,
136- } ) ;
126+ } ) as t . Identifier ;
137127 }
138128 return t . memberExpression ( sourceName , t . identifier ( name ) ) ;
139129 } ) ;
0 commit comments