1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | "use strict";
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | var path = require("path");
|
20 | var ts = require("typescript");
|
21 | function getSourceFile(fileName, source) {
|
22 | var normalizedName = path.normalize(fileName).replace(/\\/g, "/");
|
23 | var compilerOptions = createCompilerOptions();
|
24 | var compilerHost = {
|
25 | fileExists: function () { return true; },
|
26 | getCanonicalFileName: function (filename) { return filename; },
|
27 | getCurrentDirectory: function () { return ""; },
|
28 | getDefaultLibFileName: function () { return "lib.d.ts"; },
|
29 | getDirectories: function (_path) { return []; },
|
30 | getNewLine: function () { return "\n"; },
|
31 | getSourceFile: function (filenameToGet) {
|
32 | var target = compilerOptions.target == null ? ts.ScriptTarget.ES5 : compilerOptions.target;
|
33 | return ts.createSourceFile(filenameToGet, source, target, true);
|
34 | },
|
35 | readFile: function (x) { return x; },
|
36 | useCaseSensitiveFileNames: function () { return true; },
|
37 | writeFile: function (x) { return x; },
|
38 | };
|
39 | var program = ts.createProgram([normalizedName], compilerOptions, compilerHost);
|
40 | return program.getSourceFile(normalizedName);
|
41 | }
|
42 | exports.getSourceFile = getSourceFile;
|
43 | function createCompilerOptions() {
|
44 | return {
|
45 | allowJs: true,
|
46 | noResolve: true,
|
47 | target: ts.ScriptTarget.ES5,
|
48 | };
|
49 | }
|
50 | exports.createCompilerOptions = createCompilerOptions;
|
51 | function doesIntersect(failure, disabledIntervals) {
|
52 | return disabledIntervals.some(function (interval) {
|
53 | var maxStart = Math.max(interval.startPosition, failure.getStartPosition().getPosition());
|
54 | var minEnd = Math.min(interval.endPosition, failure.getEndPosition().getPosition());
|
55 | return maxStart <= minEnd;
|
56 | });
|
57 | }
|
58 | exports.doesIntersect = doesIntersect;
|
59 |
|
60 | function scanAllTokens(scanner, callback) {
|
61 | var lastStartPos = -1;
|
62 | while (scanner.scan() !== ts.SyntaxKind.EndOfFileToken) {
|
63 | var startPos = scanner.getStartPos();
|
64 | if (startPos === lastStartPos) {
|
65 | break;
|
66 | }
|
67 | lastStartPos = startPos;
|
68 | callback(scanner);
|
69 | }
|
70 | }
|
71 | exports.scanAllTokens = scanAllTokens;
|
72 |
|
73 |
|
74 |
|
75 | function hasModifier(modifiers) {
|
76 | var modifierKinds = [];
|
77 | for (var _i = 1; _i < arguments.length; _i++) {
|
78 | modifierKinds[_i - 1] = arguments[_i];
|
79 | }
|
80 | if (modifiers === undefined || modifierKinds.length === 0) {
|
81 | return false;
|
82 | }
|
83 | return modifiers.some(function (m) {
|
84 | return modifierKinds.some(function (k) { return m.kind === k; });
|
85 | });
|
86 | }
|
87 | exports.hasModifier = hasModifier;
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | function isBlockScopedVariable(node) {
|
93 | var parentNode = (node.kind === ts.SyntaxKind.VariableDeclaration)
|
94 | ? node.parent
|
95 | : node.declarationList;
|
96 | return isNodeFlagSet(parentNode, ts.NodeFlags.Let)
|
97 | || isNodeFlagSet(parentNode, ts.NodeFlags.Const);
|
98 | }
|
99 | exports.isBlockScopedVariable = isBlockScopedVariable;
|
100 | function isBlockScopedBindingElement(node) {
|
101 | var variableDeclaration = getBindingElementVariableDeclaration(node);
|
102 |
|
103 | return (variableDeclaration == null) || isBlockScopedVariable(variableDeclaration);
|
104 | }
|
105 | exports.isBlockScopedBindingElement = isBlockScopedBindingElement;
|
106 | function getBindingElementVariableDeclaration(node) {
|
107 | var currentParent = node.parent;
|
108 | while (currentParent.kind !== ts.SyntaxKind.VariableDeclaration) {
|
109 | if (currentParent.parent == null) {
|
110 | return null;
|
111 | }
|
112 | else {
|
113 | currentParent = currentParent.parent;
|
114 | }
|
115 | }
|
116 | return currentParent;
|
117 | }
|
118 | exports.getBindingElementVariableDeclaration = getBindingElementVariableDeclaration;
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | function childOfKind(node, kind) {
|
124 | return node.getChildren().find(function (child) { return child.kind === kind; });
|
125 | }
|
126 | exports.childOfKind = childOfKind;
|
127 |
|
128 |
|
129 |
|
130 | function someAncestor(node, predicate) {
|
131 | return predicate(node) || (node.parent != null && someAncestor(node.parent, predicate));
|
132 | }
|
133 | exports.someAncestor = someAncestor;
|
134 | function isAssignment(node) {
|
135 | if (node.kind === ts.SyntaxKind.BinaryExpression) {
|
136 | var binaryExpression = node;
|
137 | return binaryExpression.operatorToken.kind >= ts.SyntaxKind.FirstAssignment
|
138 | && binaryExpression.operatorToken.kind <= ts.SyntaxKind.LastAssignment;
|
139 | }
|
140 | else {
|
141 | return false;
|
142 | }
|
143 | }
|
144 | exports.isAssignment = isAssignment;
|
145 |
|
146 |
|
147 |
|
148 | function isNodeFlagSet(node, flagToCheck) {
|
149 |
|
150 | return (node.flags & flagToCheck) !== 0;
|
151 | }
|
152 | exports.isNodeFlagSet = isNodeFlagSet;
|
153 |
|
154 |
|
155 |
|
156 | function isCombinedNodeFlagSet(node, flagToCheck) {
|
157 |
|
158 | return (ts.getCombinedNodeFlags(node) & flagToCheck) !== 0;
|
159 | }
|
160 | exports.isCombinedNodeFlagSet = isCombinedNodeFlagSet;
|
161 |
|
162 |
|
163 |
|
164 | function isCombinedModifierFlagSet(node, flagToCheck) {
|
165 |
|
166 | return (ts.getCombinedModifierFlags(node) & flagToCheck) !== 0;
|
167 | }
|
168 | exports.isCombinedModifierFlagSet = isCombinedModifierFlagSet;
|
169 |
|
170 |
|
171 |
|
172 | function isTypeFlagSet(type, flagToCheck) {
|
173 |
|
174 | return (type.flags & flagToCheck) !== 0;
|
175 | }
|
176 | exports.isTypeFlagSet = isTypeFlagSet;
|
177 |
|
178 |
|
179 |
|
180 | function isSymbolFlagSet(symbol, flagToCheck) {
|
181 |
|
182 | return (symbol.flags & flagToCheck) !== 0;
|
183 | }
|
184 | exports.isSymbolFlagSet = isSymbolFlagSet;
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | function isObjectFlagSet(objectType, flagToCheck) {
|
190 |
|
191 | return (objectType.objectFlags & flagToCheck) !== 0;
|
192 | }
|
193 | exports.isObjectFlagSet = isObjectFlagSet;
|
194 |
|
195 |
|
196 |
|
197 | function isNestedModuleDeclaration(decl) {
|
198 |
|
199 |
|
200 |
|
201 | return decl.name.pos === decl.pos;
|
202 | }
|
203 | exports.isNestedModuleDeclaration = isNestedModuleDeclaration;
|
204 | function unwrapParentheses(node) {
|
205 | while (node.kind === ts.SyntaxKind.ParenthesizedExpression) {
|
206 | node = node.expression;
|
207 | }
|
208 | return node;
|
209 | }
|
210 | exports.unwrapParentheses = unwrapParentheses;
|
211 | function isScopeBoundary(node) {
|
212 | return node.kind === ts.SyntaxKind.FunctionDeclaration
|
213 | || node.kind === ts.SyntaxKind.FunctionExpression
|
214 | || node.kind === ts.SyntaxKind.PropertyAssignment
|
215 | || node.kind === ts.SyntaxKind.ShorthandPropertyAssignment
|
216 | || node.kind === ts.SyntaxKind.MethodDeclaration
|
217 | || node.kind === ts.SyntaxKind.Constructor
|
218 | || node.kind === ts.SyntaxKind.ModuleDeclaration
|
219 | || node.kind === ts.SyntaxKind.ArrowFunction
|
220 | || node.kind === ts.SyntaxKind.ParenthesizedExpression
|
221 | || node.kind === ts.SyntaxKind.ClassDeclaration
|
222 | || node.kind === ts.SyntaxKind.ClassExpression
|
223 | || node.kind === ts.SyntaxKind.InterfaceDeclaration
|
224 | || node.kind === ts.SyntaxKind.GetAccessor
|
225 | || node.kind === ts.SyntaxKind.SetAccessor
|
226 | || node.kind === ts.SyntaxKind.SourceFile && ts.isExternalModule(node);
|
227 | }
|
228 | exports.isScopeBoundary = isScopeBoundary;
|
229 | function isBlockScopeBoundary(node) {
|
230 | return isScopeBoundary(node)
|
231 | || node.kind === ts.SyntaxKind.Block
|
232 | || isLoop(node)
|
233 | || node.kind === ts.SyntaxKind.WithStatement
|
234 | || node.kind === ts.SyntaxKind.SwitchStatement
|
235 | || node.parent !== undefined
|
236 | && (node.parent.kind === ts.SyntaxKind.TryStatement
|
237 | || node.parent.kind === ts.SyntaxKind.IfStatement);
|
238 | }
|
239 | exports.isBlockScopeBoundary = isBlockScopeBoundary;
|
240 | function isLoop(node) {
|
241 | return node.kind === ts.SyntaxKind.DoStatement
|
242 | || node.kind === ts.SyntaxKind.WhileStatement
|
243 | || node.kind === ts.SyntaxKind.ForStatement
|
244 | || node.kind === ts.SyntaxKind.ForInStatement
|
245 | || node.kind === ts.SyntaxKind.ForOfStatement;
|
246 | }
|
247 | exports.isLoop = isLoop;
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | function forEachToken(node, skipTrivia, cb, filter) {
|
259 |
|
260 | var sourceFile = node.getSourceFile();
|
261 | var fullText = sourceFile.text;
|
262 | var iterateFn = filter === undefined ? iterateChildren : iterateWithFilter;
|
263 | var handleTrivia = skipTrivia ? undefined : createTriviaHandler(sourceFile, cb);
|
264 | iterateFn(node);
|
265 |
|
266 | function iterateWithFilter(child) {
|
267 | if (filter(child)) {
|
268 | return iterateChildren(child);
|
269 | }
|
270 | }
|
271 | function iterateChildren(child) {
|
272 | if (child.kind < ts.SyntaxKind.FirstNode ||
|
273 |
|
274 |
|
275 | child.kind === ts.SyntaxKind.JsxText) {
|
276 |
|
277 | return callback(child);
|
278 | }
|
279 | |
280 |
|
281 |
|
282 | if (child.kind !== ts.SyntaxKind.JSDocComment) {
|
283 |
|
284 | return child.getChildren(sourceFile).forEach(iterateFn);
|
285 | }
|
286 | }
|
287 | function callback(token) {
|
288 | var tokenStart = token.getStart(sourceFile);
|
289 | if (!skipTrivia && tokenStart !== token.pos) {
|
290 |
|
291 | handleTrivia(token.pos, tokenStart, token);
|
292 | }
|
293 | return cb(fullText, token.kind, { tokenStart: tokenStart, fullStart: token.pos, end: token.end }, token.parent);
|
294 | }
|
295 | }
|
296 | exports.forEachToken = forEachToken;
|
297 | function createTriviaHandler(sourceFile, cb) {
|
298 | var fullText = sourceFile.text;
|
299 | var scanner = ts.createScanner(sourceFile.languageVersion, false, sourceFile.languageVariant, fullText);
|
300 | |
301 |
|
302 |
|
303 |
|
304 | function handleTrivia(start, end, token) {
|
305 | var parent = token.parent;
|
306 |
|
307 | if (!canHaveLeadingTrivia(token.kind, parent)) {
|
308 | return;
|
309 | }
|
310 | scanner.setTextPos(start);
|
311 | var position;
|
312 |
|
313 | do {
|
314 | var kind = scanner.scan();
|
315 | position = scanner.getTextPos();
|
316 | cb(fullText, kind, { tokenStart: scanner.getTokenPos(), end: position, fullStart: start }, parent);
|
317 | } while (position < end);
|
318 | }
|
319 | return handleTrivia;
|
320 | }
|
321 |
|
322 | function forEachComment(node, cb) {
|
323 | |
324 |
|
325 |
|
326 |
|
327 |
|
328 | return forEachToken(node, true, function (fullText, tokenKind, pos, parent) {
|
329 |
|
330 | if (canHaveLeadingTrivia(tokenKind, parent)) {
|
331 |
|
332 | var comments = ts.getLeadingCommentRanges(fullText, pos.fullStart);
|
333 | if (comments !== undefined) {
|
334 | for (var _i = 0, comments_1 = comments; _i < comments_1.length; _i++) {
|
335 | var comment = comments_1[_i];
|
336 | cb(fullText, comment.kind, { fullStart: pos.fullStart, tokenStart: comment.pos, end: comment.end });
|
337 | }
|
338 | }
|
339 | }
|
340 | if (canHaveTrailingTrivia(tokenKind, parent)) {
|
341 | var comments = ts.getTrailingCommentRanges(fullText, pos.end);
|
342 | if (comments !== undefined) {
|
343 | for (var _a = 0, comments_2 = comments; _a < comments_2.length; _a++) {
|
344 | var comment = comments_2[_a];
|
345 | cb(fullText, comment.kind, { fullStart: pos.fullStart, tokenStart: comment.pos, end: comment.end });
|
346 | }
|
347 | }
|
348 | }
|
349 | });
|
350 | }
|
351 | exports.forEachComment = forEachComment;
|
352 |
|
353 | function canHaveLeadingTrivia(tokenKind, parent) {
|
354 | if (tokenKind === ts.SyntaxKind.JsxText) {
|
355 | return false;
|
356 | }
|
357 | if (tokenKind === ts.SyntaxKind.OpenBraceToken) {
|
358 |
|
359 | return parent.kind !== ts.SyntaxKind.JsxExpression || parent.parent.kind !== ts.SyntaxKind.JsxElement;
|
360 | }
|
361 | if (tokenKind === ts.SyntaxKind.LessThanToken) {
|
362 | if (parent.kind === ts.SyntaxKind.JsxClosingElement) {
|
363 | return false;
|
364 | }
|
365 | if (parent.kind === ts.SyntaxKind.JsxOpeningElement || parent.kind === ts.SyntaxKind.JsxSelfClosingElement) {
|
366 |
|
367 | return parent.parent.parent.kind !== ts.SyntaxKind.JsxElement;
|
368 | }
|
369 | }
|
370 | return true;
|
371 | }
|
372 |
|
373 | function canHaveTrailingTrivia(tokenKind, parent) {
|
374 | if (tokenKind === ts.SyntaxKind.JsxText) {
|
375 | return false;
|
376 | }
|
377 | if (tokenKind === ts.SyntaxKind.CloseBraceToken) {
|
378 |
|
379 | return parent.kind !== ts.SyntaxKind.JsxExpression || parent.parent.kind !== ts.SyntaxKind.JsxElement;
|
380 | }
|
381 | if (tokenKind === ts.SyntaxKind.GreaterThanToken) {
|
382 | if (parent.kind === ts.SyntaxKind.JsxOpeningElement) {
|
383 | return false;
|
384 | }
|
385 | if (parent.kind === ts.SyntaxKind.JsxClosingElement || parent.kind === ts.SyntaxKind.JsxSelfClosingElement) {
|
386 |
|
387 | return parent.parent.parent.kind !== ts.SyntaxKind.JsxElement;
|
388 | }
|
389 | }
|
390 | return true;
|
391 | }
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 | function hasCommentAfterPosition(text, position) {
|
400 | return ts.getTrailingCommentRanges(text, position) !== undefined ||
|
401 | ts.getLeadingCommentRanges(text, position) !== undefined;
|
402 | }
|
403 | exports.hasCommentAfterPosition = hasCommentAfterPosition;
|
404 | function getEqualsKind(node) {
|
405 | switch (node.kind) {
|
406 | case ts.SyntaxKind.EqualsEqualsToken:
|
407 | return { isPositive: true, isStrict: false };
|
408 | case ts.SyntaxKind.EqualsEqualsEqualsToken:
|
409 | return { isPositive: true, isStrict: true };
|
410 | case ts.SyntaxKind.ExclamationEqualsToken:
|
411 | return { isPositive: false, isStrict: false };
|
412 | case ts.SyntaxKind.ExclamationEqualsEqualsToken:
|
413 | return { isPositive: false, isStrict: true };
|
414 | default:
|
415 | return undefined;
|
416 | }
|
417 | }
|
418 | exports.getEqualsKind = getEqualsKind;
|