fix(compiler-sfc): support runtime Enum in normal script (#4698)

This commit is contained in:
edison 2021-10-08 23:57:39 +08:00 committed by GitHub
parent 914e2e3880
commit f66d456b7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 1 deletions

View 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`] = ` exports[`SFC compile <script setup> with TypeScript withDefaults (dynamic) 1`] = `
"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue' "import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
import { defaults } from './foo' import { defaults } from './foo'

View 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', () => { test('const Enum', () => {
const { content, bindings } = compile( const { content, bindings } = compile(
`<script setup lang="ts"> `<script setup lang="ts">

View File

@ -834,7 +834,8 @@ export function compileScript(
} else if ( } else if (
(node.type === 'VariableDeclaration' || (node.type === 'VariableDeclaration' ||
node.type === 'FunctionDeclaration' || node.type === 'FunctionDeclaration' ||
node.type === 'ClassDeclaration') && node.type === 'ClassDeclaration' ||
node.type === 'TSEnumDeclaration') &&
!node.declare !node.declare
) { ) {
walkDeclaration(node, scriptBindings, userImportAlias) walkDeclaration(node, scriptBindings, userImportAlias)
@ -1504,6 +1505,7 @@ function walkDeclaration(
} }
} }
} else if ( } else if (
node.type === 'TSEnumDeclaration' ||
node.type === 'FunctionDeclaration' || node.type === 'FunctionDeclaration' ||
node.type === 'ClassDeclaration' node.type === 'ClassDeclaration'
) { ) {