wip: improve v-if & v-for codegen output formatting

This commit is contained in:
Evan You 2019-09-22 21:10:22 -04:00
parent e8463f1198
commit 3ab016e44f

View File

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