test: tests for codegen

This commit is contained in:
Evan You
2019-09-24 15:49:02 -04:00
parent 76a1196935
commit 7a46e51815
12 changed files with 486 additions and 26 deletions

View File

@@ -19,10 +19,14 @@ export const transformIf = createStructuralDirectiveTransform(
processExpression(dir.exp, context)
}
if (dir.name === 'if') {
// check if this v-if is root - so that in codegen we can avoid generating
// arrays for each branch
const isRoot = context.parent === context.root
context.replaceNode({
type: NodeTypes.IF,
loc: node.loc,
branches: [createIfBranch(node, dir)]
branches: [createIfBranch(node, dir, isRoot)],
isRoot
})
} else {
// locate the adjacent v-if
@@ -39,7 +43,7 @@ export const transformIf = createStructuralDirectiveTransform(
if (sibling && sibling.type === NodeTypes.IF) {
// move the node to the if node's branches
context.removeNode()
const branch = createIfBranch(node, dir)
const branch = createIfBranch(node, dir, sibling.isRoot)
if (__DEV__ && comments.length) {
branch.children = [...comments, ...branch.children]
}
@@ -63,11 +67,16 @@ export const transformIf = createStructuralDirectiveTransform(
}
)
function createIfBranch(node: ElementNode, dir: DirectiveNode): IfBranchNode {
function createIfBranch(
node: ElementNode,
dir: DirectiveNode,
isRoot: boolean
): IfBranchNode {
return {
type: NodeTypes.IF_BRANCH,
loc: node.loc,
condition: dir.name === 'else' ? undefined : dir.exp,
children: node.tagType === ElementTypes.TEMPLATE ? node.children : [node]
children: node.tagType === ElementTypes.TEMPLATE ? node.children : [node],
isRoot
}
}