]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-core): hoist element with static ref (#344)
authorHcySunYang <HcySunYang@outlook.com>
Mon, 21 Oct 2019 14:29:17 +0000 (22:29 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 21 Oct 2019 14:29:17 +0000 (10:29 -0400)
packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap
packages/compiler-core/__tests__/transforms/hoistStatic.spec.ts
packages/compiler-core/__tests__/transforms/transformElement.spec.ts
packages/compiler-core/src/transforms/transformElement.ts

index 1bb57f3dee0c9a5f9fc5462055816d2cb97022e9..267445b4054cf6dd61130e97e3641fab6e9bfd7e 100644 (file)
@@ -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
index 1fb81f93171a0319097a65418866b1b589f91a75..719aee704debd57b56e87d9ab857d0ffa89146e4 100644 (file)
@@ -677,5 +677,42 @@ describe('compiler: hoistStatic transform', () => {
         }).code
       ).toMatchSnapshot()
     })
+
+    test('hoist element with static ref', () => {
+      const { root, args } = transformWithHoist(
+        `<div><span ref="o"></span></div>`,
+        {
+          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()
+    })
   })
 })
index 74b2cc878e2753f1fc06fd1248f1a726f8a60454..b8ef7543b72aaefddb1ab2b3b0cbf4f555994e27 100644 (file)
@@ -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(`<div ref="foo" />`)
-      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)', () => {
index 646a51ce61931b53e5c042f595789602ac1e4e87..99f1c675278f97bf9aecfc67a36d7168456088ae 100644 (file)
@@ -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(