Skip to content

Commit

Permalink
fix(compiler-sfc): Optimize the value of emitIdentifier (#12851)
Browse files Browse the repository at this point in the history
  • Loading branch information
webfansplz authored Oct 23, 2023
1 parent 099401e commit bb59751
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,10 @@ export function compileScript(
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
13 changes: 13 additions & 0 deletions packages/compiler-sfc/test/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,19 @@ const emit = defineEmits(['a', 'b'])
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', () => {
const { content, bindings } = compile(
`<script setup lang="ts">
Expand Down

0 comments on commit bb59751

Please sign in to comment.