]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: improve v-if & v-for codegen output formatting
authorEvan You <yyx990803@gmail.com>
Mon, 23 Sep 2019 01:10:22 +0000 (21:10 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 23 Sep 2019 01:10:22 +0000 (21:10 -0400)
packages/compiler-core/src/codegen.ts

index a32b09c108fa3cf23ba10ce99ffa448760d0bf14..296c1e0b60cde7c1bb939e59b8db2cad936bacc2 100644 (file)
@@ -291,12 +291,14 @@ function genIfBranch(
   context: CodegenContext
 ) {
   if (condition) {
-    const { push, indent, deindent, newline } = context
     // v-if or v-else-if
+    const { push, indent, deindent, newline } = context
     push(`(${condition.content})`, condition)
     indent()
+    context.indentLevel++
     push(`? `)
     genChildren(children, context)
+    context.indentLevel--
     newline()
     push(`: `)
     if (nextIndex < branches.length) {
@@ -317,40 +319,41 @@ function genFor(node: ForNode, context: CodegenContext) {
   const { source, keyAlias, valueAlias, objectIndexAlias, children } = node
   push(`${RENDER_LIST_HELPER}(`, node)
   genExpression(source, context)
-  context.push(`(`)
+  push(`, (`)
   if (valueAlias) {
     // not using genExpression here because these aliases can only be code
     // that is valid in the function argument position, so the parse rule can
     // be off and they don't need identifier prefixing anyway.
     push(valueAlias.content, valueAlias)
-    push(`, `)
   }
   if (keyAlias) {
     if (!valueAlias) {
-      push(`_`)
+      push(`_`)
     }
-    push(keyAlias.content, keyAlias)
     push(`, `)
+    push(keyAlias.content, keyAlias)
   }
   if (objectIndexAlias) {
     if (!keyAlias) {
       if (!valueAlias) {
-        push(`_, `)
+        push(`_, __`)
+      } else {
+        push(`__`)
       }
-      push(`_, `)
     }
+    push(`, `)
     push(objectIndexAlias.content, objectIndexAlias)
   }
-  context.push(`) => `)
+  push(`) => `)
   genChildren(children, context)
-  context.push(`)`)
+  push(`)`)
 }
 
 // JavaScript
 function genCallExpression(
   node: CallExpression,
   context: CodegenContext,
-  multilines = node.arguments.length > 1
+  multilines = node.arguments.length > 2
 ) {
   context.push(node.callee + `(`, node)
   multilines && context.indent()