Skip to content

Commit 0c26b0d

Browse files
committed
feat: withIndent
1 parent 4b4cb05 commit 0c26b0d

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

packages/compiler-vapor/src/generate.ts

+59-54
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ export interface CodegenContext extends Required<CodegenOptions> {
6262
codes: [left: string, right: string, segment?: string],
6363
...fn: Array<false | (() => void)>
6464
): void
65-
indent(): void
66-
deindent(): void
65+
withIndent(fn: () => void): void
6766
newline(): void
6867

6968
helpers: Set<string>
@@ -183,10 +182,9 @@ function createCodegenContext(
183182
}
184183
context.push(right)
185184
},
186-
indent() {
185+
withIndent(fn) {
187186
++context.indentLevel
188-
},
189-
deindent() {
187+
fn()
190188
--context.indentLevel
191189
},
192190
newline() {
@@ -231,8 +229,15 @@ export function generate(
231229
options: CodegenOptions = {},
232230
): CodegenResult {
233231
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
236241

237242
const functionName = 'render'
238243
const isSetupInlined = !!options.inline
@@ -243,62 +248,62 @@ export function generate(
243248
newline()
244249
pushWithNewline(`export function ${functionName}(_ctx) {`)
245250
}
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-
})
264251

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+
})
267269

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()`)
274272

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+
}
281279

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+
}
285286

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) {
290288
genOperation(operation, ctx)
291289
}
292-
deindent()
293-
pushWithNewline('})')
294-
}
295290

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+
})
300306

301-
deindent()
302307
newline()
303308
if (isSetupInlined) {
304309
push('})()')

0 commit comments

Comments
 (0)