Skip to content

Commit bb59751

Browse files
authored
fix(compiler-sfc): Optimize the value of emitIdentifier (#12851)
1 parent 099401e commit bb59751

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

Diff for: packages/compiler-sfc/src/compileScript.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,10 @@ export function compileScript(
414414
}
415415

416416
if (declId) {
417-
emitIdentifier = scriptSetup!.content.slice(declId.start!, declId.end!)
417+
emitIdentifier =
418+
declId.type === 'Identifier'
419+
? declId.name
420+
: scriptSetup!.content.slice(declId.start!, declId.end!)
418421
}
419422

420423
return true

Diff for: packages/compiler-sfc/test/__snapshots__/compileScript.spec.ts.snap

+16
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,22 @@ export default /*#__PURE__*/_defineComponent({
558558
559559
560560
561+
return { emit }
562+
}
563+
564+
})"
565+
`;
566+
567+
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (interface ts type) 1`] = `
568+
"import { defineComponent as _defineComponent } from 'vue'
569+
interface Emits { (e: 'foo'): void }
570+
571+
export default /*#__PURE__*/_defineComponent({
572+
emits: ['foo'],
573+
setup(__props, { emit }) {
574+
575+
576+
561577
return { emit }
562578
}
563579

Diff for: packages/compiler-sfc/test/compileScript.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,19 @@ const emit = defineEmits(['a', 'b'])
10811081
expect(content).toMatch(`emits: ["foo", "bar"]`)
10821082
})
10831083

1084+
// https://github.com/vuejs/core/issues/5393
1085+
test('defineEmits w/ type (interface ts type)', () => {
1086+
const { content } = compile(`
1087+
<script setup lang="ts">
1088+
interface Emits { (e: 'foo'): void }
1089+
const emit: Emits = defineEmits(['foo'])
1090+
</script>
1091+
`)
1092+
assertCode(content)
1093+
expect(content).toMatch(`setup(__props, { emit }) {`)
1094+
expect(content).toMatch(`emits: ['foo']`)
1095+
})
1096+
10841097
test('runtime Enum', () => {
10851098
const { content, bindings } = compile(
10861099
`<script setup lang="ts">

0 commit comments

Comments
 (0)