fix(compiler-sfc): Optimize the value of emitIdentifier (#12851)

This commit is contained in:
webfansplz 2023-10-23 15:08:17 +08:00 committed by GitHub
parent 099401e227
commit bb59751dd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View File

@ -414,7 +414,10 @@ export function compileScript(
} }
if (declId) { if (declId) {
emitIdentifier = scriptSetup!.content.slice(declId.start!, declId.end!) emitIdentifier =
declId.type === 'Identifier'
? declId.name
: scriptSetup!.content.slice(declId.start!, declId.end!)
} }
return true return true

View File

@ -558,6 +558,22 @@ export default /*#__PURE__*/_defineComponent({
return { emit }
}
})"
`;
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (interface ts type) 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
interface Emits { (e: 'foo'): void }
export default /*#__PURE__*/_defineComponent({
emits: ['foo'],
setup(__props, { emit }) {
return { emit } return { emit }
} }

View File

@ -1081,6 +1081,19 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(`emits: ["foo", "bar"]`) expect(content).toMatch(`emits: ["foo", "bar"]`)
}) })
// https://github.com/vuejs/core/issues/5393
test('defineEmits w/ type (interface ts type)', () => {
const { content } = compile(`
<script setup lang="ts">
interface Emits { (e: 'foo'): void }
const emit: Emits = defineEmits(['foo'])
</script>
`)
assertCode(content)
expect(content).toMatch(`setup(__props, { emit }) {`)
expect(content).toMatch(`emits: ['foo']`)
})
test('runtime Enum', () => { test('runtime Enum', () => {
const { content, bindings } = compile( const { content, bindings } = compile(
`<script setup lang="ts"> `<script setup lang="ts">