-
Notifications
You must be signed in to change notification settings - Fork 188
/
TopRefNodeParser.ts
30 lines (26 loc) · 1.18 KB
/
TopRefNodeParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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(
private childNodeParser: NodeParser,
private fullName: string | undefined,
private topRef: boolean
) {}
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, sourceFileName);
} else if (!this.topRef && baseType instanceof DefinitionType) {
return setSourceFileNameIfDefinitionType(baseType.getType(), sourceFileName)
} else {
return setSourceFileNameIfDefinitionType(baseType, sourceFileName);
}
}
}