]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): fix local var access check for bindings in normal script
authorEvan You <yyx990803@gmail.com>
Wed, 22 Sep 2021 14:10:20 +0000 (10:10 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 22 Sep 2021 14:10:20 +0000 (10:10 -0400)
fix #4644

packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 7afc1d5cabb9b2706b48393b059537a58a2c44e2..20ce70a97e8c10c6962298be12f8c5c87c7438d8 100644 (file)
@@ -1181,6 +1181,19 @@ const emit = defineEmits(['a', 'b'])
         defineEmits([bar])
         </script>`)
       ).toThrow(`cannot reference locally declared variables`)
+
+      // #4644
+      expect(() =>
+        compile(`
+        <script>const bar = 1</script>
+        <script setup>
+        defineProps({
+          foo: {
+            default: () => bar
+          }
+        })
+        </script>`)
+      ).not.toThrow(`cannot reference locally declared variables`)
     })
 
     test('should allow defineProps/Emit() referencing scope var', () => {
index 08eb55883959ef11f3bbc814c67551eabb441248..0dac40b2340fdaff27bf72a0bd671344c291d8b0 100644 (file)
@@ -239,6 +239,7 @@ export function compileScript(
   const helperImports: Set<string> = new Set()
   const userImports: Record<string, ImportBinding> = Object.create(null)
   const userImportAlias: Record<string, string> = Object.create(null)
+  const scriptBindings: Record<string, BindingTypes> = Object.create(null)
   const setupBindings: Record<string, BindingTypes> = Object.create(null)
 
   let defaultExport: Node | undefined
@@ -739,7 +740,7 @@ export function compileScript(
           }
         }
         if (node.declaration) {
-          walkDeclaration(node.declaration, setupBindings, userImportAlias)
+          walkDeclaration(node.declaration, scriptBindings, userImportAlias)
         }
       } else if (
         (node.type === 'VariableDeclaration' ||
@@ -747,7 +748,7 @@ export function compileScript(
           node.type === 'ClassDeclaration') &&
         !node.declare
       ) {
-        walkDeclaration(node, setupBindings, userImportAlias)
+        walkDeclaration(node, scriptBindings, userImportAlias)
       }
     }
 
@@ -1070,6 +1071,9 @@ export function compileScript(
         ? BindingTypes.SETUP_CONST
         : BindingTypes.SETUP_MAYBE_REF
   }
+  for (const key in scriptBindings) {
+    bindingMetadata[key] = scriptBindings[key]
+  }
   for (const key in setupBindings) {
     bindingMetadata[key] = setupBindings[key]
   }
@@ -1198,8 +1202,11 @@ export function compileScript(
       returned = `() => {}`
     }
   } else {
-    // return bindings from setup
-    const allBindings: Record<string, any> = { ...setupBindings }
+    // return bindings from script and script setup
+    const allBindings: Record<string, any> = {
+      ...scriptBindings,
+      ...setupBindings
+    }
     for (const key in userImports) {
       if (!userImports[key].isType && userImports[key].isUsedInTemplate) {
         allBindings[key] = true