From 920773fc6b082269e5c54a9c44609e6fd7dba5f3 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Mon, 21 Oct 2019 22:29:17 +0800 Subject: [PATCH] feat(compiler-core): hoist element with static ref (#344) --- .../__snapshots__/hoistStatic.spec.ts.snap | 17 +++++++++ .../__tests__/transforms/hoistStatic.spec.ts | 37 +++++++++++++++++++ .../transforms/transformElement.spec.ts | 11 ++++-- .../src/transforms/transformElement.ts | 3 -- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap b/packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap index 1bb57f3de..267445b40 100644 --- a/packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap +++ b/packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap @@ -169,6 +169,23 @@ return function render() { }" `; +exports[`compiler: hoistStatic transform prefixIdentifiers hoist element with static ref 1`] = ` +"const _Vue = Vue +const _createVNode = Vue.createVNode + +const _hoisted_1 = _createVNode(\\"span\\", { ref: \\"o\\" }) + +return function render() { + with (this) { + const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue + + return (_openBlock(), _createBlock(\\"div\\", null, [ + _hoisted_1 + ])) + } +}" +`; + exports[`compiler: hoistStatic transform prefixIdentifiers hoist nested static tree with static interpolation 1`] = ` "const _Vue = Vue const _createVNode = Vue.createVNode diff --git a/packages/compiler-core/__tests__/transforms/hoistStatic.spec.ts b/packages/compiler-core/__tests__/transforms/hoistStatic.spec.ts index 1fb81f931..719aee704 100644 --- a/packages/compiler-core/__tests__/transforms/hoistStatic.spec.ts +++ b/packages/compiler-core/__tests__/transforms/hoistStatic.spec.ts @@ -677,5 +677,42 @@ describe('compiler: hoistStatic transform', () => { }).code ).toMatchSnapshot() }) + + test('hoist element with static ref', () => { + const { root, args } = transformWithHoist( + `
`, + { + prefixIdentifiers: true + } + ) + + expect(root.hoists.length).toBe(1) + expect(root.hoists).toMatchObject([ + { + type: NodeTypes.JS_CALL_EXPRESSION, + callee: CREATE_VNODE, + arguments: [ + `"span"`, + createObjectMatcher({ + ref: `o` + }) + ] + } + ]) + expect(args).toMatchObject([ + `"div"`, + `null`, + [ + { + type: NodeTypes.ELEMENT, + codegenNode: { + type: NodeTypes.SIMPLE_EXPRESSION, + content: `_hoisted_1` + } + } + ] + ]) + expect(generate(root).code).toMatchSnapshot() + }) }) }) diff --git a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts index 74b2cc878..b8ef7543b 100644 --- a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts @@ -699,10 +699,15 @@ describe('compiler: element transform', () => { expect(node.arguments[3]).toBe(genFlagText(PatchFlags.FULL_PROPS)) }) - test('NEED_PATCH (static ref)', () => { + test('NO NEED_PATCH (static ref)', () => { const { node } = parseWithBind(`
`) - expect(node.arguments.length).toBe(4) - expect(node.arguments[3]).toBe(genFlagText(PatchFlags.NEED_PATCH)) + expect(node.arguments.length).toBe(2) + expect(node.arguments).toMatchObject([ + `"div"`, + createObjectMatcher({ + ref: `foo` + }) + ]) }) test('NEED_PATCH (dynamic ref)', () => { diff --git a/packages/compiler-core/src/transforms/transformElement.ts b/packages/compiler-core/src/transforms/transformElement.ts index 646a51ce6..99f1c6752 100644 --- a/packages/compiler-core/src/transforms/transformElement.ts +++ b/packages/compiler-core/src/transforms/transformElement.ts @@ -249,9 +249,6 @@ export function buildProps( const prop = props[i] if (prop.type === NodeTypes.ATTRIBUTE) { const { loc, name, value } = prop - if (name === 'ref') { - hasRef = true - } properties.push( createObjectProperty( createSimpleExpression(