Skip to content

Commit 929bc6e

Browse files
committed
fix(complex-types): handle ExpressionWithTypeArguments
1 parent 2a3c1df commit 929bc6e

File tree

3 files changed

+194
-49
lines changed

3 files changed

+194
-49
lines changed

packages/complex-types/src/core/printer.ts

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ export class Printer {
1717
);
1818
}
1919

20+
private typeToString(type: ts.Type): string {
21+
return this.checker.typeToString(
22+
type,
23+
undefined,
24+
ts.TypeFormatFlags.NoTruncation,
25+
);
26+
}
27+
2028
private printIntersectionTypeNode(node: ts.IntersectionTypeNode): string {
2129
return node.types.map((t) => this.print(t)).join(" & ");
2230
}
@@ -31,11 +39,7 @@ export class Printer {
3139
for (const member of node.members) {
3240
if (ts.isPropertySignature(member)) {
3341
const stringBaseType = member.type
34-
? this.checker.typeToString(
35-
this.getBaseType(member.type),
36-
undefined,
37-
ts.TypeFormatFlags.NoTruncation,
38-
)
42+
? this.typeToString(this.getBaseType(member.type))
3943
: "any";
4044

4145
parts.push(
@@ -69,11 +73,7 @@ export class Printer {
6973
: "";
7074

7175
const valueType = this.checker.getTypeOfSymbol(property);
72-
const stringValueType = this.checker.typeToString(
73-
this.getBaseType(valueType),
74-
undefined,
75-
ts.TypeFormatFlags.NoTruncation,
76-
);
76+
const stringValueType = this.typeToString(this.getBaseType(valueType));
7777
parts.push(
7878
`${this.checker.symbolToString(
7979
property,
@@ -119,21 +119,13 @@ export class Printer {
119119
} else if (ts.isMethodSignature(member)) {
120120
const signature = this.checker.getSignatureFromDeclaration(member);
121121
const returnType = signature
122-
? this.checker.typeToString(
123-
signature.getReturnType(),
124-
undefined,
125-
ts.TypeFormatFlags.NoTruncation,
126-
)
122+
? this.typeToString(signature.getReturnType())
127123
: "any";
128124

129125
const parameters = member.parameters
130126
.map((param) => {
131127
const paramType = param.type
132-
? this.checker.typeToString(
133-
this.getBaseType(param.type),
134-
undefined,
135-
ts.TypeFormatFlags.NoTruncation,
136-
)
128+
? this.typeToString(this.getBaseType(param.type))
137129
: "any";
138130

139131
return `${param.name.getText()}: ${paramType}`;
@@ -182,33 +174,21 @@ export class Printer {
182174
if (ts.isPropertyDeclaration(member)) {
183175
const name = member.name?.getText();
184176
const typeAnnotation = member.type
185-
? this.checker.typeToString(
186-
this.getBaseType(member.type),
187-
undefined,
188-
ts.TypeFormatFlags.NoTruncation,
189-
)
177+
? this.typeToString(this.getBaseType(member.type))
190178
: "any";
191179
const optional = member.questionToken ? "?" : "";
192180
parts.push(`${name}${optional}: ${typeAnnotation}`);
193181
} else if (ts.isMethodDeclaration(member)) {
194182
const name = member.name?.getText();
195183
const signature = this.checker.getSignatureFromDeclaration(member);
196184
const returnType = signature
197-
? this.checker.typeToString(
198-
signature.getReturnType(),
199-
undefined,
200-
ts.TypeFormatFlags.NoTruncation,
201-
)
185+
? this.typeToString(signature.getReturnType())
202186
: "any";
203187

204188
const parameters = member.parameters
205189
.map((param) => {
206190
const paramType = param.type
207-
? this.checker.typeToString(
208-
this.getBaseType(param.type),
209-
undefined,
210-
ts.TypeFormatFlags.NoTruncation,
211-
)
191+
? this.typeToString(this.getBaseType(param.type))
212192
: "any";
213193

214194
return `${param.name.getText()}: ${paramType}`;
@@ -220,11 +200,7 @@ export class Printer {
220200
const parameters = member.parameters
221201
.map((param) => {
222202
const paramType = param.type
223-
? this.checker.typeToString(
224-
this.getBaseType(param.type),
225-
undefined,
226-
ts.TypeFormatFlags.NoTruncation,
227-
)
203+
? this.typeToString(this.getBaseType(param.type))
228204
: "any";
229205

230206
return `${param.name.getText()}: ${paramType}`;
@@ -326,7 +302,11 @@ export class Printer {
326302

327303
for (const type of node.types) {
328304
if (ts.isExpressionWithTypeArguments(type)) {
329-
parts.push(this.print(type.expression));
305+
if (type.expression && type.typeArguments) {
306+
parts.push(this.print(type));
307+
} else {
308+
parts.push(this.print(type.expression));
309+
}
330310
}
331311
}
332312

@@ -394,8 +374,6 @@ export class Printer {
394374
return this.printUnionTypeNode(node);
395375
} else if (ts.isTypeLiteralNode(node)) {
396376
return this.printTypeLiteralNode(node);
397-
} else if (ts.isMappedTypeNode(node)) {
398-
return this.printTypeByType(node);
399377
} else if (ts.isTypeReferenceNode(node)) {
400378
return this.print(node.typeName);
401379
} else if (ts.isInterfaceDeclaration(node)) {
@@ -449,6 +427,11 @@ export class Printer {
449427
this.isKeywordTypeNode(node)
450428
) {
451429
return node.getText();
430+
} else if (
431+
ts.isMappedTypeNode(node) ||
432+
ts.isExpressionWithTypeArguments(node)
433+
) {
434+
return this.printTypeByType(node);
452435
} else {
453436
console.error(
454437
`[@vue.ts/complex-types] \`${
@@ -467,11 +450,7 @@ export class Printer {
467450
const parameters = c.getParameters();
468451
const event = parameters[0];
469452

470-
return this.checker.typeToString(
471-
this.checker.getTypeOfSymbol(event),
472-
undefined,
473-
ts.TypeFormatFlags.NoTruncation,
474-
);
453+
return this.typeToString(this.checker.getTypeOfSymbol(event));
475454
});
476455
}
477456

packages/complex-types/test/__snapshots__/fixtures-compiled.test.ts.snap

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,90 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
352352
"aria-valuetext": {
353353
type: String,
354354
required: false
355-
}
355+
},
356+
onCopy: { required: false },
357+
onCut: { required: false },
358+
onPaste: { required: false },
359+
onCompositionend: { required: false },
360+
onCompositionstart: { required: false },
361+
onCompositionupdate: { required: false },
362+
onDrag: { required: false },
363+
onDragend: { required: false },
364+
onDragenter: { required: false },
365+
onDragexit: { required: false },
366+
onDragleave: { required: false },
367+
onDragover: { required: false },
368+
onDragstart: { required: false },
369+
onDrop: { required: false },
370+
onFocus: { required: false },
371+
onFocusin: { required: false },
372+
onFocusout: { required: false },
373+
onBlur: { required: false },
374+
onChange: { required: false },
375+
onBeforeinput: { required: false },
376+
onInput: { required: false },
377+
onReset: { required: false },
378+
onSubmit: { required: false },
379+
onInvalid: { required: false },
380+
onLoad: { required: false },
381+
onError: { required: false },
382+
onKeydown: { required: false },
383+
onKeypress: { required: false },
384+
onKeyup: { required: false },
385+
onAuxclick: { required: false },
386+
onClick: { required: false },
387+
onContextmenu: { required: false },
388+
onDblclick: { required: false },
389+
onMousedown: { required: false },
390+
onMouseenter: { required: false },
391+
onMouseleave: { required: false },
392+
onMousemove: { required: false },
393+
onMouseout: { required: false },
394+
onMouseover: { required: false },
395+
onMouseup: { required: false },
396+
onAbort: { required: false },
397+
onCanplay: { required: false },
398+
onCanplaythrough: { required: false },
399+
onDurationchange: { required: false },
400+
onEmptied: { required: false },
401+
onEncrypted: { required: false },
402+
onEnded: { required: false },
403+
onLoadeddata: { required: false },
404+
onLoadedmetadata: { required: false },
405+
onLoadstart: { required: false },
406+
onPause: { required: false },
407+
onPlay: { required: false },
408+
onPlaying: { required: false },
409+
onProgress: { required: false },
410+
onRatechange: { required: false },
411+
onSeeked: { required: false },
412+
onSeeking: { required: false },
413+
onStalled: { required: false },
414+
onSuspend: { required: false },
415+
onTimeupdate: { required: false },
416+
onVolumechange: { required: false },
417+
onWaiting: { required: false },
418+
onSelect: { required: false },
419+
onScroll: { required: false },
420+
onScrollend: { required: false },
421+
onTouchcancel: { required: false },
422+
onTouchend: { required: false },
423+
onTouchmove: { required: false },
424+
onTouchstart: { required: false },
425+
onPointerdown: { required: false },
426+
onPointermove: { required: false },
427+
onPointerup: { required: false },
428+
onPointercancel: { required: false },
429+
onPointerenter: { required: false },
430+
onPointerleave: { required: false },
431+
onPointerover: { required: false },
432+
onPointerout: { required: false },
433+
onWheel: { required: false },
434+
onAnimationstart: { required: false },
435+
onAnimationend: { required: false },
436+
onAnimationiteration: { required: false },
437+
onTransitionend: { required: false },
438+
onTransitionstart: { required: false }
356439
},
357440
setup(__props, { expose: __expose }) {
358441
__expose();

packages/complex-types/test/__snapshots__/fixtures.test.ts.snap

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,89 @@ defineProps<{
129129
'aria-valuenow'?: number | string
130130
'aria-valuetext'?: string
131131
} & {
132+
onCopy?: any
133+
onCut?: any
134+
onPaste?: any
135+
onCompositionend?: any
136+
onCompositionstart?: any
137+
onCompositionupdate?: any
138+
onDrag?: any
139+
onDragend?: any
140+
onDragenter?: any
141+
onDragexit?: any
142+
onDragleave?: any
143+
onDragover?: any
144+
onDragstart?: any
145+
onDrop?: any
146+
onFocus?: any
147+
onFocusin?: any
148+
onFocusout?: any
149+
onBlur?: any
150+
onChange?: any
151+
onBeforeinput?: any
152+
onInput?: any
153+
onReset?: any
154+
onSubmit?: any
155+
onInvalid?: any
156+
onLoad?: any
157+
onError?: any
158+
onKeydown?: any
159+
onKeypress?: any
160+
onKeyup?: any
161+
onAuxclick?: any
162+
onClick?: any
163+
onContextmenu?: any
164+
onDblclick?: any
165+
onMousedown?: any
166+
onMouseenter?: any
167+
onMouseleave?: any
168+
onMousemove?: any
169+
onMouseout?: any
170+
onMouseover?: any
171+
onMouseup?: any
172+
onAbort?: any
173+
onCanplay?: any
174+
onCanplaythrough?: any
175+
onDurationchange?: any
176+
onEmptied?: any
177+
onEncrypted?: any
178+
onEnded?: any
179+
onLoadeddata?: any
180+
onLoadedmetadata?: any
181+
onLoadstart?: any
182+
onPause?: any
183+
onPlay?: any
184+
onPlaying?: any
185+
onProgress?: any
186+
onRatechange?: any
187+
onSeeked?: any
188+
onSeeking?: any
189+
onStalled?: any
190+
onSuspend?: any
191+
onTimeupdate?: any
192+
onVolumechange?: any
193+
onWaiting?: any
194+
onSelect?: any
195+
onScroll?: any
196+
onScrollend?: any
197+
onTouchcancel?: any
198+
onTouchend?: any
199+
onTouchmove?: any
200+
onTouchstart?: any
201+
onPointerdown?: any
202+
onPointermove?: any
203+
onPointerup?: any
204+
onPointercancel?: any
205+
onPointerenter?: any
206+
onPointerleave?: any
207+
onPointerover?: any
208+
onPointerout?: any
209+
onWheel?: any
210+
onAnimationstart?: any
211+
onAnimationend?: any
212+
onAnimationiteration?: any
213+
onTransitionend?: any
214+
onTransitionstart?: any
132215
}>();
133216
</script>
134217
"

0 commit comments

Comments
 (0)