Skip to content

Commit

Permalink
fix: Properly resolve the name of a default export using a layer of i…
Browse files Browse the repository at this point in the history
…ndirection
  • Loading branch information
tristanzander committed May 15, 2024
1 parent cf3dcd1 commit 3204c93
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,11 +907,39 @@ 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
6 changes: 6 additions & 0 deletions src/test/converter2/issues/gh2574/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Default export and class implementation
*/
export default class DefaultExport {
constructor() {}
}
3 changes: 3 additions & 0 deletions src/test/converter2/issues/gh2574/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Default } from "./reexported";

export function usesDefaultExport(param: Default) {}
1 change: 1 addition & 0 deletions src/test/converter2/issues/gh2574/reexported.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Default } from "./default";
16 changes: 16 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ProjectReflection,
QueryType,
ReferenceReflection,
ReferenceType,
ReflectionKind,
ReflectionType,
SignatureReflection,
Expand Down Expand Up @@ -91,6 +92,21 @@ 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();
const toNumber = query(project, "toNumber");
Expand Down

0 comments on commit 3204c93

Please sign in to comment.