]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): fix binding type for constants when hoistStatic is disabled (...
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Thu, 6 Apr 2023 09:19:00 +0000 (17:19 +0800)
committerGitHub <noreply@github.com>
Thu, 6 Apr 2023 09:19:00 +0000 (17:19 +0800)
packages/compiler-sfc/__tests__/__snapshots__/compileScriptHoistStatic.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/__tests__/compileScriptHoistStatic.spec.ts
packages/compiler-sfc/src/compileScript.ts

index ed5301432e82aa629fdb314c9808101ba2abae9a..3b94955f2db2ebcfaa07b6da76e863397552670b 100644 (file)
@@ -130,3 +130,15 @@ return () => {}
 
 }"
 `;
+
+exports[`sfc hoist static > should not hoist when disabled 1`] = `
+"export default {
+  setup(__props) {
+
+    const foo = 'bar'
+    
+return () => {}
+}
+
+}"
+`;
index 1d8d20ec1a08e7b512f2c4607abec59b9397aee7..2da79cf8312b80670d2a009a7b64fe715db7a0ea 100644 (file)
@@ -39,7 +39,7 @@ describe('SFC compile <script setup>', () => {
     expect(bindings).toStrictEqual({
       x: BindingTypes.SETUP_MAYBE_REF,
       a: BindingTypes.SETUP_LET,
-      b: BindingTypes.LITERAL_CONST,
+      b: BindingTypes.SETUP_CONST,
       c: BindingTypes.SETUP_CONST,
       d: BindingTypes.SETUP_CONST,
       xx: BindingTypes.SETUP_MAYBE_REF,
index 032a8deb0daf4a2d860e4f14a6fbee8106a8fd75..4879dd5f924ede9d96d6349b5d5b7eead7ca8a2a 100644 (file)
@@ -183,7 +183,22 @@ describe('sfc hoist static', () => {
     </script>
     `)
     expect(bindings).toStrictEqual({
-      foo: BindingTypes.LITERAL_CONST
+      foo: BindingTypes.SETUP_CONST
+    })
+    assertCode(content)
+  })
+
+  test('should not hoist when disabled', () => {
+    const { content, bindings } = compile(
+      `
+    <script setup>
+    const foo = 'bar'
+    </script>
+    `,
+      { hoistStatic: false }
+    )
+    expect(bindings).toStrictEqual({
+      foo: BindingTypes.SETUP_CONST
     })
     assertCode(content)
   })
index 31114c52d8cc3ac3c793346018ebc7f7209e4fcc..b00c17799c0b7ea224298f266dd4c7e604f1a67e 100644 (file)
@@ -803,7 +803,7 @@ export function compileScript(
     if (!node) return
     walkIdentifiers(node, id => {
       const binding = setupBindings[id.name]
-      if (binding && (binding !== BindingTypes.LITERAL_CONST || !hoistStatic)) {
+      if (binding && binding !== BindingTypes.LITERAL_CONST) {
         error(
           `\`${method}()\` in <script setup> cannot reference locally ` +
             `declared variables because it will be hoisted outside of the ` +
@@ -1258,7 +1258,13 @@ export function compileScript(
           }
         }
         if (node.declaration) {
-          walkDeclaration(node.declaration, scriptBindings, vueImportAliases)
+          walkDeclaration(
+            'script',
+            node.declaration,
+            scriptBindings,
+            vueImportAliases,
+            hoistStatic
+          )
         }
       } else if (
         (node.type === 'VariableDeclaration' ||
@@ -1267,7 +1273,13 @@ export function compileScript(
           node.type === 'TSEnumDeclaration') &&
         !node.declare
       ) {
-        walkDeclaration(node, scriptBindings, vueImportAliases)
+        walkDeclaration(
+          'script',
+          node,
+          scriptBindings,
+          vueImportAliases,
+          hoistStatic
+        )
       }
     }
 
@@ -1394,7 +1406,13 @@ export function compileScript(
         node.type === 'TSEnumDeclaration') &&
       !node.declare
     ) {
-      isAllLiteral = walkDeclaration(node, setupBindings, vueImportAliases)
+      isAllLiteral = walkDeclaration(
+        'scriptSetup',
+        node,
+        setupBindings,
+        vueImportAliases,
+        hoistStatic
+      )
     }
 
     // hoist literal constants
@@ -1891,9 +1909,11 @@ function registerBinding(
 }
 
 function walkDeclaration(
+  from: 'script' | 'scriptSetup',
   node: Declaration,
   bindings: Record<string, BindingTypes>,
-  userImportAliases: Record<string, string>
+  userImportAliases: Record<string, string>,
+  hoistStatic: boolean
 ): boolean {
   let isAllLiteral = false
 
@@ -1918,7 +1938,10 @@ function walkDeclaration(
       if (id.type === 'Identifier') {
         let bindingType
         const userReactiveBinding = userImportAliases['reactive']
-        if (isAllLiteral || (isConst && isStaticNode(init!))) {
+        if (
+          (hoistStatic || from === 'script') &&
+          (isAllLiteral || (isConst && isStaticNode(init!)))
+        ) {
           bindingType = BindingTypes.LITERAL_CONST
         } else if (isCallOf(init, userReactiveBinding)) {
           // treat reactive() calls as let since it's meant to be mutable