浏览代码

fix(compiler-sfc): support runtime Enum in normal script (#4698)

edison 4 年之前
父节点
当前提交
f66d456b7a

+ 19 - 0
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

@@ -1323,6 +1323,25 @@ return { Foo }
 })"
 `;
 
+exports[`SFC compile <script setup> with TypeScript runtime Enum in normal script 1`] = `
+"import { defineComponent as _defineComponent } from 'vue'
+enum Foo { A = 123 }
+        
+          export enum D { D = \\"D\\" }
+          const enum C { C = \\"C\\" }
+          enum B { B = \\"B\\" }
+        
+export default /*#__PURE__*/_defineComponent({
+  setup(__props, { expose }) {
+  expose()
+
+        
+return { D, C, B, Foo }
+}
+
+})"
+`;
+
 exports[`SFC compile <script setup> with TypeScript withDefaults (dynamic) 1`] = `
 "import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
 import { defaults } from './foo'

+ 20 - 0
packages/compiler-sfc/__tests__/compileScript.spec.ts

@@ -1042,6 +1042,26 @@ const emit = defineEmits(['a', 'b'])
       })
     })
 
+    test('runtime Enum in normal script', () => {
+      const { content, bindings } = compile(
+        `<script lang="ts">
+          export enum D { D = "D" }
+          const enum C { C = "C" }
+          enum B { B = "B" }
+        </script>
+        <script setup lang="ts">
+        enum Foo { A = 123 }
+        </script>`
+      )
+      assertCode(content)
+      expect(bindings).toStrictEqual({
+        D: BindingTypes.SETUP_CONST,
+        C: BindingTypes.SETUP_CONST,
+        B: BindingTypes.SETUP_CONST,
+        Foo: BindingTypes.SETUP_CONST
+      })
+    })
+
     test('const Enum', () => {
       const { content, bindings } = compile(
         `<script setup lang="ts">

+ 3 - 1
packages/compiler-sfc/src/compileScript.ts

@@ -834,7 +834,8 @@ export function compileScript(
       } else if (
         (node.type === 'VariableDeclaration' ||
           node.type === 'FunctionDeclaration' ||
-          node.type === 'ClassDeclaration') &&
+          node.type === 'ClassDeclaration' ||
+          node.type === 'TSEnumDeclaration') &&
         !node.declare
       ) {
         walkDeclaration(node, scriptBindings, userImportAlias)
@@ -1504,6 +1505,7 @@ function walkDeclaration(
       }
     }
   } else if (
+    node.type === 'TSEnumDeclaration' ||
     node.type === 'FunctionDeclaration' ||
     node.type === 'ClassDeclaration'
   ) {