11import  {  capitalize  }  from  '@vue/shared' 
22import  type  {  Node ,  ObjectExpression ,  Statement  }  from  '@babel/types' 
3- import  {  partition  }  from  '@antfu/utils' 
3+ import  {  notNullish ,   partition ,   uniq  }  from  '@antfu/utils' 
44import  type  {  ParsedSFC ,  ScriptSetupTransformOptions  }  from  '../types' 
55import  {  applyMacros  }  from  './macros' 
66import  {  getIdentifierDeclarations  }  from  './identifiers' 
77import  {  generate ,  t  }  from  './babel' 
8- import  {  isNotNil ,   pascalize  }  from  './utils' 
8+ import  {  pascalize  }  from  './utils' 
99
10- function  isAsyncImport ( node : any )  { 
11-   if  ( node . type   ===   'VariableDeclaration' )  { 
10+ function  isAsyncImport ( node : Statement )  { 
11+   if  ( t . isVariableDeclaration ( node ) )  { 
1212    const  declaration  =  node . declarations [ 0 ] 
1313
14-     return  declaration ?. init ?. callee ?. name  ===  'defineAsyncComponent' 
14+     return  ( 
15+       declaration  !==  undefined 
16+       &&  t . isCallExpression ( declaration . init ) 
17+       &&  t . isIdentifier ( declaration . init . callee ) 
18+       &&  declaration . init . callee . name  ===  'defineAsyncComponent' 
19+     ) 
1520  } 
1621
1722  return  false 
1823} 
1924
20- export  function  transformScriptSetup ( sfc : ParsedSFC ,  options ?: ScriptSetupTransformOptions )  { 
25+ export  function  transformScriptSetup ( 
26+   sfc : ParsedSFC , 
27+   options ?: ScriptSetupTransformOptions , 
28+ )  { 
2129  const  {  scriptSetup,  script,  template }  =  sfc 
2230
2331  const  {  nodes : body ,  props,  expose }  =  applyMacros ( scriptSetup . ast . body ) 
@@ -26,16 +34,17 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
2634    body , 
2735    n  => 
2836      isAsyncImport ( n ) 
29-      ||  n . type   ===   'ImportDeclaration' 
30-      ||  n . type   ===   'ExportNamedDeclaration' 
31-      ||  n . type . startsWith ( 'TS' ) , 
37+        ||  t . isImportDeclaration ( n ) 
38+        ||  t . isExportNamedDeclaration ( n ) 
39+        ||  n . type . startsWith ( 'TS' ) , 
3240  ) 
3341
3442  // get all identifiers in `<script setup>` 
35-   const  declarations  =  new  Set < string > ( ) 
36-   getIdentifierDeclarations ( hoisted ,  declarations ) 
37-   getIdentifierDeclarations ( setupBody ,  declarations ) 
38-   const  declarationArray  =  Array . from ( declarations ) . filter ( isNotNil ) 
43+   const  declarations  =  [ 
44+     ...getIdentifierDeclarations ( hoisted ) , 
45+     ...getIdentifierDeclarations ( setupBody ) , 
46+   ] 
47+   const  declarationArray  =  uniq ( declarations ) . filter ( notNullish ) 
3948
4049  // filter out identifiers that are used in `<template>` 
4150  const  returns : ObjectExpression [ 'properties' ]  =  declarationArray 
@@ -45,19 +54,24 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
4554      return  t . objectProperty ( id ,  id ,  false ,  true ) 
4655    } ) 
4756
48-   const  components  =  Array . from ( template . components ) . map ( component  => 
49-     declarationArray . find ( declare  =>  declare  ===  component ) 
50-     ??  declarationArray . find ( declare  =>  pascalize ( declare )  ===  component ) , 
51-   ) . filter ( isNotNil ) 
57+   const  components  =  Array . from ( template . components ) 
58+     . map ( 
59+       component  => 
60+         declarationArray . find ( declare  =>  declare  ===  component ) 
61+         ??  declarationArray . find ( declare  =>  pascalize ( declare )  ===  component ) , 
62+     ) 
63+     . filter ( notNullish ) 
5264
53-   const  directiveDeclaration  =  Array . from ( template . directives ) . map ( ( directive )  =>  { 
54-     const  identifier  =  declarationArray . find ( declaration  =>  declaration  ===  `v${ capitalize ( directive ) }  ` ) 
55-     if  ( identifier  ===  undefined ) 
56-       return  undefined 
65+   const  directiveDeclaration  =  Array . from ( template . directives ) 
66+     . map ( ( directive )  =>  { 
67+       const  identifier  =  declarationArray . find ( 
68+         declaration  =>  declaration  ===  `v${ capitalize ( directive ) }  ` , 
69+       ) 
70+       if  ( identifier  ===  undefined )  return  undefined 
5771
58-     return  {  identifier,  directive } 
59-   } , 
60-   ) . filter ( isNotNil ) 
72+        return  {  identifier,  directive } 
73+      } ) 
74+      . filter ( notNullish ) 
6175
6276  // append `<script setup>` imports to `<script>` 
6377
@@ -71,10 +85,7 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
7185    if  ( node . type  ===  'ExportDefaultDeclaration' )  { 
7286      hasBody  =  true 
7387      return  t . variableDeclaration ( 'const' ,  [ 
74-         t . variableDeclarator ( 
75-           __sfc , 
76-           node . declaration  as  any , 
77-         ) , 
88+         t . variableDeclarator ( __sfc ,  node . declaration  as  any ) , 
7889      ] ) 
7990    } 
8091    return  node 
@@ -90,10 +101,7 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
90101  if  ( ! hasBody )  { 
91102    ast . body . push ( 
92103      t . variableDeclaration ( 'const' ,  [ 
93-         t . variableDeclarator ( 
94-           __sfc , 
95-           t . objectExpression ( [ ] ) , 
96-         ) , 
104+         t . variableDeclarator ( __sfc ,  t . objectExpression ( [ ] ) ) , 
97105      ] ) , 
98106    ) 
99107  } 
@@ -104,7 +112,8 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
104112    hasBody  =  true 
105113    ast . body . push ( 
106114      t . expressionStatement ( 
107-         t . assignmentExpression ( '=' , 
115+         t . assignmentExpression ( 
116+           '=' , 
108117          t . memberExpression ( __sfc ,  t . identifier ( 'props' ) ) , 
109118          props  as  any , 
110119        ) , 
@@ -126,15 +135,13 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
126135
127136    ast . body . push ( 
128137      t . expressionStatement ( 
129-         t . assignmentExpression ( '=' , 
138+         t . assignmentExpression ( 
139+           '=' , 
130140          t . memberExpression ( __sfc ,  t . identifier ( 'setup' ) ) , 
131-           t . arrowFunctionExpression ( [ 
132-             t . identifier ( '__props' ) , 
133-             t . identifier ( '__ctx' ) , 
134-           ] ,  t . blockStatement ( [ 
135-             ...setupBody , 
136-             returnStatement  as  any , 
137-           ] ) ) , 
141+           t . arrowFunctionExpression ( 
142+             [ t . identifier ( '__props' ) ,  t . identifier ( '__ctx' ) ] , 
143+             t . blockStatement ( [ ...setupBody ,  returnStatement  as  any ] ) , 
144+           ) , 
138145        ) , 
139146      )  as  any , 
140147    ) 
@@ -153,7 +160,8 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
153160
154161    ast . body . push ( 
155162      t . expressionStatement ( 
156-         t . assignmentExpression ( '=' , 
163+         t . assignmentExpression ( 
164+           '=' , 
157165          t . memberExpression ( __sfc ,  t . identifier ( 'components' ) ) , 
158166          t . callExpression ( 
159167            t . memberExpression ( t . identifier ( 'Object' ) ,  t . identifier ( 'assign' ) ) , 
@@ -172,17 +180,20 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
172180  if  ( directiveDeclaration . length )  { 
173181    hasBody  =  true 
174182    const  directivesObject  =  t . objectExpression ( 
175-       directiveDeclaration . map ( ( {  directive,  identifier } )  =>  ( t . objectProperty ( 
176-         t . identifier ( directive ) , 
177-         t . identifier ( identifier ) , 
178-         false , 
179-         false , 
180-       ) ) ) , 
183+       directiveDeclaration . map ( ( {  directive,  identifier } )  => 
184+         t . objectProperty ( 
185+           t . identifier ( directive ) , 
186+           t . identifier ( identifier ) , 
187+           false , 
188+           false , 
189+         ) , 
190+       ) , 
181191    ) 
182192
183193    ast . body . push ( 
184194      t . expressionStatement ( 
185-         t . assignmentExpression ( '=' , 
195+         t . assignmentExpression ( 
196+           '=' , 
186197          t . memberExpression ( __sfc ,  t . identifier ( 'directives' ) ) , 
187198          t . callExpression ( 
188199            t . memberExpression ( t . identifier ( 'Object' ) ,  t . identifier ( 'assign' ) ) , 
@@ -205,9 +216,7 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
205216
206217  // re-export 
207218  // `export default __sfc_main` 
208-   ast . body . push ( 
209-     t . exportDefaultDeclaration ( __sfc )  as  any , 
210-   ) 
219+   ast . body . push ( t . exportDefaultDeclaration ( __sfc )  as  any ) 
211220
212221  ast  =  options ?. astTransforms ?. post ?.( ast ,  sfc )  ||  ast 
213222
0 commit comments