test: tests for codegen
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user