]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat: withIndent
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Sat, 9 Dec 2023 17:05:26 +0000 (01:05 +0800)
committer三咲智子 Kevin Deng <sxzz@sxzz.moe>
Sat, 9 Dec 2023 17:05:26 +0000 (01:05 +0800)
packages/compiler-vapor/src/generate.ts

index 9dc5eb1adca9fd21aab7bc6f712e81c5059e1c96..ff41cad183843ed8e64a30058a5041b556d02602 100644 (file)
@@ -62,8 +62,7 @@ export interface CodegenContext extends Required<CodegenOptions> {
     codes: [left: string, right: string, segment?: string],
     ...fn: Array<false | (() => void)>
   ): void
-  indent(): void
-  deindent(): void
+  withIndent(fn: () => void): void
   newline(): void
 
   helpers: Set<string>
@@ -183,10 +182,9 @@ function createCodegenContext(
       }
       context.push(right)
     },
-    indent() {
+    withIndent(fn) {
       ++context.indentLevel
-    },
-    deindent() {
+      fn()
       --context.indentLevel
     },
     newline() {
@@ -231,8 +229,15 @@ export function generate(
   options: CodegenOptions = {},
 ): CodegenResult {
   const ctx = createCodegenContext(ir, options)
-  const { push, pushWithNewline, indent, deindent, newline } = ctx
-  const { vaporHelper, helpers, vaporHelpers } = ctx
+  const {
+    push,
+    pushWithNewline,
+    withIndent,
+    newline,
+    helpers,
+    vaporHelper,
+    vaporHelpers,
+  } = ctx
 
   const functionName = 'render'
   const isSetupInlined = !!options.inline
@@ -243,62 +248,62 @@ export function generate(
     newline()
     pushWithNewline(`export function ${functionName}(_ctx) {`)
   }
-  indent()
-
-  ir.template.forEach((template, i) => {
-    if (template.type === IRNodeTypes.TEMPLATE_FACTORY) {
-      // TODO source map?
-      pushWithNewline(
-        `const t${i} = ${vaporHelper('template')}(${JSON.stringify(
-          template.template,
-        )})`,
-      )
-    } else {
-      // fragment
-      pushWithNewline(
-        `const t0 = ${vaporHelper('fragment')}()\n`,
-        NewlineType.End,
-      )
-    }
-  })
 
-  {
-    pushWithNewline(`const n${ir.dynamic.id} = t0()`)
+  withIndent(() => {
+    ir.template.forEach((template, i) => {
+      if (template.type === IRNodeTypes.TEMPLATE_FACTORY) {
+        // TODO source map?
+        pushWithNewline(
+          `const t${i} = ${vaporHelper('template')}(${JSON.stringify(
+            template.template,
+          )})`,
+        )
+      } else {
+        // fragment
+        pushWithNewline(
+          `const t0 = ${vaporHelper('fragment')}()\n`,
+          NewlineType.End,
+        )
+      }
+    })
 
-    const children = genChildren(ir.dynamic.children)
-    if (children) {
-      pushWithNewline(
-        `const ${children} = ${vaporHelper('children')}(n${ir.dynamic.id})`,
-      )
-    }
+    {
+      pushWithNewline(`const n${ir.dynamic.id} = t0()`)
 
-    for (const oper of ir.operation.filter(
-      (oper): oper is WithDirectiveIRNode =>
-        oper.type === IRNodeTypes.WITH_DIRECTIVE,
-    )) {
-      genWithDirective(oper, ctx)
-    }
+      const children = genChildren(ir.dynamic.children)
+      if (children) {
+        pushWithNewline(
+          `const ${children} = ${vaporHelper('children')}(n${ir.dynamic.id})`,
+        )
+      }
 
-    for (const operation of ir.operation) {
-      genOperation(operation, ctx)
-    }
+      for (const oper of ir.operation.filter(
+        (oper): oper is WithDirectiveIRNode =>
+          oper.type === IRNodeTypes.WITH_DIRECTIVE,
+      )) {
+        genWithDirective(oper, ctx)
+      }
 
-    for (const { operations } of ir.effect) {
-      pushWithNewline(`${vaporHelper('effect')}(() => {`)
-      indent()
-      for (const operation of operations) {
+      for (const operation of ir.operation) {
         genOperation(operation, ctx)
       }
-      deindent()
-      pushWithNewline('})')
-    }
 
-    // TODO multiple-template
-    // TODO return statement in IR
-    pushWithNewline(`return n${ir.dynamic.id}`)
-  }
+      for (const { operations } of ir.effect) {
+        pushWithNewline(`${vaporHelper('effect')}(() => {`)
+        withIndent(() => {
+          for (const operation of operations) {
+            genOperation(operation, ctx)
+          }
+        })
+        pushWithNewline('})')
+      }
+
+      // TODO multiple-template
+      // TODO return statement in IR
+      pushWithNewline(`return n${ir.dynamic.id}`)
+    }
+  })
 
-  deindent()
   newline()
   if (isSetupInlined) {
     push('})()')