@@ -22,6 +22,10 @@ import { convertInject } from "./vue-property-decorator/Inject";
2222import { convertProvide } from "./vue-property-decorator/Provide" ;
2323import { convertTemplateRef } from "./vue-class-component/TemplateRef" ;
2424import { TsHelper } from "../helpers/TsHelper" ;
25+ import { convertVuexAction } from "./vuex-class/Action" ;
26+ import { convertVuexGetter } from "./vuex-class/Getter" ;
27+ import { convertVuexState } from "./vuex-class/State" ;
28+ import { convertVuexMutation } from "./vuex-class/Mutation" ;
2529
2630export function getDefaultPlugins ( tsModule : typeof ts ) : ASTConvertPlugins {
2731 return {
@@ -41,6 +45,10 @@ export function getDefaultPlugins(tsModule: typeof ts): ASTConvertPlugins {
4145 convertInject ,
4246 convertData ,
4347 convertTemplateRef ,
48+ convertVuexAction ,
49+ convertVuexGetter ,
50+ convertVuexState ,
51+ convertVuexMutation ,
4452 ] ,
4553 [ tsModule . SyntaxKind . GetAccessor ] : [ convertGetter ] ,
4654 [ tsModule . SyntaxKind . SetAccessor ] : [ convertSetter ] ,
@@ -132,6 +140,8 @@ export function convertASTResultToSetupFn(
132140) : ts . MethodDeclaration {
133141 const $t = new TsHelper ( options ) ;
134142
143+ const composables = getComposables ( astResults , $t ) ;
144+
135145 const returnStatement = $t . addTodoComment (
136146 $t . factory . createReturnStatement (
137147 $t . factory . createObjectLiteralExpression ( [
@@ -149,6 +159,7 @@ export function convertASTResultToSetupFn(
149159 "setup" ,
150160 [ options . setupPropsKey , options . setupContextKey ] ,
151161 [
162+ ...composables ,
152163 ...( astResults
153164 . filter ( ( el ) => el . kind === ASTResultKind . COMPOSITION )
154165 . map ( ( el ) => el . nodes )
@@ -158,15 +169,74 @@ export function convertASTResultToSetupFn(
158169 ) ;
159170}
160171
172+ interface Clause {
173+ named : Set < string > ;
174+ default ?: string ;
175+ params ?: ts . Expression [ ] ;
176+ }
177+
178+ function getComposables ( astResults : ASTResult < ts . Node > [ ] , $t : TsHelper ) : ts . VariableStatement [ ] {
179+ const composableMap = new Map < string , Clause > ( ) ;
180+ astResults . forEach ( ( result ) => {
181+ if ( ! result . composables ) return ;
182+
183+ result . composables . forEach ( ( info ) => {
184+ const func = info . func ;
185+ const tmp : Clause = composableMap . get ( func ) ?? { named : new Set ( ) } ;
186+
187+ if ( ! ( "default" in tmp ) && "default" in info ) {
188+ tmp . default = info . default ;
189+ }
190+
191+ if ( "params" in info ) {
192+ tmp . params = info . params ;
193+ }
194+
195+ if ( "named" in info ) {
196+ info . named ?. forEach ( ( name ) => tmp . named . add ( name ) ) ;
197+ }
198+
199+ composableMap . set ( func , tmp ) ;
200+ } ) ;
201+ } ) ;
202+
203+ const composables = Array . from ( composableMap )
204+ . map ( ( [ func , clause ] ) => {
205+ const funcExpr = $t . createCallExpression ( func , undefined , clause . params ) ;
206+ const statements : ts . VariableStatement [ ] = [ ] ;
207+ if ( clause . default ) {
208+ statements . push ( $t . createConstStatement ( clause . default , funcExpr ) ) ;
209+ } else {
210+ const u = undefined ;
211+ const importElements = [ ...clause . named ] . map ( ( name ) =>
212+ $t . factory . createBindingElement ( u , u , $t . factory . createIdentifier ( name ) )
213+ ) ;
214+
215+ if ( importElements . length > 0 ) {
216+ const vars = $t . factory . createObjectBindingPattern ( importElements ) ;
217+ const boundElementsDeclaration = $t . factory . createVariableDeclaration (
218+ vars ,
219+ u ,
220+ u ,
221+ funcExpr
222+ ) ;
223+ const varDecList = $t . factory . createVariableDeclarationList ( [ boundElementsDeclaration ] ) ;
224+ statements . push ( $t . factory . createVariableStatement ( u , varDecList ) ) ;
225+ }
226+ }
227+
228+ return statements ;
229+ } )
230+ . flat ( )
231+ . filter ( ( composable ) : composable is ts . VariableStatement => ! ! composable ) ;
232+
233+ return composables ;
234+ }
235+
161236export function convertASTResultToImport (
162237 astResults : ASTResult < ts . Node > [ ] ,
163238 options : UncouthOptions
164239) : ts . ImportDeclaration [ ] {
165- interface Clause {
166- named : Set < string > ;
167- default ?: string ;
168- }
169-
170240 const $t = new TsHelper ( options ) ;
171241
172242 const importMap = new Map < string , Clause > ( ) ;
@@ -177,10 +247,15 @@ export function convertASTResultToImport(
177247 if ( ! ( "default" in temp ) && "default" in importInfo ) {
178248 temp . default = importInfo . default ;
179249 }
180- temp . named . add ( "defineComponent" ) ;
250+
251+ if ( key === "vue" ) {
252+ temp . named . add ( "defineComponent" ) ;
253+ }
254+
181255 for ( const named of importInfo . named || [ ] ) {
182256 temp . named . add ( named ) ;
183257 }
258+
184259 importMap . set ( key , temp ) ;
185260 }
186261 }
0 commit comments