fix(compiler-sfc): accept `StringLiteral` node in `defineEmit` tuple syntax (#8041)

close #8040
This commit is contained in:
-isum 2023-04-06 17:13:34 +08:00 committed by GitHub
parent 5531ff4eb0
commit 3ccbea08e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 3 deletions

View File

@ -1457,6 +1457,22 @@ export default /*#__PURE__*/_defineComponent({
return { emit }
}
})"
`;
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (property syntax string literal) 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
export default /*#__PURE__*/_defineComponent({
emits: [\\"foo:bar\\"],
setup(__props, { expose: __expose, emit }) {
__expose();
return { emit } return { emit }
} }

View File

@ -1629,6 +1629,17 @@ const emit = defineEmits(['a', 'b'])
assertCode(content) assertCode(content)
}) })
// #8040
test('defineEmits w/ type (property syntax string literal)', () => {
const { content } = compile(`
<script setup lang="ts">
const emit = defineEmits<{ 'foo:bar': [] }>()
</script>
`)
expect(content).toMatch(`emits: ["foo:bar"]`)
assertCode(content)
})
describe('defineSlots()', () => { describe('defineSlots()', () => {
test('basic usage', () => { test('basic usage', () => {
const { content } = compile(` const { content } = compile(`

View File

@ -2316,11 +2316,15 @@ function extractRuntimeEmits(
hasCallSignature = true hasCallSignature = true
} }
if (t.type === 'TSPropertySignature') { if (t.type === 'TSPropertySignature') {
if (t.key.type !== 'Identifier' || t.computed) { if (t.key.type === 'Identifier' && !t.computed) {
error(`defineEmits() type cannot use computed keys.`, t.key)
}
emits.add(t.key.name) emits.add(t.key.name)
hasProperty = true hasProperty = true
} else if (t.key.type === 'StringLiteral' && !t.computed) {
emits.add(t.key.value)
hasProperty = true
} else {
error(`defineEmits() type cannot use computed keys.`, t.key)
}
} }
} }
if (hasCallSignature && hasProperty) { if (hasCallSignature && hasProperty) {