Skip to content

Commit 920773f

Browse files
HcySunYangyyx990803
authored andcommitted
feat(compiler-core): hoist element with static ref (vuejs#344)
1 parent 27f3c2d commit 920773f

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,23 @@ return function render() {
169169
}"
170170
`;
171171
172+
exports[`compiler: hoistStatic transform prefixIdentifiers hoist element with static ref 1`] = `
173+
"const _Vue = Vue
174+
const _createVNode = Vue.createVNode
175+
176+
const _hoisted_1 = _createVNode(\\"span\\", { ref: \\"o\\" })
177+
178+
return function render() {
179+
with (this) {
180+
const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue
181+
182+
return (_openBlock(), _createBlock(\\"div\\", null, [
183+
_hoisted_1
184+
]))
185+
}
186+
}"
187+
`;
188+
172189
exports[`compiler: hoistStatic transform prefixIdentifiers hoist nested static tree with static interpolation 1`] = `
173190
"const _Vue = Vue
174191
const _createVNode = Vue.createVNode

packages/compiler-core/__tests__/transforms/hoistStatic.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,5 +677,42 @@ describe('compiler: hoistStatic transform', () => {
677677
}).code
678678
).toMatchSnapshot()
679679
})
680+
681+
test('hoist element with static ref', () => {
682+
const { root, args } = transformWithHoist(
683+
`<div><span ref="o"></span></div>`,
684+
{
685+
prefixIdentifiers: true
686+
}
687+
)
688+
689+
expect(root.hoists.length).toBe(1)
690+
expect(root.hoists).toMatchObject([
691+
{
692+
type: NodeTypes.JS_CALL_EXPRESSION,
693+
callee: CREATE_VNODE,
694+
arguments: [
695+
`"span"`,
696+
createObjectMatcher({
697+
ref: `o`
698+
})
699+
]
700+
}
701+
])
702+
expect(args).toMatchObject([
703+
`"div"`,
704+
`null`,
705+
[
706+
{
707+
type: NodeTypes.ELEMENT,
708+
codegenNode: {
709+
type: NodeTypes.SIMPLE_EXPRESSION,
710+
content: `_hoisted_1`
711+
}
712+
}
713+
]
714+
])
715+
expect(generate(root).code).toMatchSnapshot()
716+
})
680717
})
681718
})

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,15 @@ describe('compiler: element transform', () => {
699699
expect(node.arguments[3]).toBe(genFlagText(PatchFlags.FULL_PROPS))
700700
})
701701

702-
test('NEED_PATCH (static ref)', () => {
702+
test('NO NEED_PATCH (static ref)', () => {
703703
const { node } = parseWithBind(`<div ref="foo" />`)
704-
expect(node.arguments.length).toBe(4)
705-
expect(node.arguments[3]).toBe(genFlagText(PatchFlags.NEED_PATCH))
704+
expect(node.arguments.length).toBe(2)
705+
expect(node.arguments).toMatchObject([
706+
`"div"`,
707+
createObjectMatcher({
708+
ref: `foo`
709+
})
710+
])
706711
})
707712

708713
test('NEED_PATCH (dynamic ref)', () => {

packages/compiler-core/src/transforms/transformElement.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,6 @@ export function buildProps(
249249
const prop = props[i]
250250
if (prop.type === NodeTypes.ATTRIBUTE) {
251251
const { loc, name, value } = prop
252-
if (name === 'ref') {
253-
hasRef = true
254-
}
255252
properties.push(
256253
createObjectProperty(
257254
createSimpleExpression(

0 commit comments

Comments
 (0)