Skip to content

Commit

Permalink
address comments and add another test case
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanzander committed May 16, 2024
1 parent 3204c93 commit 6a928dc
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 44 deletions.
8 changes: 8 additions & 0 deletions src/lib/converter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,17 @@ const referenceConverter: TypeConverter<
return ref;
}

let name;
if (ts.isIdentifier(node.typeName)) {
name = node.typeName.text;
} else {
name = node.typeName.right.text;
}

const ref = ReferenceType.createSymbolReference(
context.resolveAliasedSymbol(symbol),
context,
name,
);
if (type.flags & ts.TypeFlags.Substitution) {
// NoInfer<T>
Expand Down
28 changes: 0 additions & 28 deletions src/lib/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,39 +907,11 @@ export class ReferenceType extends Type {
return new ReferenceType(name, target, project, name);
}

/**
* In certain cases, TypeScript returns the name `default` for the name of a type that is defined with
* the format `export default class` or `export default function`. This method checks for that case and returns the
* declaration of the export instead of the name `default`.
*/
private static getNameForDefaultExport(
symbol: ts.Symbol,
): string | undefined {
if (
symbol.name !== "default" ||
symbol.valueDeclaration === undefined
) {
return;
}

const name = ts.getNameOfDeclaration(symbol.valueDeclaration);

if (!name) {
return;
}

if (ts.isIdentifier(name)) {
return name.text;
}
}

static createSymbolReference(
symbol: ts.Symbol,
context: Context,
name?: string,
) {
name ??= this.getNameForDefaultExport(symbol);

const ref = new ReferenceType(
name ?? symbol.name,
new ReflectionSymbolId(symbol),
Expand Down
4 changes: 3 additions & 1 deletion src/test/converter2/issues/gh2574/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Default } from "./reexported";
import { Default, NotDefault } from "./reexported";

export function usesDefaultExport(param: Default) {}

export function usesNonDefaultExport(param: NotDefault) {}
3 changes: 3 additions & 0 deletions src/test/converter2/issues/gh2574/notDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class NotDefaultExport {
constructor() {}
}
1 change: 1 addition & 0 deletions src/test/converter2/issues/gh2574/reexported.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as Default } from "./default";
export { NotDefaultExport as NotDefault } from "./notDefault";
51 changes: 36 additions & 15 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
ProjectReflection,
QueryType,
ReferenceReflection,
ReferenceType,
ReflectionKind,
ReflectionType,
SignatureReflection,
Expand Down Expand Up @@ -92,20 +91,6 @@ describe("Issue Tests", () => {
);
});

it("#2574", () => {
const project = convert();
const usesDefaultExport = query(project, "usesDefaultExport");
const sig = usesDefaultExport.signatures?.[0];
ok(sig, "Missing signature for usesDefaultExport");
const param = sig.parameters?.[0];
ok(param, "Missing parameter");
equal(param.name, "param", "Incorrect parameter name");
const paramType = param.type as ReferenceType | undefined;
ok(paramType, "Parameter type is not a reference type or undefined");
equal(paramType.type, "reference", "Parameter is not a reference type");
equal(paramType.name, "DefaultExport", "Incorrect reference name");
equal(paramType.qualifiedName, "default", "Incorrect qualified name");
});

it("#671", () => {
const project = convert();
Expand Down Expand Up @@ -1460,4 +1445,40 @@ describe("Issue Tests", () => {
app.validate(project);
logger.expectNoOtherMessages();
});

it("#2574 default export", () => {
const project = convert();
const sig = querySig(project, "usesDefaultExport");
const param = sig.parameters?.[0];
ok(param, "Missing parameter");
equal(param.name, "param", "Incorrect parameter name");
ok(param.type, "Parameter type is not a reference type or undefined");
equal(
param.type!.type,
"reference",
"Parameter is not a reference type",
);
equal(param.type!.name, "DefaultExport", "Incorrect reference name");
equal(param.type!.qualifiedName, "default", "Incorrect qualified name");
});

it("#2574 not default export", () => {
const project = convert();
const sig = querySig(project, "usesNonDefaultExport");
const param = sig.parameters?.[0];
ok(param, "Missing parameter");
equal(param.name, "param", "Incorrect parameter name");
ok(param.type, "Parameter type is not a reference type or undefined");
equal(
param.type!.type,
"reference",
"Parameter is not a reference type",
);
equal(param.type!.name, "NotDefaultExport", "Incorrect reference name");
equal(
param.type!.qualifiedName,
"NotDefaultExport",
"Incorrect qualified name",
);
});
});

0 comments on commit 6a928dc

Please sign in to comment.