Skip to content

Commit

Permalink
fix(compiler-sfc): support runtime Enum in normal script (#4698)
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Oct 8, 2021
1 parent 914e2e3 commit f66d456
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Expand Up @@ -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'
Expand Down
20 changes: 20 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -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">
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -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)
Expand Down Expand Up @@ -1504,6 +1505,7 @@ function walkDeclaration(
}
}
} else if (
node.type === 'TSEnumDeclaration' ||
node.type === 'FunctionDeclaration' ||
node.type === 'ClassDeclaration'
) {
Expand Down

0 comments on commit f66d456

Please sign in to comment.