Skip to content

Commit

Permalink
fix(compiler-sfc): accept StringLiteral node in defineEmit tuple …
Browse files Browse the repository at this point in the history
…syntax (#8041)

close #8040
  • Loading branch information
equt authored Apr 6, 2023
1 parent 5531ff4 commit 3ccbea0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
11 changes: 11 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,17 @@ const emit = defineEmits(['a', 'b'])
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()', () => {
test('basic usage', () => {
const { content } = compile(`
Expand Down
10 changes: 7 additions & 3 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2316,11 +2316,15 @@ function extractRuntimeEmits(
hasCallSignature = true
}
if (t.type === 'TSPropertySignature') {
if (t.key.type !== 'Identifier' || t.computed) {
if (t.key.type === 'Identifier' && !t.computed) {
emits.add(t.key.name)
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)
}
emits.add(t.key.name)
hasProperty = true
}
}
if (hasCallSignature && hasProperty) {
Expand Down

1 comment on commit 3ccbea0

@vercel
Copy link

@vercel vercel bot commented on 3ccbea0 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.