]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): support runtime Enum in normal script (#4698)
authoredison <daiwei521@126.com>
Fri, 8 Oct 2021 15:57:39 +0000 (23:57 +0800)
committerGitHub <noreply@github.com>
Fri, 8 Oct 2021 15:57:39 +0000 (11:57 -0400)
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 9dbe37cc9dea43771ad1e9f99b2ee014b05716c4..79d598ae6f3f71582ce96092fe7143c54670cae4 100644 (file)
@@ -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'
index ff4279ecd632aecde2ce6656990ad2ec22b4725c..f513eea00563dc5182f084f0c58c6090f5717be9 100644 (file)
@@ -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">
index 1f1336fd1071160dc18ebbb012d48d99632108bb..daef402a4863f43a26f0dc0c2852807d0bd0edce 100644 (file)
@@ -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'
   ) {