1- import t from '@babel/types' ;
2- import LOG from '../../../logHelper.js' ;
3- import { globalLibPaths } from '../../defaultConfig.js' ;
4- import { addInstance } from '../../jsx/handlers/instanceHandler.js' ;
5- import { JSErrors } from '../../../errors.js' ;
6- import { DATA_REACTIVE , INSTANCE } from '../../jsx/consts.js' ;
7- import { watchParser } from './watchHandler.js' ;
8- import { propsParser } from './propsHandler.js' ;
1+ import t from '@babel/types'
2+ import LOG from '../../../logHelper.js'
3+ import { globalLibPaths } from '../../defaultConfig.js'
4+ import { addInstance } from '../../jsx/handlers/instanceHandler.js'
5+ import { JSErrors } from '../../../errors.js'
6+ import { DATA_REACTIVE , INSTANCE } from '../../jsx/consts.js'
7+ import { watchParser } from './watchHandler.js'
8+ import { propsParser } from './propsHandler.js'
9+ import { getIsPinia } from '../jsUtils.js' ;
910
1011/**
1112 * 找到Object的key和value
@@ -116,6 +117,7 @@ function computedParser(ast, reactCovert) {
116117 let computedBody = null ;
117118 if ( prop . type === 'SpreadElement' ) {
118119 /*
120+ vuex中需要讲mapState转成useMapState, pinia不需要
119121 computed: {
120122 ...mapState('counter', ['count']),
121123 }
@@ -124,7 +126,8 @@ function computedParser(ast, reactCovert) {
124126 */
125127 if ( prop . argument . type === 'CallExpression' ) {
126128 const callName = prop . argument . callee . name ;
127- const ags0 = prop . argument . arguments [ 0 ] ;
129+ const isPinia = getIsPinia ( reactCovert ) ;
130+ const ags0 = prop . argument . arguments [ isPinia ? 1 : 0 ] ;
128131 let outKeys = [ ] ;
129132 let outputsNode = null ;
130133 // 存在vuex的模块定义
@@ -143,10 +146,10 @@ function computedParser(ast, reactCovert) {
143146 } ) ;
144147 }
145148 }
146-
149+ const newCallName = isPinia ? callName : globalLibPaths . vuex . imports [ callName ] ;
147150 // 创建函数调用表达式 xx('')
148151 const functionCallExpression = t . callExpression (
149- t . identifier ( globalLibPaths . vuex . imports [ callName ] || callName ) ,
152+ t . identifier ( newCallName ) ,
150153 prop . argument . arguments
151154 ) ;
152155
@@ -221,24 +224,26 @@ function computedParser(ast, reactCovert) {
221224}
222225
223226function methodsParser ( ast , reactCovert ) {
227+ // vuex中需要讲mapState转成useMapState, pinia不需要
224228 const methodNames = [ ] ;
225229 ast ?. properties . forEach ( prop => {
226230 let computedBody = null ;
227231 if ( prop . type === 'SpreadElement' ) {
228232 if ( prop . argument . type === 'CallExpression' ) {
233+ const isPinia = getIsPinia ( reactCovert ) ;
229234 const callName = prop . argument . callee . name ;
230- const ags0 = prop . argument . arguments [ 0 ] ;
235+ const mapAttrParam = prop . argument . arguments [ isPinia ? 1 : 0 ] ;
231236 let outKeys = [ ] ;
232237 let outputsNode = null ;
233238 // 存在vuex的模块定义
234- if ( ags0 . type === 'ObjectExpression' ) {
235- const { keys } = findObjectExpressionKeyAndValue ( ags0 ) ;
239+ if ( mapAttrParam . type === 'ObjectExpression' ) {
240+ const { keys } = findObjectExpressionKeyAndValue ( mapAttrParam ) ;
236241 outKeys = keys ;
237242 } else {
238- if ( ags0 . type === 'StringLiteral' ) {
243+ if ( mapAttrParam . type === 'StringLiteral' ) {
239244 outputsNode = prop . argument . arguments ?. [ 1 ] ;
240- } else if ( ags0 . type === 'ArrayExpression' ) {
241- outputsNode = ags0 ;
245+ } else if ( mapAttrParam . type === 'ArrayExpression' ) {
246+ outputsNode = mapAttrParam ;
242247 }
243248 if ( outputsNode && outputsNode . type === 'ArrayExpression' ) {
244249 outputsNode . elements . forEach ( v => {
@@ -247,10 +252,10 @@ function methodsParser(ast, reactCovert) {
247252 } ) ;
248253 }
249254 }
250-
255+ const newCallName = isPinia ? callName : globalLibPaths . vuex . imports [ callName ]
251256 // 创建函数调用表达式 xx('')
252257 const functionCallExpression = t . callExpression (
253- t . identifier ( globalLibPaths . vuex . imports [ callName ] || callName ) ,
258+ t . identifier ( newCallName ) ,
254259 prop . argument . arguments
255260 ) ;
256261
@@ -298,11 +303,16 @@ function methodsParser(ast, reactCovert) {
298303
299304 // 创建 methods 对象
300305 const methodsObject = t . objectExpression (
301- methodNames . map ( name => t . objectProperty ( t . identifier ( name ) , t . identifier ( name ) , false , true ) )
306+ methodNames . map ( name =>
307+ t . objectProperty ( t . identifier ( name ) , t . identifier ( name ) , false , true )
308+ )
302309 ) ;
303310
304311 // 创建 setToInstance 函数调用
305- const setToInstanceCall = t . callExpression ( t . identifier ( 'setToInstance' ) , [ t . identifier ( INSTANCE ) , methodsObject ] ) ;
312+ const setToInstanceCall = t . callExpression ( t . identifier ( 'setToInstance' ) , [
313+ t . identifier ( INSTANCE ) ,
314+ methodsObject
315+ ] ) ;
306316
307317 // 创建表达式语句
308318 const expressionStatement = t . expressionStatement ( setToInstanceCall ) ;
@@ -382,8 +392,10 @@ function componentsParser(ast, reactCovert) {
382392 t . identifier ( 'components' ) ,
383393 t . objectExpression (
384394 ast . properties . map ( node => {
385- const name = reactCovert . sourceCodeContext . optionTypeRegistComponentMap . get ( node . key . name ) ;
386- return t . objectProperty ( t . identifier ( node . key . name ) , t . identifier ( name ) ) ;
395+ const keyName = node . key . name || node . key . value ;
396+ const mapped = reactCovert . sourceCodeContext . optionTypeRegistComponentMap . get ( keyName ) ;
397+ const targetName = mapped || keyName ;
398+ return t . objectProperty ( t . identifier ( keyName ) , t . identifier ( targetName ) ) ;
387399 } )
388400 )
389401 ) ,
0 commit comments