1
|
"use strict";
|
2
|
/**
|
3
|
* @license
|
4
|
* Copyright Google Inc. All Rights Reserved.
|
5
|
*
|
6
|
* Use of this source code is governed by an MIT-style license that can be
|
7
|
* found in the LICENSE file at https://angular.io/license
|
8
|
*/
|
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
10
|
var compiler_1 = require("@angular/compiler");
|
11
|
var ts = require("typescript");
|
12
|
var stubCancellationToken = {
|
13
|
isCancellationRequested: function () { return false; },
|
14
|
throwIfCancellationRequested: function () { }
|
15
|
};
|
16
|
var TypeChecker = (function () {
|
17
|
function TypeChecker(program, tsOptions, compilerHost, aotCompilerHost, aotOptions, _analyzedModules, _generatedFiles) {
|
18
|
this.program = program;
|
19
|
this.tsOptions = tsOptions;
|
20
|
this.compilerHost = compilerHost;
|
21
|
this.aotCompilerHost = aotCompilerHost;
|
22
|
this.aotOptions = aotOptions;
|
23
|
this._analyzedModules = _analyzedModules;
|
24
|
this._generatedFiles = _generatedFiles;
|
25
|
this._currentCancellationToken = stubCancellationToken;
|
26
|
this._partial = false;
|
27
|
}
|
28
|
TypeChecker.prototype.getDiagnostics = function (fileName, cancellationToken) {
|
29
|
this._currentCancellationToken = cancellationToken || stubCancellationToken;
|
30
|
try {
|
31
|
return fileName ?
|
32
|
this.diagnosticsByFileName.get(fileName) || [] : (_a = []).concat.apply(_a, Array.from(this.diagnosticsByFileName.values()));
|
33
|
}
|
34
|
finally {
|
35
|
this._currentCancellationToken = stubCancellationToken;
|
36
|
}
|
37
|
var _a;
|
38
|
};
|
39
|
Object.defineProperty(TypeChecker.prototype, "partialResults", {
|
40
|
get: function () { return this._partial; },
|
41
|
enumerable: true,
|
42
|
configurable: true
|
43
|
});
|
44
|
Object.defineProperty(TypeChecker.prototype, "analyzedModules", {
|
45
|
get: function () {
|
46
|
return this._analyzedModules || (this._analyzedModules = this.aotCompiler.analyzeModulesSync(this.program.getSourceFiles().map(function (sf) { return sf.fileName; })));
|
47
|
},
|
48
|
enumerable: true,
|
49
|
configurable: true
|
50
|
});
|
51
|
Object.defineProperty(TypeChecker.prototype, "diagnosticsByFileName", {
|
52
|
get: function () {
|
53
|
return this._diagnosticsByFile || this.createDiagnosticsByFile();
|
54
|
},
|
55
|
enumerable: true,
|
56
|
configurable: true
|
57
|
});
|
58
|
Object.defineProperty(TypeChecker.prototype, "diagnosticProgram", {
|
59
|
get: function () {
|
60
|
return this._diagnosticProgram || this.createDiagnosticProgram();
|
61
|
},
|
62
|
enumerable: true,
|
63
|
configurable: true
|
64
|
});
|
65
|
Object.defineProperty(TypeChecker.prototype, "generatedFiles", {
|
66
|
get: function () {
|
67
|
var result = this._generatedFiles;
|
68
|
if (!result) {
|
69
|
this._generatedFiles = result = this.aotCompiler.emitAllImpls(this.analyzedModules);
|
70
|
}
|
71
|
return result;
|
72
|
},
|
73
|
enumerable: true,
|
74
|
configurable: true
|
75
|
});
|
76
|
Object.defineProperty(TypeChecker.prototype, "aotCompiler", {
|
77
|
get: function () {
|
78
|
return this._aotCompiler || this.createCompilerAndReflector();
|
79
|
},
|
80
|
enumerable: true,
|
81
|
configurable: true
|
82
|
});
|
83
|
Object.defineProperty(TypeChecker.prototype, "reflector", {
|
84
|
get: function () {
|
85
|
var result = this._reflector;
|
86
|
if (!result) {
|
87
|
this.createCompilerAndReflector();
|
88
|
result = this._reflector;
|
89
|
}
|
90
|
return result;
|
91
|
},
|
92
|
enumerable: true,
|
93
|
configurable: true
|
94
|
});
|
95
|
Object.defineProperty(TypeChecker.prototype, "factories", {
|
96
|
get: function () {
|
97
|
return this._factories || this.createFactories();
|
98
|
},
|
99
|
enumerable: true,
|
100
|
configurable: true
|
101
|
});
|
102
|
Object.defineProperty(TypeChecker.prototype, "factoryNames", {
|
103
|
get: function () {
|
104
|
return this._factoryNames || (this.createFactories() && this._factoryNames);
|
105
|
},
|
106
|
enumerable: true,
|
107
|
configurable: true
|
108
|
});
|
109
|
TypeChecker.prototype.createCompilerAndReflector = function () {
|
110
|
var _a = compiler_1.createAotCompiler(this.aotCompilerHost, this.aotOptions), compiler = _a.compiler, reflector = _a.reflector;
|
111
|
this._reflector = reflector;
|
112
|
return this._aotCompiler = compiler;
|
113
|
};
|
114
|
TypeChecker.prototype.createDiagnosticProgram = function () {
|
115
|
// Create a program that is all the files from the original program plus the factories.
|
116
|
var existingFiles = this.program.getSourceFiles().map(function (source) { return source.fileName; });
|
117
|
var host = new TypeCheckingHost(this.compilerHost, this.program, this.factories);
|
118
|
return this._diagnosticProgram =
|
119
|
ts.createProgram(existingFiles.concat(this.factoryNames), this.tsOptions, host);
|
120
|
};
|
121
|
TypeChecker.prototype.createFactories = function () {
|
122
|
// Create all the factory files with enough information to map the diagnostics reported for the
|
123
|
// created file back to the original source.
|
124
|
var emitter = new compiler_1.TypeScriptEmitter();
|
125
|
var factorySources = this.generatedFiles.filter(function (file) { return file.stmts != null && file.stmts.length; })
|
126
|
.map(function (file) { return [file.genFileUrl, createFactoryInfo(emitter, file)]; });
|
127
|
this._factories = new Map(factorySources);
|
128
|
this._factoryNames = Array.from(this._factories.keys());
|
129
|
return this._factories;
|
130
|
};
|
131
|
TypeChecker.prototype.createDiagnosticsByFile = function () {
|
132
|
// Collect all the diagnostics binned by original source file name.
|
133
|
var result = new Map();
|
134
|
var diagnosticsFor = function (fileName) {
|
135
|
var r = result.get(fileName);
|
136
|
if (!r) {
|
137
|
r = [];
|
138
|
result.set(fileName, r);
|
139
|
}
|
140
|
return r;
|
141
|
};
|
142
|
var program = this.diagnosticProgram;
|
143
|
for (var _i = 0, _a = this.factoryNames; _i < _a.length; _i++) {
|
144
|
var factoryName = _a[_i];
|
145
|
if (this._currentCancellationToken.isCancellationRequested())
|
146
|
return result;
|
147
|
var sourceFile = program.getSourceFile(factoryName);
|
148
|
for (var _b = 0, _c = this.diagnosticProgram.getSemanticDiagnostics(sourceFile); _b < _c.length; _b++) {
|
149
|
var diagnostic = _c[_b];
|
150
|
var span = this.sourceSpanOf(diagnostic.file, diagnostic.start, diagnostic.length);
|
151
|
if (span) {
|
152
|
var fileName = span.start.file.url;
|
153
|
var diagnosticsList = diagnosticsFor(fileName);
|
154
|
diagnosticsList.push({
|
155
|
message: diagnosticMessageToString(diagnostic.messageText),
|
156
|
category: diagnosticCategoryConverter(diagnostic.category), span: span
|
157
|
});
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
return result;
|
162
|
};
|
163
|
TypeChecker.prototype.sourceSpanOf = function (source, start, length) {
|
164
|
// Find the corresponding TypeScript node
|
165
|
var info = this.factories.get(source.fileName);
|
166
|
if (info) {
|
167
|
var _a = ts.getLineAndCharacterOfPosition(source, start), line = _a.line, character = _a.character;
|
168
|
return info.context.spanOf(line, character);
|
169
|
}
|
170
|
return null;
|
171
|
};
|
172
|
return TypeChecker;
|
173
|
}());
|
174
|
exports.TypeChecker = TypeChecker;
|
175
|
function diagnosticMessageToString(message) {
|
176
|
return ts.flattenDiagnosticMessageText(message, '\n');
|
177
|
}
|
178
|
function diagnosticCategoryConverter(kind) {
|
179
|
// The diagnostics kind matches ts.DiagnosticCategory. Review this code if this changes.
|
180
|
return kind;
|
181
|
}
|
182
|
function createFactoryInfo(emitter, file) {
|
183
|
var _a = emitter.emitStatementsAndContext(file.srcFileUrl, file.genFileUrl, file.stmts), sourceText = _a.sourceText, context = _a.context;
|
184
|
var source = ts.createSourceFile(file.genFileUrl, sourceText, ts.ScriptTarget.Latest, /* setParentNodes */ true);
|
185
|
return { source: source, context: context };
|
186
|
}
|
187
|
var TypeCheckingHost = (function () {
|
188
|
function TypeCheckingHost(host, originalProgram, factories) {
|
189
|
this.host = host;
|
190
|
this.originalProgram = originalProgram;
|
191
|
this.factories = factories;
|
192
|
this.writeFile = function () { throw new Error('Unexpected write in diagnostic program'); };
|
193
|
}
|
194
|
TypeCheckingHost.prototype.getSourceFile = function (fileName, languageVersion, onError) {
|
195
|
var originalSource = this.originalProgram.getSourceFile(fileName);
|
196
|
if (originalSource) {
|
197
|
return originalSource;
|
198
|
}
|
199
|
var factoryInfo = this.factories.get(fileName);
|
200
|
if (factoryInfo) {
|
201
|
return factoryInfo.source;
|
202
|
}
|
203
|
return this.host.getSourceFile(fileName, languageVersion, onError);
|
204
|
};
|
205
|
TypeCheckingHost.prototype.getDefaultLibFileName = function (options) {
|
206
|
return this.host.getDefaultLibFileName(options);
|
207
|
};
|
208
|
TypeCheckingHost.prototype.getCurrentDirectory = function () {
|
209
|
return this.host.getCurrentDirectory();
|
210
|
};
|
211
|
TypeCheckingHost.prototype.getDirectories = function (path) { return this.host.getDirectories(path); };
|
212
|
TypeCheckingHost.prototype.getCanonicalFileName = function (fileName) {
|
213
|
return this.host.getCanonicalFileName(fileName);
|
214
|
};
|
215
|
TypeCheckingHost.prototype.useCaseSensitiveFileNames = function () { return this.host.useCaseSensitiveFileNames(); };
|
216
|
TypeCheckingHost.prototype.getNewLine = function () { return this.host.getNewLine(); };
|
217
|
TypeCheckingHost.prototype.fileExists = function (fileName) {
|
218
|
return this.factories.has(fileName) || this.host.fileExists(fileName);
|
219
|
};
|
220
|
TypeCheckingHost.prototype.readFile = function (fileName) {
|
221
|
var factoryInfo = this.factories.get(fileName);
|
222
|
return (factoryInfo && factoryInfo.source.text) || this.host.readFile(fileName);
|
223
|
};
|
224
|
return TypeCheckingHost;
|
225
|
}());
|
226
|
//# sourceMappingURL=check_types.js.map
|