fix(compiler-core): properly transform replaced nodes (#2927)
This commit is contained in:
parent
5db2b141dc
commit
0fe567abfc
@ -3,7 +3,8 @@ import {
|
|||||||
baseParse as parse,
|
baseParse as parse,
|
||||||
transform,
|
transform,
|
||||||
ErrorCodes,
|
ErrorCodes,
|
||||||
BindingTypes
|
BindingTypes,
|
||||||
|
NodeTransform
|
||||||
} from '../../src'
|
} from '../../src'
|
||||||
import {
|
import {
|
||||||
RESOLVE_COMPONENT,
|
RESOLVE_COMPONENT,
|
||||||
@ -939,4 +940,35 @@ describe('compiler: element transform', () => {
|
|||||||
isBlock: true
|
isBlock: true
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('should process node when node has been replaced', () => {
|
||||||
|
// a NodeTransform that swaps out <div id="foo" /> with <span id="foo" />
|
||||||
|
const customNodeTransform: NodeTransform = (node, context) => {
|
||||||
|
if (
|
||||||
|
node.type === NodeTypes.ELEMENT &&
|
||||||
|
node.tag === 'div' &&
|
||||||
|
node.props.some(
|
||||||
|
prop =>
|
||||||
|
prop.type === NodeTypes.ATTRIBUTE &&
|
||||||
|
prop.name === 'id' &&
|
||||||
|
prop.value &&
|
||||||
|
prop.value.content === 'foo'
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
context.replaceNode({
|
||||||
|
...node,
|
||||||
|
tag: 'span'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const ast = parse(`<div><div id="foo" /></div>`)
|
||||||
|
transform(ast, {
|
||||||
|
nodeTransforms: [transformElement, transformText, customNodeTransform]
|
||||||
|
})
|
||||||
|
expect((ast as any).children[0].children[0].codegenNode).toMatchObject({
|
||||||
|
type: NodeTypes.VNODE_CALL,
|
||||||
|
tag: '"span"',
|
||||||
|
isBlock: false
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -62,18 +62,21 @@ const directiveImportMap = new WeakMap<DirectiveNode, symbol>()
|
|||||||
|
|
||||||
// generate a JavaScript AST for this element's codegen
|
// generate a JavaScript AST for this element's codegen
|
||||||
export const transformElement: NodeTransform = (node, context) => {
|
export const transformElement: NodeTransform = (node, context) => {
|
||||||
if (
|
|
||||||
!(
|
|
||||||
node.type === NodeTypes.ELEMENT &&
|
|
||||||
(node.tagType === ElementTypes.ELEMENT ||
|
|
||||||
node.tagType === ElementTypes.COMPONENT)
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// perform the work on exit, after all child expressions have been
|
// perform the work on exit, after all child expressions have been
|
||||||
// processed and merged.
|
// processed and merged.
|
||||||
return function postTransformElement() {
|
return function postTransformElement() {
|
||||||
|
node = context.currentNode!
|
||||||
|
|
||||||
|
if (
|
||||||
|
!(
|
||||||
|
node.type === NodeTypes.ELEMENT &&
|
||||||
|
(node.tagType === ElementTypes.ELEMENT ||
|
||||||
|
node.tagType === ElementTypes.COMPONENT)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const { tag, props } = node
|
const { tag, props } = node
|
||||||
const isComponent = node.tagType === ElementTypes.COMPONENT
|
const isComponent = node.tagType === ElementTypes.COMPONENT
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user