forked from barbatus/meteor-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts-utils.js
235 lines (201 loc) · 6.12 KB
/
ts-utils.js
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
import assert from "assert";
import ts from "typescript";
import _ from "underscore";
import { assertProps } from "./utils";
// 1) Normalizes slashes in the file path
// 2) Removes file extension
export function normalizePath(filePath) {
let resultName = filePath;
if (ts.fileExtensionIs(filePath, ".map")) {
resultName = filePath.replace(/\.map$/, "");
}
return ts.removeFileExtension(
ts.normalizeSlashes(resultName));
}
export function getRootedPath(filePath) {
if (ts.getRootLength(filePath) === 0) {
return "/" + filePath;
}
return filePath;
}
export function prepareSourceMap(sourceMapContent, fileContent, sourceMapPath) {
const sourceMapJson = JSON.parse(sourceMapContent);
sourceMapJson.sourcesContent = [fileContent];
sourceMapJson.sources = [sourceMapPath];
return sourceMapJson;
}
/**
* Gets all local modules given sourceFile imports types from.
* Supports transitivity, i.e., if some module (directly imported)
* re-exports types from another module, this another module
* will be in the output too.
*/
function getDeps(sourceFile, checker) {
const modules = [];
function getModulePath(module) {
if (!module) return null;
const decl = module.declarations[0];
const sf = decl.getSourceFile();
if (sf && !sf.isDeclarationFile) {
return sf.path;
}
return null;
}
function isExternal(module) {
const decl = module.declarations[0];
const sf = decl.getSourceFile();
return sf.isDeclarationFile;
}
if (sourceFile.imports) {
const paths = new Set();
_.each(sourceFile.imports, function(importName) {
const module = checker.getSymbolAtLocation(importName);
if (module && !isExternal(module)) {
const path = getModulePath(module);
if (path) {
paths.add(path);
}
const nodes = checker.getExportsOfModule(module);
_.each(nodes, function(node) {
if (node.parent && node.parent !== module) {
const path = getModulePath(node.parent);
if (path) {
paths.add(path);
}
return;
}
// If directly imported module re-uses and exports of a type
// from another module, add this module to the dependency as well.
const type = checker.getTypeAtLocation(node.declarations[0]);
if (type && type.symbol) {
const typeModule = type.symbol.parent;
if (typeModule !== module) {
const path = getModulePath(typeModule);
if (path) {
paths.add(path);
}
}
}
});
}
});
paths.forEach(function(path) {
modules.push(path)
});
}
return modules;
}
export function getDepsAndRefs(sourceFile, typeChecker) {
assert.ok(typeChecker);
const modules = getDeps(sourceFile, typeChecker);
const refs = getRefs(sourceFile);
const mappings = getMappings(sourceFile);
return {
modules,
mappings,
refFiles: refs.refFiles,
refTypings: refs.refTypings,
};
}
function getMappings(sourceFile) {
const mappings = [];
if (sourceFile.resolvedModules) {
const modules = sourceFile.resolvedModules;
modules.forEach((module, modulePath) => {
mappings.push({
modulePath,
resolvedPath: module ? ts.removeFileExtension(module.resolvedFileName) : null,
external: module ? module.isExternalLibraryImport : false,
resolved: !!module,
});
});
}
return mappings;
}
function getRefs(sourceFile) {
// Collect referenced file paths, e.g.:
// /// <reference path=".." />
let refTypings = [], refFiles = [];
if (sourceFile.referencedFiles) {
const refPaths = sourceFile.referencedFiles.map((ref) => ref.fileName);
refTypings = _.filter(refPaths, (ref) => isTypings(ref));
refFiles = _.filter(refPaths, (ref) => !isTypings(ref));
}
// Collect resolved paths to referenced declaration types, e.g.:
// /// <reference types=".." />
if (sourceFile.resolvedTypeReferenceDirectiveNames) {
const modules = sourceFile.resolvedTypeReferenceDirectiveNames;
modules.forEach((ref, lib) => {
if (!ref) return;
refTypings.push(ref.resolvedFileName);
});
}
return {
refFiles,
refTypings,
};
}
export function createDiagnostics(tsSyntactic, tsSemantic) {
// Parse diagnostics to leave only info we need.
var syntactic = flattenDiagnostics(tsSyntactic);
var semantic = flattenDiagnostics(tsSemantic);
return {
syntacticErrors: syntactic,
semanticErrors: semantic,
};
}
export class TsDiagnostics {
constructor(diagnostics) {
assert.ok(this instanceof TsDiagnostics);
assert.ok(diagnostics);
assertProps(diagnostics, [
"syntacticErrors", "semanticErrors",
]);
_.extend(this, diagnostics);
}
hasErrors() {
return !!this.semanticErrors.length ||
!!this.syntacticErrors.length;
}
hasUnresolvedModules() {
const index = _.findIndex(this.semanticErrors, (msg) =>
msg.code === ts.Diagnostics.Cannot_find_module_0.code
);
return index !== -1;
}
}
function flattenDiagnostics(tsDiagnostics) {
const diagnostics = [];
const dLen = tsDiagnostics.length;
for (let i = 0; i < dLen; i++) {
const diagnostic = tsDiagnostics[i];
if (!diagnostic.file) continue;
const pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
const line = pos.line + 1;
const column = pos.character + 1;
diagnostics.push({
code: diagnostic.code,
fileName: diagnostic.file.fileName,
message,
line,
column,
});
}
return diagnostics;
}
export function hasErrors(diagnostics) {
if (!diagnostics) return true;
return diagnostics.semanticErrors.length ||
diagnostics.syntacticErrors.length;
}
export function isSourceMap(fileName) {
return ts.fileExtensionIs(fileName, ".map");
}
export function isTypings(fileName) {
return ts.fileExtensionIs(fileName, ".d.ts");
}
export function getExcludeRegExp(exclude) {
if (!exclude) return exclude;
return ts.getRegularExpressionForWildcard(exclude, "", "exclude");
}