-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
23 lines (20 loc) · 920 Bytes
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const ts = require('typescript');
const sourceFile = ts.createSourceFile('', `const foo = <number>(3 + 5);`, ts.ScriptTarget.ESNext, undefined, ts.ScriptKind.TS);
sourceFile.forEachChild(function cb(node) {
if (ts.isTypeAssertionExpression(node)) {
console.log(node.getFirstToken(sourceFile));
console.log(node.getLastToken(sourceFile));
console.log(node.type);
console.log(getTokenBefore(node.type, node, sourceFile).getText(sourceFile));
console.log(getTokenAfter(node.type, node, sourceFile).getText(sourceFile));
}
node.forEachChild(cb);
function getTokenBefore(node, parent) {
const children = parent.getChildren(sourceFile);
return children[children.indexOf(node) - 1];
}
function getTokenAfter(node, parent) {
const children = parent.getChildren(sourceFile);
return children[children.indexOf(node) + 1];
}
});