@@ -121,132 +121,88 @@ function _generate(
121121 const codeMaps = new Map < string , RawSourceMap > ( )
122122 const { type, sourceMap, isGlobal, locale, jit } = options
123123
124- const codegenFn : CodeGenFunction = jit
124+ const _codegenFn : CodeGenFunction = jit
125125 ? generateResourceAst
126126 : generateMessageFunction
127127
128+ function codegenFn ( value : string ) {
129+ const { code, map } = _codegenFn ( value , options , pathStack )
130+ sourceMap && map != null && codeMaps . set ( value , map )
131+ return code
132+ }
133+
128134 const componentNamespace = '_Component'
129135
130136 traverseNodes ( node , {
131137 enterNode ( node : JSONNode , parent : JSONNode ) {
138+ if ( parent ?. type === 'JSONArrayExpression' ) {
139+ const lastIndex = itemsCountStack . length - 1
140+ const currentCount = parent . elements . length - itemsCountStack [ lastIndex ]
141+ pathStack . push ( currentCount . toString ( ) )
142+ itemsCountStack [ lastIndex ] = -- itemsCountStack [ lastIndex ]
143+ } else if ( parent ?. type === 'JSONObjectExpression' ) {
144+ const lastIndex = propsCountStack . length - 1
145+ propsCountStack [ lastIndex ] = -- propsCountStack [ lastIndex ]
146+ }
147+
132148 switch ( node . type ) {
133149 case 'Program' :
134150 if ( type === 'plain' ) {
135151 generator . push ( `const resource = ` )
136152 } else if ( type === 'sfc' ) {
137- // for 'sfc'
138- const variableName =
139- type === 'sfc' ? ( ! isGlobal ? '__i18n' : '__i18nGlobal' ) : ''
140- const localeName =
141- type === 'sfc' ? ( locale != null ? locale : `""` ) : ''
142- const exportSyntax = 'export default'
143- generator . push ( `${ exportSyntax } function (Component) {` )
153+ const variableName = ! isGlobal ? '__i18n' : '__i18nGlobal'
154+ const localeName = JSON . stringify ( locale ?? `""` )
155+ generator . push ( `export default function (Component) {` )
144156 generator . indent ( )
145- const componentVariable = `Component`
146- generator . pushline (
147- `const ${ componentNamespace } = ${ componentVariable } `
148- )
157+ generator . pushline ( `const ${ componentNamespace } = Component` )
149158 generator . pushline (
150159 `${ componentNamespace } .${ variableName } = ${ componentNamespace } .${ variableName } || []`
151160 )
152161 generator . push ( `${ componentNamespace } .${ variableName } .push({` )
153162 generator . indent ( )
154- generator . pushline ( `"locale": ${ JSON . stringify ( localeName ) } ,` )
163+ generator . pushline ( `"locale": ${ localeName } ,` )
155164 generator . push ( `"resource": ` )
156165 }
157166 break
158167 case 'JSONObjectExpression' :
159168 generator . push ( `{` )
160169 generator . indent ( )
161170 propsCountStack . push ( node . properties . length )
162- if ( parent . type === 'JSONArrayExpression' ) {
163- const lastIndex = itemsCountStack . length - 1
164- const currentCount =
165- parent . elements . length - itemsCountStack [ lastIndex ]
166- pathStack . push ( currentCount . toString ( ) )
167- itemsCountStack [ lastIndex ] = -- itemsCountStack [ lastIndex ]
168- }
169171 break
170- case 'JSONProperty' :
171- if (
172- node . value . type === 'JSONLiteral' &&
173- ( node . key . type === 'JSONLiteral' ||
174- node . key . type === 'JSONIdentifier' )
175- ) {
176- const name =
177- node . key . type === 'JSONLiteral' ? node . key . value : node . key . name
172+ case 'JSONArrayExpression' :
173+ generator . push ( `[` )
174+ generator . indent ( )
175+ itemsCountStack . push ( node . elements . length )
176+ break
177+ case 'JSONProperty' : {
178+ const name =
179+ node . key . type === 'JSONLiteral' ? node . key . value : node . key . name
180+ const strName = JSON . stringify ( name )
181+ generator . push ( `${ strName } : ` )
182+ pathStack . push ( name . toString ( ) )
183+ if ( node . value . type === 'JSONLiteral' ) {
178184 const value = node . value . value
185+ const strValue = JSON . stringify ( value )
179186 if ( isString ( value ) ) {
180- generator . push ( `${ JSON . stringify ( name ) } : ` )
181- pathStack . push ( name . toString ( ) )
182- const { code, map } = codegenFn ( value , options , pathStack )
183- sourceMap && map != null && codeMaps . set ( value , map )
184- generator . push ( `${ code } ` , node . value , value )
187+ generator . push ( codegenFn ( value ) , node . value , value )
188+ } else if ( forceStringify ) {
189+ generator . push ( codegenFn ( strValue ) , node . value , strValue )
185190 } else {
186- if ( forceStringify ) {
187- const strValue = JSON . stringify ( value )
188- generator . push ( `${ JSON . stringify ( name ) } : ` )
189- pathStack . push ( name . toString ( ) )
190- const { code, map } = codegenFn ( strValue , options , pathStack )
191- sourceMap && map != null && codeMaps . set ( strValue , map )
192- generator . push ( `${ code } ` , node . value , strValue )
193- } else {
194- generator . push (
195- `${ JSON . stringify ( name ) } : ${ JSON . stringify ( value ) } `
196- )
197- pathStack . push ( name . toString ( ) )
198- }
191+ generator . push ( strValue )
199192 }
200- } else if (
201- ( node . value . type === 'JSONObjectExpression' ||
202- node . value . type === 'JSONArrayExpression' ) &&
203- ( node . key . type === 'JSONLiteral' ||
204- node . key . type === 'JSONIdentifier' )
205- ) {
206- const name =
207- node . key . type === 'JSONLiteral' ? node . key . value : node . key . name
208- generator . push ( `${ JSON . stringify ( name ) } : ` )
209- pathStack . push ( name . toString ( ) )
210193 }
211- const lastIndex = propsCountStack . length - 1
212- propsCountStack [ lastIndex ] = -- propsCountStack [ lastIndex ]
213- break
214- case 'JSONArrayExpression' :
215- generator . push ( `[` )
216- generator . indent ( )
217- if ( parent . type === 'JSONArrayExpression' ) {
218- const lastIndex = itemsCountStack . length - 1
219- const currentCount =
220- parent . elements . length - itemsCountStack [ lastIndex ]
221- pathStack . push ( currentCount . toString ( ) )
222- itemsCountStack [ lastIndex ] = -- itemsCountStack [ lastIndex ]
223- }
224- itemsCountStack . push ( node . elements . length )
225194 break
195+ }
226196 case 'JSONLiteral' :
227- if ( parent . type === 'JSONArrayExpression' ) {
228- const lastIndex = itemsCountStack . length - 1
229- const currentCount =
230- parent . elements . length - itemsCountStack [ lastIndex ]
231- pathStack . push ( currentCount . toString ( ) )
232- if ( node . type === 'JSONLiteral' ) {
233- const value = node . value
234- if ( isString ( value ) ) {
235- const { code, map } = codegenFn ( value , options , pathStack )
236- sourceMap && map != null && codeMaps . set ( value , map )
237- generator . push ( `${ code } ` , node , value )
238- } else {
239- if ( forceStringify ) {
240- const strValue = JSON . stringify ( value )
241- const { code, map } = codegenFn ( strValue , options , pathStack )
242- sourceMap && map != null && codeMaps . set ( strValue , map )
243- generator . push ( `${ code } ` , node , strValue )
244- } else {
245- generator . push ( `${ JSON . stringify ( value ) } ` )
246- }
247- }
248- }
249- itemsCountStack [ lastIndex ] = -- itemsCountStack [ lastIndex ]
197+ if ( parent . type !== 'JSONArrayExpression' ) break
198+ const value = node . value
199+ const strValue = JSON . stringify ( value )
200+ if ( isString ( value ) ) {
201+ generator . push ( codegenFn ( value ) , node , value )
202+ } else if ( forceStringify ) {
203+ generator . push ( codegenFn ( strValue ) , node , strValue )
204+ } else {
205+ generator . push ( strValue )
250206 }
251207 break
252208 default :
@@ -273,18 +229,6 @@ function _generate(
273229 }
274230 generator . deindent ( )
275231 generator . push ( `}` )
276- if ( parent . type === 'JSONArrayExpression' ) {
277- if ( itemsCountStack [ itemsCountStack . length - 1 ] !== 0 ) {
278- pathStack . pop ( )
279- generator . pushline ( `,` )
280- }
281- }
282- break
283- case 'JSONProperty' :
284- if ( propsCountStack [ propsCountStack . length - 1 ] !== 0 ) {
285- pathStack . pop ( )
286- generator . pushline ( `,` )
287- }
288232 break
289233 case 'JSONArrayExpression' :
290234 if ( itemsCountStack [ itemsCountStack . length - 1 ] === 0 ) {
@@ -293,26 +237,23 @@ function _generate(
293237 }
294238 generator . deindent ( )
295239 generator . push ( `]` )
296- if ( parent . type === 'JSONArrayExpression' ) {
297- if ( itemsCountStack [ itemsCountStack . length - 1 ] !== 0 ) {
298- pathStack . pop ( )
299- generator . pushline ( `,` )
300- }
301- }
302- break
303- case 'JSONLiteral' :
304- if ( parent . type === 'JSONArrayExpression' ) {
305- if ( itemsCountStack [ itemsCountStack . length - 1 ] !== 0 ) {
306- pathStack . pop ( )
307- generator . pushline ( `,` )
308- } else {
309- generator . pushline ( `,` )
310- }
311- }
312240 break
313241 default :
314242 break
315243 }
244+
245+ // if not last obj property or array value
246+ if (
247+ parent ?. type === 'JSONArrayExpression' ||
248+ parent ?. type === 'JSONObjectExpression'
249+ ) {
250+ const stackArr =
251+ node . type === 'JSONProperty' ? propsCountStack : itemsCountStack
252+ if ( stackArr [ stackArr . length - 1 ] !== 0 ) {
253+ pathStack . pop ( )
254+ generator . pushline ( `,` )
255+ }
256+ }
316257 }
317258 } )
318259
0 commit comments