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

feat: add Source FileName for DefinitionType, used by TopRefNodeParser and ExposeNodeParser. #1056

Draft
wants to merge 2 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ExposeNodeParser.ts
Expand Up @@ -6,6 +6,7 @@ import { DefinitionType } from "./Type/DefinitionType";
import { ReferenceType } from "./Type/ReferenceType";
import { hasJsDocTag } from "./Utils/hasJsDocTag";
import { symbolAtNode } from "./Utils/symbolAtNode";
import { setSourceFileNameIfDefinitionType } from "./Utils/setSourceFileNameIfDefinitionType";

export class ExposeNodeParser implements SubNodeParser {
public constructor(
Expand All @@ -21,16 +22,17 @@ export class ExposeNodeParser implements SubNodeParser {

public createType(node: ts.Node, context: Context, reference?: ReferenceType): BaseType | undefined {
const baseType = this.subNodeParser.createType(node, context, reference);
const sourceFileName = node.getSourceFile().fileName;

if (baseType === undefined) {
return undefined;
}

if (!this.isExportNode(node)) {
return baseType;
return setSourceFileNameIfDefinitionType(baseType, sourceFileName);
}

return new DefinitionType(this.getDefinitionName(node, context), baseType);
return new DefinitionType(this.getDefinitionName(node, context), baseType, sourceFileName);
}

private isExportNode(node: ts.Node): boolean {
Expand Down
8 changes: 5 additions & 3 deletions src/TopRefNodeParser.ts
Expand Up @@ -2,6 +2,7 @@ import ts from "typescript";
import { Context, NodeParser } from "./NodeParser";
import { BaseType } from "./Type/BaseType";
import { DefinitionType } from "./Type/DefinitionType";
import { setSourceFileNameIfDefinitionType } from "./Utils/setSourceFileNameIfDefinitionType";

export class TopRefNodeParser implements NodeParser {
public constructor(
Expand All @@ -12,17 +13,18 @@ export class TopRefNodeParser implements NodeParser {

public createType(node: ts.Node, context: Context): BaseType | undefined {
const baseType = this.childNodeParser.createType(node, context);
const sourceFileName = node.getSourceFile().fileName;

if (baseType === undefined) {
return undefined;
}

if (this.topRef && !(baseType instanceof DefinitionType)) {
return new DefinitionType(this.fullName, baseType);
return new DefinitionType(this.fullName, baseType, sourceFileName);
} else if (!this.topRef && baseType instanceof DefinitionType) {
return baseType.getType();
return setSourceFileNameIfDefinitionType(baseType.getType(), sourceFileName)
} else {
return baseType;
return setSourceFileNameIfDefinitionType(baseType, sourceFileName);
}
}
}
19 changes: 18 additions & 1 deletion src/Type/DefinitionType.ts
@@ -1,7 +1,16 @@
import { BaseType } from "./BaseType";

export class DefinitionType extends BaseType {
public constructor(private name: string | undefined, private type: BaseType) {
public constructor(
private name: string | undefined,
private type: BaseType,
/**
* Source Code fileName for DefinitionType. Used at TopRefNodeParser
* and ExposeNodeParser. So that, You can override DefinitionFormatter
* to custome your self DefinitionType name by fileName.
*/
private sourceFileName?: string
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't feel clean. Can we make sourceFileName an argument to the DefinitionType constructor?

Originally posted by @domoritz in #1019 (comment)

) {
super();
}

Expand All @@ -16,4 +25,12 @@ export class DefinitionType extends BaseType {
public getType(): BaseType {
return this.type;
}

public setSourceFileName(fileName: string) {
this.sourceFileName = fileName;
}

public getSourceFileName() {
return this.sourceFileName;
}
}
9 changes: 9 additions & 0 deletions src/Utils/setSourceFileNameIfDefinitionType.ts
@@ -0,0 +1,9 @@
import { DefinitionType } from "../Type/DefinitionType";
import { BaseType } from "../Type/BaseType";

export const setSourceFileNameIfDefinitionType = (type: BaseType, sourceFileName: string) => {
if (type instanceof DefinitionType) {
type.setSourceFileName(sourceFileName);
}
return type;
};