utils.js
3.69 KB
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
"use strict";
var path = require("path");
var ts = require("typescript");
function getSourceFile(fileName, source) {
var normalizedName = path.normalize(fileName).replace(/\\/g, "/");
var compilerOptions = createCompilerOptions();
var compilerHost = {
fileExists: function () { return true; },
getCanonicalFileName: function (filename) { return filename; },
getCurrentDirectory: function () { return ""; },
getDefaultLibFileName: function () { return "lib.d.ts"; },
getNewLine: function () { return "\n"; },
getSourceFile: function (filenameToGet) {
if (filenameToGet === normalizedName) {
return ts.createSourceFile(filenameToGet, source, compilerOptions.target, true);
}
},
readFile: function () { return null; },
useCaseSensitiveFileNames: function () { return true; },
writeFile: function () { return null; },
};
var program = ts.createProgram([normalizedName], compilerOptions, compilerHost);
return program.getSourceFile(normalizedName);
}
exports.getSourceFile = getSourceFile;
function createCompilerOptions() {
return {
noResolve: true,
target: ts.ScriptTarget.ES5,
};
}
exports.createCompilerOptions = createCompilerOptions;
function doesIntersect(failure, disabledIntervals) {
return disabledIntervals.some(function (interval) {
var maxStart = Math.max(interval.startPosition, failure.getStartPosition().getPosition());
var minEnd = Math.min(interval.endPosition, failure.getEndPosition().getPosition());
return maxStart <= minEnd;
});
}
exports.doesIntersect = doesIntersect;
function scanAllTokens(scanner, callback) {
var lastStartPos = -1;
while (scanner.scan() !== ts.SyntaxKind.EndOfFileToken) {
var startPos = scanner.getStartPos();
if (startPos === lastStartPos) {
break;
}
lastStartPos = startPos;
callback(scanner);
}
}
exports.scanAllTokens = scanAllTokens;
function hasModifier(modifiers) {
var modifierKinds = [];
for (var _i = 1; _i < arguments.length; _i++) {
modifierKinds[_i - 1] = arguments[_i];
}
if (modifiers == null || modifierKinds == null) {
return false;
}
return modifiers.some(function (m) {
return modifierKinds.some(function (k) { return m.kind === k; });
});
}
exports.hasModifier = hasModifier;
function isBlockScopedVariable(node) {
var parentNode = (node.kind === ts.SyntaxKind.VariableDeclaration)
? node.parent
: node.declarationList;
return isNodeFlagSet(parentNode, ts.NodeFlags.Let)
|| isNodeFlagSet(parentNode, ts.NodeFlags.Const);
}
exports.isBlockScopedVariable = isBlockScopedVariable;
function isBlockScopedBindingElement(node) {
var variableDeclaration = getBindingElementVariableDeclaration(node);
return (variableDeclaration == null) || isBlockScopedVariable(variableDeclaration);
}
exports.isBlockScopedBindingElement = isBlockScopedBindingElement;
function getBindingElementVariableDeclaration(node) {
var currentParent = node.parent;
while (currentParent.kind !== ts.SyntaxKind.VariableDeclaration) {
if (currentParent.parent == null) {
return null;
}
else {
currentParent = currentParent.parent;
}
}
return currentParent;
}
exports.getBindingElementVariableDeclaration = getBindingElementVariableDeclaration;
function isNodeFlagSet(node, flagToCheck) {
return (node.flags & flagToCheck) !== 0;
}
exports.isNodeFlagSet = isNodeFlagSet;
function isNestedModuleDeclaration(decl) {
return decl.name.pos === decl.pos;
}
exports.isNestedModuleDeclaration = isNestedModuleDeclaration;