]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): should also expose regular script block bindings when `<script...
authorEvan You <yyx990803@gmail.com>
Tue, 17 Aug 2021 19:52:48 +0000 (15:52 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 17 Aug 2021 19:52:48 +0000 (15:52 -0400)
close #4369

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

index c282b7eb4d6725aa49ec508d52fc3fb3410b9232..7e3a35a7cf0ba6ca8b4baeade32c8d797ec08c60 100644 (file)
@@ -610,10 +610,15 @@ export default {
       function c() {}
       class d {}
       
-return { a, b, c, d, x }
+return { aa, bb, cc, dd, a, b, c, d, xx, x }
 }
 
-}"
+}
+      import { xx } from './x'
+      let aa = 1
+      const bb = 2
+      function cc() {}
+      class dd {}"
 `;
 
 exports[`SFC compile <script setup> with TypeScript const Enum 1`] = `
index 2bf7f1fd2e320189823b3e2d46de2fe4a2a63e13..5688805601264ba046304450fb1b1394500fa22f 100644 (file)
@@ -3,7 +3,7 @@ import { compileSFCScript as compile, assertCode } from './utils'
 
 describe('SFC compile <script setup>', () => {
   test('should expose top level declarations', () => {
-    const { content } = compile(`
+    const { content, bindings } = compile(`
       <script setup>
       import { x } from './x'
       let a = 1
@@ -11,9 +11,29 @@ describe('SFC compile <script setup>', () => {
       function c() {}
       class d {}
       </script>
+
+      <script>
+      import { xx } from './x'
+      let aa = 1
+      const bb = 2
+      function cc() {}
+      class dd {}
+      </script>
       `)
+    expect(content).toMatch('return { aa, bb, cc, dd, a, b, c, d, xx, x }')
+    expect(bindings).toStrictEqual({
+      x: BindingTypes.SETUP_MAYBE_REF,
+      a: BindingTypes.SETUP_LET,
+      b: BindingTypes.SETUP_CONST,
+      c: BindingTypes.SETUP_CONST,
+      d: BindingTypes.SETUP_CONST,
+      xx: BindingTypes.SETUP_MAYBE_REF,
+      aa: BindingTypes.SETUP_LET,
+      bb: BindingTypes.SETUP_CONST,
+      cc: BindingTypes.SETUP_CONST,
+      dd: BindingTypes.SETUP_CONST
+    })
     assertCode(content)
-    expect(content).toMatch('return { a, b, c, d, x }')
   })
 
   test('defineProps()', () => {
index 68129564e1c5701ff8370c56c5af976fe0b7beb3..5aa8a9f5533b0079a9a22c04c79a8c8034023aea 100644 (file)
@@ -827,6 +827,13 @@ export function compileScript(
             )
           }
         }
+      } else if (
+        (node.type === 'VariableDeclaration' ||
+          node.type === 'FunctionDeclaration' ||
+          node.type === 'ClassDeclaration') &&
+        !node.declare
+      ) {
+        walkDeclaration(node, setupBindings, userImportAlias)
       }
     }
   }