Skip to content

Commit f597146

Browse files
committed
refactor(compiler-sfc): remove unnecessary emits type codegen
This is no longer necessary as we no longer recommend type checking generated code
1 parent ef73ea5 commit f597146

File tree

3 files changed

+20
-40
lines changed

3 files changed

+20
-40
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+17-17
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ export interface Emits { (e: 'foo' | 'bar'): void }
13881388

13891389
export default /*#__PURE__*/_defineComponent({
13901390
emits: [\\"foo\\", \\"bar\\"],
1391-
setup(__props, { expose: __expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
1391+
setup(__props, { expose: __expose, emit }) {
13921392
__expose();
13931393

13941394

@@ -1405,7 +1405,7 @@ export type Emits = { (e: 'foo' | 'bar'): void }
14051405

14061406
export default /*#__PURE__*/_defineComponent({
14071407
emits: [\\"foo\\", \\"bar\\"],
1408-
setup(__props, { expose: __expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
1408+
setup(__props, { expose: __expose, emit }) {
14091409
__expose();
14101410

14111411

@@ -1439,7 +1439,7 @@ interface Emits { (e: 'foo' | 'bar'): void }
14391439

14401440
export default /*#__PURE__*/_defineComponent({
14411441
emits: [\\"foo\\", \\"bar\\"],
1442-
setup(__props, { expose: __expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
1442+
setup(__props, { expose: __expose, emit }) {
14431443
__expose();
14441444

14451445

@@ -1450,13 +1450,12 @@ return { emit }
14501450
})"
14511451
`;
14521452

1453-
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (referenced exported function type) 1`] = `
1453+
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (property syntax) 1`] = `
14541454
"import { defineComponent as _defineComponent } from 'vue'
1455-
export type Emits = (e: 'foo' | 'bar') => void
1456-
1455+
14571456
export default /*#__PURE__*/_defineComponent({
14581457
emits: [\\"foo\\", \\"bar\\"],
1459-
setup(__props, { expose: __expose, emit }: { emit: ((e: 'foo' | 'bar') => void), expose: any, slots: any, attrs: any }) {
1458+
setup(__props, { expose: __expose, emit }) {
14601459
__expose();
14611460

14621461

@@ -1467,13 +1466,13 @@ return { emit }
14671466
})"
14681467
`;
14691468

1470-
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (referenced function type) 1`] = `
1469+
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (referenced exported function type) 1`] = `
14711470
"import { defineComponent as _defineComponent } from 'vue'
1472-
type Emits = (e: 'foo' | 'bar') => void
1471+
export type Emits = (e: 'foo' | 'bar') => void
14731472

14741473
export default /*#__PURE__*/_defineComponent({
14751474
emits: [\\"foo\\", \\"bar\\"],
1476-
setup(__props, { expose: __expose, emit }: { emit: ((e: 'foo' | 'bar') => void), expose: any, slots: any, attrs: any }) {
1475+
setup(__props, { expose: __expose, emit }) {
14771476
__expose();
14781477

14791478

@@ -1484,12 +1483,13 @@ return { emit }
14841483
})"
14851484
`;
14861485

1487-
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (tuple syntax) 1`] = `
1486+
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (referenced function type) 1`] = `
14881487
"import { defineComponent as _defineComponent } from 'vue'
1489-
1488+
type Emits = (e: 'foo' | 'bar') => void
1489+
14901490
export default /*#__PURE__*/_defineComponent({
14911491
emits: [\\"foo\\", \\"bar\\"],
1492-
setup(__props, { expose: __expose, emit }: { emit: ({ foo: [], bar: [] }), expose: any, slots: any, attrs: any }) {
1492+
setup(__props, { expose: __expose, emit }) {
14931493
__expose();
14941494

14951495

@@ -1506,7 +1506,7 @@ type Emits = { (e: 'foo' | 'bar'): void }
15061506

15071507
export default /*#__PURE__*/_defineComponent({
15081508
emits: [\\"foo\\", \\"bar\\"],
1509-
setup(__props, { expose: __expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
1509+
setup(__props, { expose: __expose, emit }) {
15101510
__expose();
15111511

15121512

@@ -1522,7 +1522,7 @@ exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (typ
15221522

15231523
export default /*#__PURE__*/_defineComponent({
15241524
emits: [\\"foo\\", \\"bar\\", \\"baz\\"],
1525-
setup(__props, { expose: __expose, emit }: { emit: ({(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}), expose: any, slots: any, attrs: any }) {
1525+
setup(__props, { expose: __expose, emit }) {
15261526
__expose();
15271527

15281528

@@ -1538,7 +1538,7 @@ exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type 1`]
15381538

15391539
export default /*#__PURE__*/_defineComponent({
15401540
emits: [\\"foo\\", \\"bar\\"],
1541-
setup(__props, { expose: __expose, emit }: { emit: ((e: 'foo' | 'bar') => void), expose: any, slots: any, attrs: any }) {
1541+
setup(__props, { expose: __expose, emit }) {
15421542
__expose();
15431543

15441544

@@ -1556,7 +1556,7 @@ exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type from
15561556

15571557
export default /*#__PURE__*/_defineComponent({
15581558
emits: [\\"foo\\", \\"bar\\"],
1559-
setup(__props, { expose: __expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
1559+
setup(__props, { expose: __expose, emit }) {
15601560
__expose();
15611561

15621562

packages/compiler-sfc/__tests__/compileScript.spec.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,6 @@ const emit = defineEmits(['a', 'b'])
14141414
</script>
14151415
`)
14161416
assertCode(content)
1417-
expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
14181417
expect(content).toMatch(`emits: ["foo", "bar"]`)
14191418
})
14201419

@@ -1437,7 +1436,6 @@ const emit = defineEmits(['a', 'b'])
14371436
</script>
14381437
`)
14391438
assertCode(content)
1440-
expect(content).toMatch(`emit: (${type}),`)
14411439
expect(content).toMatch(`emits: ["foo", "bar", "baz"]`)
14421440
})
14431441

@@ -1449,7 +1447,6 @@ const emit = defineEmits(['a', 'b'])
14491447
</script>
14501448
`)
14511449
assertCode(content)
1452-
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
14531450
expect(content).toMatch(`emits: ["foo", "bar"]`)
14541451
})
14551452

@@ -1461,7 +1458,6 @@ const emit = defineEmits(['a', 'b'])
14611458
</script>
14621459
`)
14631460
assertCode(content)
1464-
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
14651461
expect(content).toMatch(`emits: ["foo", "bar"]`)
14661462
})
14671463

@@ -1475,7 +1471,6 @@ const emit = defineEmits(['a', 'b'])
14751471
</script>
14761472
`)
14771473
assertCode(content)
1478-
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
14791474
expect(content).toMatch(`emits: ["foo", "bar"]`)
14801475
})
14811476

@@ -1487,7 +1482,6 @@ const emit = defineEmits(['a', 'b'])
14871482
</script>
14881483
`)
14891484
assertCode(content)
1490-
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
14911485
expect(content).toMatch(`emits: ["foo", "bar"]`)
14921486
})
14931487

@@ -1499,7 +1493,6 @@ const emit = defineEmits(['a', 'b'])
14991493
</script>
15001494
`)
15011495
assertCode(content)
1502-
expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
15031496
expect(content).toMatch(`emits: ["foo", "bar"]`)
15041497
})
15051498

@@ -1511,7 +1504,6 @@ const emit = defineEmits(['a', 'b'])
15111504
</script>
15121505
`)
15131506
assertCode(content)
1514-
expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
15151507
expect(content).toMatch(`emits: ["foo", "bar"]`)
15161508
})
15171509

@@ -1523,7 +1515,6 @@ const emit = defineEmits(['a', 'b'])
15231515
</script>
15241516
`)
15251517
assertCode(content)
1526-
expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
15271518
expect(content).toMatch(`emits: ["foo", "bar"]`)
15281519
})
15291520

@@ -1536,11 +1527,10 @@ const emit = defineEmits(['a', 'b'])
15361527
</script>
15371528
`)
15381529
assertCode(content)
1539-
expect(content).toMatch(`setup(__props, { expose: __expose, emit }) {`)
15401530
expect(content).toMatch(`emits: ['foo']`)
15411531
})
15421532

1543-
test('defineEmits w/ type (tuple syntax)', () => {
1533+
test('defineEmits w/ type (property syntax)', () => {
15441534
const { content } = compile(`
15451535
<script setup lang="ts">
15461536
const emit = defineEmits<{ foo: [], bar: [] }>()
@@ -1882,7 +1872,7 @@ const emit = defineEmits(['a', 'b'])
18821872
)
18831873
})
18841874

1885-
test('mixed usage of tuple / call signature in defineEmits', () => {
1875+
test('mixed usage of property / call signature in defineEmits', () => {
18861876
expect(() =>
18871877
compile(`<script setup lang="ts">
18881878
defineEmits<{

packages/compiler-sfc/src/compileScript.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ export function compileScript(
322322
let propsIdentifier: string | undefined
323323
let emitsRuntimeDecl: Node | undefined
324324
let emitsTypeDecl: EmitsDeclType | undefined
325-
let emitsTypeDeclRaw: Node | undefined
326325
let emitIdentifier: string | undefined
327326
let optionsRuntimeDecl: Node | undefined
328327
let hasAwait = false
@@ -582,7 +581,7 @@ export function compileScript(
582581
)
583582
}
584583

585-
emitsTypeDeclRaw = node.typeParameters.params[0]
584+
const emitsTypeDeclRaw = node.typeParameters.params[0]
586585
emitsTypeDecl = resolveQualifiedType(
587586
emitsTypeDeclRaw,
588587
node => node.type === 'TSFunctionType' || node.type === 'TSTypeLiteral'
@@ -1623,15 +1622,6 @@ export function compileScript(
16231622
}
16241623
if (destructureElements.length) {
16251624
args += `, { ${destructureElements.join(', ')} }`
1626-
if (emitsTypeDecl) {
1627-
const content = emitsTypeDecl.__fromNormalScript
1628-
? script!.content
1629-
: scriptSetup.content
1630-
args += `: { emit: (${content.slice(
1631-
emitsTypeDecl.start!,
1632-
emitsTypeDecl.end!
1633-
)}), expose: any, slots: any, attrs: any }`
1634-
}
16351625
}
16361626

16371627
// 10. generate return statement

0 commit comments

Comments
 (0)