fix(compiler-vapor): don't mutate ast

This commit is contained in:
三咲智子 Kevin Deng 2024-01-28 03:28:27 +08:00
parent 7e47ae17f9
commit d3baff92b1
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
2 changed files with 13 additions and 10 deletions

View File

@ -404,14 +404,8 @@ export function createStructuralDirectiveTransform(
return
}
const exitFns = []
for (let i = 0; i < props.length; i++) {
const prop = props[i]
for (const prop of props) {
if (prop.type === NodeTypes.DIRECTIVE && matches(prop.name)) {
// structural directives are removed to avoid infinite recursion
// also we remove them *before* applying so that it can further
// traverse itself in case it moves the node around
props.splice(i, 1)
i--
const onExit = fn(node, prop as VaporDirectiveNode, context)
if (onExit) exitFns.push(onExit)
}

View File

@ -65,16 +65,25 @@ export function createIfBranch(
node.tagType !== ElementTypes.TEMPLATE
) {
node = extend({}, node, {
type: NodeTypes.ELEMENT,
tag: 'template',
props: [],
tagType: ElementTypes.TEMPLATE,
children: [node],
} as TemplateNode)
children: [
extend({}, node, {
props: node.props.filter(
(p) => p.type !== NodeTypes.DIRECTIVE && p.name !== 'if',
),
} as TemplateChildNode),
],
} as Partial<TemplateNode>)
context.node = node
}
const branch: BlockFunctionIRNode = {
type: IRNodeTypes.BLOCK_FUNCTION,
loc: dir.loc,
node: node,
node,
templateIndex: -1,
dynamic: {
id: null,