Skip to content

Commit

Permalink
refactor(compiler-sfc): remove unnecessary emits type codegen
Browse files Browse the repository at this point in the history
This is no longer necessary as we no longer recommend type checking generated code
  • Loading branch information
yyx990803 committed Mar 30, 2023
1 parent ef73ea5 commit f597146
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ export interface Emits { (e: 'foo' | 'bar'): void }

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


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

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


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

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


Expand All @@ -1450,13 +1450,12 @@ return { emit }
})"
`;

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


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


Expand All @@ -1467,13 +1466,13 @@ return { emit }
})"
`;

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

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


Expand All @@ -1484,12 +1483,13 @@ return { emit }
})"
`;

exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (tuple syntax) 1`] = `
exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (referenced function type) 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

type Emits = (e: 'foo' | 'bar') => void

export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\"],
setup(__props, { expose: __expose, emit }: { emit: ({ foo: [], bar: [] }), expose: any, slots: any, attrs: any }) {
setup(__props, { expose: __expose, emit }) {
__expose();


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

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


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

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


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

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


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

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


Expand Down
14 changes: 2 additions & 12 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,6 @@ const emit = defineEmits(['a', 'b'])
</script>
`)
assertCode(content)
expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
expect(content).toMatch(`emits: ["foo", "bar"]`)
})

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

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

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

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

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

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

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

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

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

test('defineEmits w/ type (tuple syntax)', () => {
test('defineEmits w/ type (property syntax)', () => {
const { content } = compile(`
<script setup lang="ts">
const emit = defineEmits<{ foo: [], bar: [] }>()
Expand Down Expand Up @@ -1882,7 +1872,7 @@ const emit = defineEmits(['a', 'b'])
)
})

test('mixed usage of tuple / call signature in defineEmits', () => {
test('mixed usage of property / call signature in defineEmits', () => {
expect(() =>
compile(`<script setup lang="ts">
defineEmits<{
Expand Down
12 changes: 1 addition & 11 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ export function compileScript(
let propsIdentifier: string | undefined
let emitsRuntimeDecl: Node | undefined
let emitsTypeDecl: EmitsDeclType | undefined
let emitsTypeDeclRaw: Node | undefined
let emitIdentifier: string | undefined
let optionsRuntimeDecl: Node | undefined
let hasAwait = false
Expand Down Expand Up @@ -582,7 +581,7 @@ export function compileScript(
)
}

emitsTypeDeclRaw = node.typeParameters.params[0]
const emitsTypeDeclRaw = node.typeParameters.params[0]
emitsTypeDecl = resolveQualifiedType(
emitsTypeDeclRaw,
node => node.type === 'TSFunctionType' || node.type === 'TSTypeLiteral'
Expand Down Expand Up @@ -1623,15 +1622,6 @@ export function compileScript(
}
if (destructureElements.length) {
args += `, { ${destructureElements.join(', ')} }`
if (emitsTypeDecl) {
const content = emitsTypeDecl.__fromNormalScript
? script!.content
: scriptSetup.content
args += `: { emit: (${content.slice(
emitsTypeDecl.start!,
emitsTypeDecl.end!
)}), expose: any, slots: any, attrs: any }`
}
}

// 10. generate return statement
Expand Down

0 comments on commit f597146

Please sign in to comment.