Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Resolve the name of parameters referring to default exports #2574

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
tristanzander marked this conversation as resolved.
Show resolved Hide resolved
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");
tristanzander marked this conversation as resolved.
Show resolved Hide resolved
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");
tristanzander marked this conversation as resolved.
Show resolved Hide resolved
equal(paramType.name, "DefaultExport", "Incorrect reference name");
tristanzander marked this conversation as resolved.
Show resolved Hide resolved
equal(paramType.qualifiedName, "default", "Incorrect qualified name");
});

it("#671", () => {
const project = convert();
const toNumber = query(project, "toNumber");
Expand Down
Loading