Skip to content

Commit 2a1a9b2

Browse files
authored
fix(component-meta): attach namespace prefix correctly on generated types (#5326)
1 parent 6f2d55c commit 2a1a9b2

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

packages/component-meta/lib/base.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ function createSchemaResolvers(
486486
const visited = new Set<ts.Type>();
487487

488488
function shouldIgnore(subtype: ts.Type) {
489-
const name = typeChecker.typeToString(subtype);
489+
const name = getFullyQualifiedName(subtype);
490490
if (name === 'any') {
491491
return true;
492492
}
@@ -531,7 +531,7 @@ function createSchemaResolvers(
531531
text: tag.text !== undefined ? ts.displayPartsToString(tag.text) : undefined,
532532
})),
533533
required: !(prop.flags & ts.SymbolFlags.Optional),
534-
type: typeChecker.typeToString(subtype),
534+
type: getFullyQualifiedName(subtype),
535535
rawType: rawType ? subtype : undefined,
536536
get declarations() {
537537
return declarations ??= getDeclarations(prop.declarations ?? []);
@@ -551,7 +551,7 @@ function createSchemaResolvers(
551551

552552
return {
553553
name: prop.getName(),
554-
type: typeChecker.typeToString(subtype),
554+
type: getFullyQualifiedName(subtype),
555555
rawType: rawType ? subtype : undefined,
556556
description: ts.displayPartsToString(prop.getDocumentationComment(typeChecker)),
557557
get declarations() {
@@ -569,7 +569,7 @@ function createSchemaResolvers(
569569

570570
return {
571571
name: expose.getName(),
572-
type: typeChecker.typeToString(subtype),
572+
type: getFullyQualifiedName(subtype),
573573
rawType: rawType ? subtype : undefined,
574574
description: ts.displayPartsToString(expose.getDocumentationComment(typeChecker)),
575575
get declarations() {
@@ -590,13 +590,13 @@ function createSchemaResolvers(
590590
if (call.parameters.length >= 2) {
591591
subtype = typeChecker.getTypeOfSymbolAtLocation(call.parameters[1], symbolNode);
592592
if ((call.parameters[1].valueDeclaration as any)?.dotDotDotToken) {
593-
subtypeStr = typeChecker.typeToString(subtype);
593+
subtypeStr = getFullyQualifiedName(subtype);
594594
getSchema = () => typeChecker.getTypeArguments(subtype! as ts.TypeReference).map(resolveSchema);
595595
}
596596
else {
597597
subtypeStr = '[';
598598
for (let i = 1; i < call.parameters.length; i++) {
599-
subtypeStr += typeChecker.typeToString(typeChecker.getTypeOfSymbolAtLocation(call.parameters[i], symbolNode)) + ', ';
599+
subtypeStr += getFullyQualifiedName(typeChecker.getTypeOfSymbolAtLocation(call.parameters[i], symbolNode)) + ', ';
600600
}
601601
subtypeStr = subtypeStr.slice(0, -2) + ']';
602602
getSchema = () => {
@@ -643,7 +643,7 @@ function createSchemaResolvers(
643643
};
644644
}
645645
function resolveSchema(subtype: ts.Type): PropertyMetaSchema {
646-
const type = typeChecker.typeToString(subtype);
646+
const type = getFullyQualifiedName(subtype);
647647

648648
if (shouldIgnore(subtype)) {
649649
return type;
@@ -662,7 +662,6 @@ function createSchemaResolvers(
662662
};
663663
}
664664

665-
// @ts-ignore - typescript internal, isArrayLikeType exists
666665
else if (typeChecker.isArrayLikeType(subtype)) {
667666
let schema: PropertyMetaSchema[];
668667
return {
@@ -694,6 +693,13 @@ function createSchemaResolvers(
694693

695694
return type;
696695
}
696+
function getFullyQualifiedName(type: ts.Type) {
697+
const str = typeChecker.typeToString(type, undefined, ts.TypeFormatFlags.UseFullyQualifiedType | ts.TypeFormatFlags.NoTruncation);
698+
if (str.includes('import(')) {
699+
return str.replace(/import\(.*?\)\./g, '');
700+
}
701+
return str;
702+
}
697703
function getDeclarations(declaration: ts.Declaration[]) {
698704
if (noDeclarations) {
699705
return [];

packages/component-meta/tests/index.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
8888
const array = meta.props.find(prop => prop.name === 'array');
8989
const arrayOptional = meta.props.find(prop => prop.name === 'arrayOptional');
9090
const enumValue = meta.props.find(prop => prop.name === 'enumValue');
91+
const namespaceType = meta.props.find(prop => prop.name === 'namespaceType');
9192
const literalFromContext = meta.props.find(prop => prop.name === 'literalFromContext');
9293
const inlined = meta.props.find(prop => prop.name === 'inlined');
9394
const recursive = meta.props.find(prop => prop.name === 'recursive');
@@ -331,6 +332,17 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
331332
schema: ['MyEnum.Small', 'MyEnum.Medium', 'MyEnum.Large']
332333
});
333334

335+
expect(namespaceType).toBeDefined();
336+
expect(namespaceType?.default).toBeUndefined();
337+
expect(namespaceType?.required).toBeTruthy();
338+
expect(namespaceType?.type).toEqual('MyNamespace.MyType');
339+
expect(namespaceType?.description).toEqual('namespace type');
340+
expect(namespaceType?.schema).toEqual({
341+
kind: 'object',
342+
type: 'MyNamespace.MyType',
343+
schema: {}
344+
});
345+
334346
expect(inlined).toBeDefined();
335347
expect(inlined?.default).toBeUndefined();
336348
expect(inlined?.required).toBeTruthy();

packages/tsc/tests/__snapshots__/dts.spec.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,9 @@ declare enum MyEnum {
620620
Medium = 1,
621621
Large = 2
622622
}
623+
declare namespace MyNamespace {
624+
type MyType = {};
625+
}
623626
declare const categories: readonly ["Uncategorized", "Content", "Interaction", "Display", "Forms", "Addons"];
624627
type MyCategories = typeof categories[number];
625628
export interface MyProps {
@@ -682,6 +685,10 @@ export interface MyProps {
682685
* enum value
683686
*/
684687
enumValue: MyEnum;
688+
/**
689+
* namespace type
690+
*/
691+
namespaceType: MyNamespace.MyType;
685692
/**
686693
* literal type alias that require context
687694
*/

test-workspace/component-meta/reference-type-props/my-props.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ enum MyEnum {
1919
Large,
2020
}
2121

22+
namespace MyNamespace {
23+
export type MyType = {};
24+
}
25+
2226
const categories = [
2327
'Uncategorized',
2428
'Content',
@@ -90,6 +94,10 @@ export interface MyProps {
9094
* enum value
9195
*/
9296
enumValue: MyEnum,
97+
/**
98+
* namespace type
99+
*/
100+
namespaceType: MyNamespace.MyType,
93101
/**
94102
* literal type alias that require context
95103
*/

0 commit comments

Comments
 (0)