/
block.ts
210 lines (189 loc) · 7.09 KB
/
block.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import * as ts from "typescript";
import { Reflection, ReflectionKind } from "../../models/index";
import { createDeclaration } from "../factories/index";
import { Context } from "../context";
import { Component, ConverterNodeComponent } from "../components";
import { Converter } from "..";
import { getCommonDirectory } from "../../utils/fs";
import { relative, resolve } from "path";
@Component({ name: "node:block" })
export class BlockConverter extends ConverterNodeComponent<
ts.SourceFile | ts.ModuleBlock
> {
/**
* List of supported TypeScript syntax kinds.
*/
supports: ts.SyntaxKind[] = [
ts.SyntaxKind.ModuleBlock,
ts.SyntaxKind.SourceFile,
];
// Created in initialize
private entryPoints!: string[];
private baseDir!: string;
initialize() {
super.initialize();
this.owner.on(Converter.EVENT_BEGIN, () => {
this.entryPoints = this.application.options
.getValue("entryPoints")
.map((path) => this.normalizeFileName(resolve(path)));
this.baseDir = getCommonDirectory(this.entryPoints);
});
}
/**
* Analyze the given class declaration node and create a suitable reflection.
*
* @param context The context object describing the current state the converter is in.
* @param node The class declaration node that should be analyzed.
* @return The resulting reflection or NULL.
*/
convert(
context: Context,
node: ts.SourceFile | ts.ModuleBlock
): Reflection | undefined {
if (node.kind === ts.SyntaxKind.SourceFile) {
return this.convertSourceFile(context, node);
} else {
for (const exp of this.getExports(context, node)) {
for (const decl of (exp.getDeclarations() ?? []).filter((d) =>
hasParent(d, node)
)) {
this.owner.convertNode(context, decl);
}
}
}
return context.scope;
}
/**
* Analyze the given source file node and create a suitable reflection.
*
* @param context The context object describing the current state the converter is in.
* @param node The source file node that should be analyzed.
* @return The resulting reflection or NULL.
*/
private convertSourceFile(
context: Context,
node: ts.SourceFile
): Reflection | undefined {
let result: Reflection | undefined = context.scope;
context.withSourceFile(node, () => {
if (this.isEntryPoint(node.fileName)) {
const symbol =
context.checker.getSymbolAtLocation(node) ?? node.symbol;
if (context.inFirstPass) {
if (this.entryPoints.length === 1) {
result = context.project;
context.project.registerReflection(result, symbol);
} else {
result = createDeclaration(
context,
node,
ReflectionKind.Module,
this.getModuleName(node.fileName)
);
}
context.withScope(result, () => {
this.convertExports(context, node);
});
} else if (symbol) {
result = context.project.getReflectionFromSymbol(symbol);
context.withScope(result, () => {
this.convertReExports(context, node);
});
}
} else {
result = createDeclaration(
context,
node,
ReflectionKind.Namespace,
this.getModuleName(node.fileName)
);
context.withScope(result, () => {
this.convertExports(context, node);
});
}
});
return result;
}
private convertExports(
context: Context,
node: ts.SourceFile | ts.ModuleBlock
) {
// We really need to rebuild the converters to work on a symbol basis rather than a node
// basis... this relies on us getting declaration merging right, which is dangerous at best
for (const exp of this.getExports(context, node).filter((exp) =>
context
.resolveAliasedSymbol(exp)
.getDeclarations()
?.every((d) => d.getSourceFile() === node.getSourceFile())
)) {
for (const decl of exp.getDeclarations() ?? []) {
this.owner.convertNode(context, decl);
}
}
}
private convertReExports(
context: Context,
node: ts.SourceFile | ts.ModuleBlock
) {
for (const exp of this.getExports(context, node).filter((exp) =>
context
.resolveAliasedSymbol(exp)
.getDeclarations()
?.some((d) => d.getSourceFile() !== node.getSourceFile())
)) {
for (const decl of exp.getDeclarations() ?? []) {
this.owner.convertNode(context, decl);
}
}
}
private getExports(
context: Context,
node: ts.SourceFile | ts.ModuleBlock
): ts.Symbol[] {
let symbol = context.checker.getSymbolAtLocation(node) ?? node.symbol;
if (!symbol && ts.isModuleBlock(node)) {
symbol = context.checker.getSymbolAtLocation(node.parent.name);
}
// The generated docs aren't great, but you really ought not be using
// this in the first place... so it's better than nothing.
const exportEq = symbol?.exports?.get("export=" as ts.__String);
if (exportEq) {
return [exportEq];
}
if (symbol) {
return context.checker.getExportsOfModule(symbol);
}
// This is a global file, get all symbols declared in this file...
// this isn't the best solution, it would be nice to have all globals given to a special
// "globals" file, but this is uncommon enough that I'm skipping it for now.
const sourceFile = node.getSourceFile();
return context.checker
.getSymbolsInScope(node, ts.SymbolFlags.Type | ts.SymbolFlags.Value)
.filter((s) =>
s
.getDeclarations()
?.some((d) => d.getSourceFile() === sourceFile)
);
}
private getModuleName(fileName: string) {
return this.normalizeFileName(relative(this.baseDir, fileName)).replace(
/(\.d)?\.[tj]sx?$/,
""
);
}
private isEntryPoint(fileName: string) {
return this.entryPoints.includes(fileName);
}
private normalizeFileName(fileName: string) {
return fileName.replace(/\\/g, "/");
}
}
function hasParent(node: ts.Node, parent: ts.Node) {
while (node.parent) {
if (node.parent === parent) {
return true;
}
node = node.parent;
}
return false;
}