-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.extension.js
48 lines (42 loc) · 1.57 KB
/
type.extension.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
const ts = require('typescript');
const TypeScriptExtensions = require('./typescript.extension.js');
function findNodeWithText(text, nodes, sourceFile) {
var startIndex = text.indexOf('${');
var searchString = text.slice(startIndex + '${'.length);
var openings = 1;
var endIndex = 0;
for (var i = 0; i < searchString.length; i++) {
var char = searchString[i];
if (char == '{') openings++;
else if (char == '}') openings--;
if (openings == 0) {
endIndex = i;
break;
}
}
searchString = searchString.slice(0, endIndex);
return nodes.find((node) => node.getText(sourceFile) == searchString);
}
/**
* @param {string} attributeValue
* @param {ts.Node[]} nodes
* @param {ts.SourceFile} sourceFile
* @param {ts.TypeChecker} checker
*/
function getAttributeType(attributeValue, nodes, sourceFile, checker) {
if (!attributeValue) throw Error();
var associatedNode = findNodeWithText(attributeValue, nodes, sourceFile);
return checker.getTypeAtLocation(associatedNode);
}
/**
* @param {ts.Program} program
* @param {ts.TypeChecker} checker
* @param {ts.Node} node
*/
function getNodeType(program, checker, node) {
var sourceFile = program.getSourceFile(node.getSourceFile().fileName);
var recurseChildren = TypeScriptExtensions.getNodesRecurse(sourceFile);
var typeNode = recurseChildren.find((x) => x.getText() == node.getText()).getLastToken(sourceFile);
return checker.getTypeAtLocation(typeNode).symbol;
}
module.exports = { findNodeWithText, getAttributeType, getNodeType };