This repository has been archived by the owner on Feb 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
compiler.ts
308 lines (242 loc) · 9.5 KB
/
compiler.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* libjass
*
* https:/Arnavion/libjass
*
* Copyright 2013 Arnav Singh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import path = require("path");
import ts = require("typescript");
import { File, FileTransform } from "async-build";
import * as AST from "./ast";
import { walk } from "./walker";
export interface StreamingCompilerHost extends ts.CompilerHost {
setOutputStream(outputStream: FileTransform): void;
}
function createCompilerHost(options: ts.CompilerOptions): StreamingCompilerHost {
const host = ts.createCompilerHost(options) as StreamingCompilerHost;
let _outputStream: FileTransform = null;
host.setOutputStream = outputStream => _outputStream = outputStream;
host.writeFile = (fileName, data, writeByteOrderMark, onError?, sourceFiles?): void => {
_outputStream.push({
path: fileName,
contents: new Buffer(data)
});
};
host.useCaseSensitiveFileNames = () => true;
host.getNewLine = () => "\n";
return host;
}
export class Compiler {
private _projectRoot: string = null;
private _host: StreamingCompilerHost;
private _program: ts.Program = null;
compile(projectConfigFile: File) {
this._projectRoot = path.dirname(projectConfigFile.path);
const projectConfig = ts.parseJsonConfigFileContent(JSON.parse(projectConfigFile.contents.toString()), ts.sys, this._projectRoot);
this._host = createCompilerHost(projectConfig.options);
this._program = ts.createProgram(projectConfig.fileNames, projectConfig.options, this._host);
const syntacticDiagnostics = this._program.getSyntacticDiagnostics();
if (syntacticDiagnostics.length > 0) {
this._reportDiagnostics(syntacticDiagnostics);
throw new Error("There were one or more syntactic diagnostics.");
}
const optionsDiagnostics = this._program.getOptionsDiagnostics();
if (optionsDiagnostics.length > 0) {
this._reportDiagnostics(optionsDiagnostics);
throw new Error("There were one or more options diagnostics.");
}
const globalDiagnostics = this._program.getGlobalDiagnostics();
if (globalDiagnostics.length > 0) {
this._reportDiagnostics(globalDiagnostics);
throw new Error("There were one or more global diagnostics.");
}
const semanticDiagnostics = this._program.getSemanticDiagnostics();
if (semanticDiagnostics.length > 0) {
this._reportDiagnostics(semanticDiagnostics);
throw new Error("There were one or more semantic diagnostics.");
}
};
writeFiles(outputStream: FileTransform) {
this._host.setOutputStream(outputStream);
const emitDiagnostics = this._program.emit().diagnostics;
if (emitDiagnostics.length > 0) {
this._reportDiagnostics(emitDiagnostics);
throw new Error("There were one or more emit diagnostics.");
}
};
get projectRoot(): string {
return this._projectRoot;
}
get typeChecker(): ts.TypeChecker {
return this._program.getTypeChecker();
}
get sourceFiles(): ts.SourceFile[] {
return this._program.getSourceFiles();
}
private _reportDiagnostics(diagnostics: ts.Diagnostic[]) {
for (const diagnostic of diagnostics) {
let message = "";
if (diagnostic.file) {
const location = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
message = `${ diagnostic.file.fileName }(${ location.line + 1 },${ location.character }): `;
}
message +=
ts.DiagnosticCategory[diagnostic.category].toLowerCase() +
` TS${ diagnostic.code }: ` +
ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
console.error(message);
}
};
}
export function build(root: string, rootNamespaceName: string): FileTransform {
const compiler = new Compiler();
return new FileTransform(function (projectConfigFile): void {
console.log("Compiling " + projectConfigFile.path + "...");
compiler.compile(projectConfigFile);
const walkResult = walk(compiler, root, rootNamespaceName);
addJSDocComments(walkResult.modules);
compiler.writeFiles(this);
console.log("Compile succeeded.");
});
}
function addJSDocComments(modules: { [name: string]: AST.Module }): void {
function visitor(current: AST.Module | AST.ModuleMember | AST.InterfaceMember) {
if (current instanceof AST.Module) {
for (const memberName of Object.keys(current.members)) {
visitor(current.members[memberName]);
}
return;
}
const newComments: string[] = [];
if (current instanceof AST.Class) {
newComments.push("@constructor");
if (current.baseType !== null) {
const baseType = current.baseType;
newComments.push(
"@extends {" +
baseType.fullName + (
(baseType instanceof AST.TypeReference && baseType.generics.length) > 0 ?
(".<" + (baseType as AST.TypeReference).generics.map(generic => generic.fullName).join(", ") + ">") :
""
) +
"}"
);
}
if (current.interfaces.length > 0) {
current.interfaces.forEach(interfase => {
newComments.push(
"@implements {" +
interfase.fullName + (
(interfase instanceof AST.TypeReference && interfase.generics.length > 0) ?
(".<" + interfase.generics.map(generic => generic.fullName).join(", ") + ">") :
""
) +
"}"
);
});
}
Object.keys(current.members).forEach(memberName => visitor(current.members[memberName]));
}
else if (current instanceof AST.Enum) {
newComments.push("@enum");
}
else if (current instanceof AST.Reference) {
return;
}
if (current.parent instanceof AST.Namespace) {
newComments.push("@memberOf " + current.parent.fullName);
}
if (AST.hasStringGenerics(current) && current.generics.length > 0) {
newComments.push("@template " + current.generics.join(", "));
}
if ((current as AST.CanBePrivate).isPrivate) {
newComments.push("@private");
}
if ((current as AST.CanBeProtected).isProtected) {
newComments.push("@protected");
}
if ((current as AST.CanBeStatic).isStatic) {
newComments.push("@static");
}
if (newComments.length > 0) {
if (current instanceof AST.Property) {
const nodes: ts.Node[] = [];
if (current.getter !== null) { nodes.push(current.getter.astNode); }
if (current.setter !== null && nodes[0] !== current.setter.astNode) { nodes.push(current.setter.astNode); }
for (const node of nodes) {
(node as any)["typescript-new-comment"] = newComments;
}
}
else {
(current.astNode as any)["typescript-new-comment"] = newComments;
}
}
}
for (const moduleName of Object.keys(modules)) {
visitor(modules[moduleName]);
}
}
class FakeSourceFile {
public text: string;
public lineMap: number[];
constructor(originalSourceFile: ts.SourceFile) {
this.text = originalSourceFile.text;
this.lineMap = ts.getLineStarts(originalSourceFile).slice();
}
addComment(originalComment: ts.CommentRange, newComments: string[]): ts.CommentRange & { sourceFile: FakeSourceFile } {
var pos = this.text.length;
this.text += "/**\n";
this.lineMap.push(this.text.length);
var originalCommentLines = this.text.substring(originalComment.pos, originalComment.end).split("\n");
originalCommentLines.shift();
originalCommentLines = originalCommentLines.map(line => line.replace(/^\s+/, " "));
if (originalCommentLines.length > 1) {
originalCommentLines.splice(originalCommentLines.length - 1, 0, " *");
}
for (const newComment of newComments) {
originalCommentLines.splice(originalCommentLines.length - 1, 0, " * " + newComment);
}
for (const newCommentLine of originalCommentLines) {
this.text += newCommentLine + "\n";
this.lineMap.push(this.text.length);
}
var end = this.text.length;
return { pos, end, hasTrailingNewLine: originalComment.hasTrailingNewLine, kind: ts.SyntaxKind.MultiLineCommentTrivia, sourceFile: this };
}
}
var fakeSourceFiles: { [name: string]: FakeSourceFile } = Object.create(null);
export const oldGetLeadingCommentRangesOfNodeFromText: typeof ts.getLeadingCommentRangesOfNodeFromText = ts.getLeadingCommentRangesOfNodeFromText.bind(ts);
ts.getLeadingCommentRangesOfNodeFromText = (node: ts.Node, text: string) => {
const originalComments = oldGetLeadingCommentRangesOfNodeFromText(node, text);
if (originalComments !== undefined && (<any>node)["typescript-new-comment"] !== undefined) {
const sourceFileOfNode = ts.getSourceFileOfNode(node);
let fakeSourceFile = fakeSourceFiles[sourceFileOfNode.fileName];
if (fakeSourceFile === undefined) {
fakeSourceFile = fakeSourceFiles[sourceFileOfNode.fileName] = new FakeSourceFile(sourceFileOfNode);
}
originalComments[originalComments.length - 1] = fakeSourceFile.addComment(originalComments[originalComments.length - 1], (<any>node)["typescript-new-comment"]);
}
return originalComments;
};
var oldWriteCommentRange: typeof ts.writeCommentRange = ts.writeCommentRange.bind(ts);
ts.writeCommentRange = (text: string, lineMap: number[], writer: ts.EmitTextWriter, comment: ts.CommentRange, newLine: string) => {
if ((<{ sourceFile: ts.SourceFile }><any>comment).sourceFile) {
const currentSourceFile = (<{ sourceFile: ts.SourceFile }><any>comment).sourceFile;
text = currentSourceFile.text;
lineMap = currentSourceFile.lineMap;
}
return oldWriteCommentRange(text, lineMap, writer, comment, newLine);
};