@@ -121,132 +121,88 @@ function _generate(
121
121
const codeMaps = new Map < string , RawSourceMap > ( )
122
122
const { type, sourceMap, isGlobal, locale, jit } = options
123
123
124
- const codegenFn : CodeGenFunction = jit
124
+ const _codegenFn : CodeGenFunction = jit
125
125
? generateResourceAst
126
126
: generateMessageFunction
127
127
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
+
128
134
const componentNamespace = '_Component'
129
135
130
136
traverseNodes ( node , {
131
137
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
+
132
148
switch ( node . type ) {
133
149
case 'Program' :
134
150
if ( type === 'plain' ) {
135
151
generator . push ( `const resource = ` )
136
152
} 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) {` )
144
156
generator . indent ( )
145
- const componentVariable = `Component`
146
- generator . pushline (
147
- `const ${ componentNamespace } = ${ componentVariable } `
148
- )
157
+ generator . pushline ( `const ${ componentNamespace } = Component` )
149
158
generator . pushline (
150
159
`${ componentNamespace } .${ variableName } = ${ componentNamespace } .${ variableName } || []`
151
160
)
152
161
generator . push ( `${ componentNamespace } .${ variableName } .push({` )
153
162
generator . indent ( )
154
- generator . pushline ( `"locale": ${ JSON . stringify ( localeName ) } ,` )
163
+ generator . pushline ( `"locale": ${ localeName } ,` )
155
164
generator . push ( `"resource": ` )
156
165
}
157
166
break
158
167
case 'JSONObjectExpression' :
159
168
generator . push ( `{` )
160
169
generator . indent ( )
161
170
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
- }
169
171
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' ) {
178
184
const value = node . value . value
185
+ const strValue = JSON . stringify ( value )
179
186
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 )
185
190
} 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 )
199
192
}
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 ( ) )
210
193
}
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 )
225
194
break
195
+ }
226
196
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 )
250
206
}
251
207
break
252
208
default :
@@ -273,18 +229,6 @@ function _generate(
273
229
}
274
230
generator . deindent ( )
275
231
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
- }
288
232
break
289
233
case 'JSONArrayExpression' :
290
234
if ( itemsCountStack [ itemsCountStack . length - 1 ] === 0 ) {
@@ -293,26 +237,23 @@ function _generate(
293
237
}
294
238
generator . deindent ( )
295
239
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
- }
312
240
break
313
241
default :
314
242
break
315
243
}
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
+ }
316
257
}
317
258
} )
318
259
0 commit comments