]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): support TS runtime enum in `<script setup>`
authorEvan You <yyx990803@gmail.com>
Wed, 30 Jun 2021 16:03:42 +0000 (12:03 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 30 Jun 2021 16:03:42 +0000 (12:03 -0400)
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 792de768a0104f87146f28d0d0d79d6af215fa0c..638c1629e4c25022aa229295e2709e54cc9251cb 100644 (file)
@@ -884,6 +884,21 @@ return {  }
 })"
 `;
 
+exports[`SFC compile <script setup> with TypeScript runtime Enum 1`] = `
+"import { defineComponent as _defineComponent } from 'vue'
+enum Foo { A = 123 }
+        
+export default _defineComponent({
+  setup(__props, { expose }) {
+  expose()
+
+        
+return { 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 5c98f39ee931fc27e3c80775f27ef8b473f19133..1da416b36fb6d6a37df897f2e403d594b6944012 100644 (file)
@@ -824,6 +824,18 @@ const emit = defineEmits(['a', 'b'])
       expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
       expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`)
     })
+
+    test('runtime Enum', () => {
+      const { content, bindings } = compile(
+        `<script setup lang="ts">
+        enum Foo { A = 123 }
+        </script>`
+      )
+      assertCode(content)
+      expect(bindings).toStrictEqual({
+        Foo: BindingTypes.SETUP_CONST
+      })
+    })
   })
 
   describe('async/await detection', () => {
index ade6d2d2e0354aa3667d4dc3f2f7a0c4e64c29c2..bf4cec6719ec83ef69e5d3a809b8320acc6084c2 100644 (file)
@@ -937,20 +937,6 @@ export function compileScript(
       walkDeclaration(node, setupBindings, userImportAlias)
     }
 
-    // Type declarations
-    if (node.type === 'VariableDeclaration' && node.declare) {
-      s.remove(start, end)
-    }
-
-    // move all type declarations to outer scope
-    if (
-      node.type.startsWith('TS') ||
-      (node.type === 'ExportNamedDeclaration' && node.exportKind === 'type')
-    ) {
-      recordType(node, declaredTypes)
-      s.move(start, end, 0)
-    }
-
     // walk statements & named exports / variable declarations for top level
     // await
     if (
@@ -986,6 +972,24 @@ export function compileScript(
         node
       )
     }
+
+    if (isTS) {
+      // runtime enum
+      if (node.type === 'TSEnumDeclaration' && !node.const) {
+        registerBinding(setupBindings, node.id, BindingTypes.SETUP_CONST)
+      }
+
+      // move all Type declarations to outer scope
+      if (
+        node.type.startsWith('TS') ||
+        (node.type === 'ExportNamedDeclaration' &&
+          node.exportKind === 'type') ||
+        (node.type === 'VariableDeclaration' && node.declare)
+      ) {
+        recordType(node, declaredTypes)
+        s.move(start, end, 0)
+      }
+    }
   }
 
   // in parse only mode, we should have collected all the information we need,