Skip to content

Commit 1ffd48a

Browse files
committed
fix(compiler-sfc): support TS runtime enum in <script setup>
1 parent f8a6b57 commit 1ffd48a

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,21 @@ return { }
884884
})"
885885
`;
886886
887+
exports[`SFC compile <script setup> with TypeScript runtime Enum 1`] = `
888+
"import { defineComponent as _defineComponent } from 'vue'
889+
enum Foo { A = 123 }
890+
891+
export default _defineComponent({
892+
setup(__props, { expose }) {
893+
expose()
894+
895+
896+
return { Foo }
897+
}
898+
899+
})"
900+
`;
901+
887902
exports[`SFC compile <script setup> with TypeScript withDefaults (dynamic) 1`] = `
888903
"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
889904
import { defaults } from './foo'

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

+12
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,18 @@ const emit = defineEmits(['a', 'b'])
824824
expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
825825
expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`)
826826
})
827+
828+
test('runtime Enum', () => {
829+
const { content, bindings } = compile(
830+
`<script setup lang="ts">
831+
enum Foo { A = 123 }
832+
</script>`
833+
)
834+
assertCode(content)
835+
expect(bindings).toStrictEqual({
836+
Foo: BindingTypes.SETUP_CONST
837+
})
838+
})
827839
})
828840

829841
describe('async/await detection', () => {

packages/compiler-sfc/src/compileScript.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -937,20 +937,6 @@ export function compileScript(
937937
walkDeclaration(node, setupBindings, userImportAlias)
938938
}
939939

940-
// Type declarations
941-
if (node.type === 'VariableDeclaration' && node.declare) {
942-
s.remove(start, end)
943-
}
944-
945-
// move all type declarations to outer scope
946-
if (
947-
node.type.startsWith('TS') ||
948-
(node.type === 'ExportNamedDeclaration' && node.exportKind === 'type')
949-
) {
950-
recordType(node, declaredTypes)
951-
s.move(start, end, 0)
952-
}
953-
954940
// walk statements & named exports / variable declarations for top level
955941
// await
956942
if (
@@ -986,6 +972,24 @@ export function compileScript(
986972
node
987973
)
988974
}
975+
976+
if (isTS) {
977+
// runtime enum
978+
if (node.type === 'TSEnumDeclaration' && !node.const) {
979+
registerBinding(setupBindings, node.id, BindingTypes.SETUP_CONST)
980+
}
981+
982+
// move all Type declarations to outer scope
983+
if (
984+
node.type.startsWith('TS') ||
985+
(node.type === 'ExportNamedDeclaration' &&
986+
node.exportKind === 'type') ||
987+
(node.type === 'VariableDeclaration' && node.declare)
988+
) {
989+
recordType(node, declaredTypes)
990+
s.move(start, end, 0)
991+
}
992+
}
989993
}
990994

991995
// in parse only mode, we should have collected all the information we need,

0 commit comments

Comments
 (0)