Skip to content

Commit f66d456

Browse files
authored
fix(compiler-sfc): support runtime Enum in normal script (#4698)
1 parent 914e2e3 commit f66d456

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,25 @@ return { Foo }
13231323
})"
13241324
`;
13251325
1326+
exports[`SFC compile <script setup> with TypeScript runtime Enum in normal script 1`] = `
1327+
"import { defineComponent as _defineComponent } from 'vue'
1328+
enum Foo { A = 123 }
1329+
1330+
export enum D { D = \\"D\\" }
1331+
const enum C { C = \\"C\\" }
1332+
enum B { B = \\"B\\" }
1333+
1334+
export default /*#__PURE__*/_defineComponent({
1335+
setup(__props, { expose }) {
1336+
expose()
1337+
1338+
1339+
return { D, C, B, Foo }
1340+
}
1341+
1342+
})"
1343+
`;
1344+
13261345
exports[`SFC compile <script setup> with TypeScript withDefaults (dynamic) 1`] = `
13271346
"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
13281347
import { defaults } from './foo'

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

+20
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,26 @@ const emit = defineEmits(['a', 'b'])
10421042
})
10431043
})
10441044

1045+
test('runtime Enum in normal script', () => {
1046+
const { content, bindings } = compile(
1047+
`<script lang="ts">
1048+
export enum D { D = "D" }
1049+
const enum C { C = "C" }
1050+
enum B { B = "B" }
1051+
</script>
1052+
<script setup lang="ts">
1053+
enum Foo { A = 123 }
1054+
</script>`
1055+
)
1056+
assertCode(content)
1057+
expect(bindings).toStrictEqual({
1058+
D: BindingTypes.SETUP_CONST,
1059+
C: BindingTypes.SETUP_CONST,
1060+
B: BindingTypes.SETUP_CONST,
1061+
Foo: BindingTypes.SETUP_CONST
1062+
})
1063+
})
1064+
10451065
test('const Enum', () => {
10461066
const { content, bindings } = compile(
10471067
`<script setup lang="ts">

packages/compiler-sfc/src/compileScript.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,8 @@ export function compileScript(
834834
} else if (
835835
(node.type === 'VariableDeclaration' ||
836836
node.type === 'FunctionDeclaration' ||
837-
node.type === 'ClassDeclaration') &&
837+
node.type === 'ClassDeclaration' ||
838+
node.type === 'TSEnumDeclaration') &&
838839
!node.declare
839840
) {
840841
walkDeclaration(node, scriptBindings, userImportAlias)
@@ -1504,6 +1505,7 @@ function walkDeclaration(
15041505
}
15051506
}
15061507
} else if (
1508+
node.type === 'TSEnumDeclaration' ||
15071509
node.type === 'FunctionDeclaration' ||
15081510
node.type === 'ClassDeclaration'
15091511
) {

0 commit comments

Comments
 (0)