Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -840,7 +840,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 @@ -1510,6 +1511,7 @@ function walkDeclaration(
}
}
} else if (
node.type === 'TSEnumDeclaration' ||
node.type === 'FunctionDeclaration' ||
node.type === 'ClassDeclaration'
) {
Expand Down