@@ -62,8 +62,7 @@ export interface CodegenContext extends Required<CodegenOptions> {
62
62
codes : [ left : string , right : string , segment ?: string ] ,
63
63
...fn : Array < false | ( ( ) => void ) >
64
64
) : void
65
- indent ( ) : void
66
- deindent ( ) : void
65
+ withIndent ( fn : ( ) => void ) : void
67
66
newline ( ) : void
68
67
69
68
helpers : Set < string >
@@ -183,10 +182,9 @@ function createCodegenContext(
183
182
}
184
183
context . push ( right )
185
184
} ,
186
- indent ( ) {
185
+ withIndent ( fn ) {
187
186
++ context . indentLevel
188
- } ,
189
- deindent ( ) {
187
+ fn ( )
190
188
-- context . indentLevel
191
189
} ,
192
190
newline ( ) {
@@ -231,8 +229,15 @@ export function generate(
231
229
options : CodegenOptions = { } ,
232
230
) : CodegenResult {
233
231
const ctx = createCodegenContext ( ir , options )
234
- const { push, pushWithNewline, indent, deindent, newline } = ctx
235
- const { vaporHelper, helpers, vaporHelpers } = ctx
232
+ const {
233
+ push,
234
+ pushWithNewline,
235
+ withIndent,
236
+ newline,
237
+ helpers,
238
+ vaporHelper,
239
+ vaporHelpers,
240
+ } = ctx
236
241
237
242
const functionName = 'render'
238
243
const isSetupInlined = ! ! options . inline
@@ -243,62 +248,62 @@ export function generate(
243
248
newline ( )
244
249
pushWithNewline ( `export function ${ functionName } (_ctx) {` )
245
250
}
246
- indent ( )
247
-
248
- ir . template . forEach ( ( template , i ) => {
249
- if ( template . type === IRNodeTypes . TEMPLATE_FACTORY ) {
250
- // TODO source map?
251
- pushWithNewline (
252
- `const t${ i } = ${ vaporHelper ( 'template' ) } (${ JSON . stringify (
253
- template . template ,
254
- ) } )`,
255
- )
256
- } else {
257
- // fragment
258
- pushWithNewline (
259
- `const t0 = ${ vaporHelper ( 'fragment' ) } ()\n` ,
260
- NewlineType . End ,
261
- )
262
- }
263
- } )
264
251
265
- {
266
- pushWithNewline ( `const n${ ir . dynamic . id } = t0()` )
252
+ withIndent ( ( ) => {
253
+ ir . template . forEach ( ( template , i ) => {
254
+ if ( template . type === IRNodeTypes . TEMPLATE_FACTORY ) {
255
+ // TODO source map?
256
+ pushWithNewline (
257
+ `const t${ i } = ${ vaporHelper ( 'template' ) } (${ JSON . stringify (
258
+ template . template ,
259
+ ) } )`,
260
+ )
261
+ } else {
262
+ // fragment
263
+ pushWithNewline (
264
+ `const t0 = ${ vaporHelper ( 'fragment' ) } ()\n` ,
265
+ NewlineType . End ,
266
+ )
267
+ }
268
+ } )
267
269
268
- const children = genChildren ( ir . dynamic . children )
269
- if ( children ) {
270
- pushWithNewline (
271
- `const ${ children } = ${ vaporHelper ( 'children' ) } (n${ ir . dynamic . id } )` ,
272
- )
273
- }
270
+ {
271
+ pushWithNewline ( `const n${ ir . dynamic . id } = t0()` )
274
272
275
- for ( const oper of ir . operation . filter (
276
- ( oper ) : oper is WithDirectiveIRNode =>
277
- oper . type === IRNodeTypes . WITH_DIRECTIVE ,
278
- ) ) {
279
- genWithDirective ( oper , ctx )
280
- }
273
+ const children = genChildren ( ir . dynamic . children )
274
+ if ( children ) {
275
+ pushWithNewline (
276
+ `const ${ children } = ${ vaporHelper ( 'children' ) } (n ${ ir . dynamic . id } )` ,
277
+ )
278
+ }
281
279
282
- for ( const operation of ir . operation ) {
283
- genOperation ( operation , ctx )
284
- }
280
+ for ( const oper of ir . operation . filter (
281
+ ( oper ) : oper is WithDirectiveIRNode =>
282
+ oper . type === IRNodeTypes . WITH_DIRECTIVE ,
283
+ ) ) {
284
+ genWithDirective ( oper , ctx )
285
+ }
285
286
286
- for ( const { operations } of ir . effect ) {
287
- pushWithNewline ( `${ vaporHelper ( 'effect' ) } (() => {` )
288
- indent ( )
289
- for ( const operation of operations ) {
287
+ for ( const operation of ir . operation ) {
290
288
genOperation ( operation , ctx )
291
289
}
292
- deindent ( )
293
- pushWithNewline ( '})' )
294
- }
295
290
296
- // TODO multiple-template
297
- // TODO return statement in IR
298
- pushWithNewline ( `return n${ ir . dynamic . id } ` )
299
- }
291
+ for ( const { operations } of ir . effect ) {
292
+ pushWithNewline ( `${ vaporHelper ( 'effect' ) } (() => {` )
293
+ withIndent ( ( ) => {
294
+ for ( const operation of operations ) {
295
+ genOperation ( operation , ctx )
296
+ }
297
+ } )
298
+ pushWithNewline ( '})' )
299
+ }
300
+
301
+ // TODO multiple-template
302
+ // TODO return statement in IR
303
+ pushWithNewline ( `return n${ ir . dynamic . id } ` )
304
+ }
305
+ } )
300
306
301
- deindent ( )
302
307
newline ( )
303
308
if ( isSetupInlined ) {
304
309
push ( '})()' )
0 commit comments