Skip to content

Commit 8264d1c

Browse files
committed
walk script WIP
1 parent 68dad0f commit 8264d1c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tests/utils/target_test_script.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// to be used with typescript_walk_nodes.ts. Write stuff below, and see what nodes there are.
2+
3+
4+
export interface LmaoInterface {
5+
6+
}

tests/utils/typescript_walk_nodes.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// sometimes it's useful to test with a target script and know what nodes there are
2+
3+
4+
import * as fs from 'fs';
5+
import * as path from 'path';
6+
import * as ts from 'typescript';
7+
import {visitNode, VisitResult} from 'typescript';
8+
9+
// visitor class
10+
class MyVisitor {
11+
12+
readonly kinds: string[];
13+
14+
constructor() {
15+
this.kinds = [];
16+
}
17+
18+
visit(node: ts.Node): VisitResult<ts.Node> {
19+
this.kinds.push(ts.SyntaxKind[node.kind]);
20+
return node
21+
22+
}
23+
24+
}
25+
26+
const myVisitor = new MyVisitor()
27+
28+
// load & parse file
29+
const fileName: string = path.resolve(__dirname, 'target_test_script.ts')
30+
const sourceFile = ts.createSourceFile(
31+
fileName,
32+
fs.readFileSync(fileName, 'utf8'),
33+
ts.ScriptTarget.Latest,
34+
true,
35+
);
36+
37+
38+
// fixme: shouldn't pass in sourceFile here. Should convert sourceFile to a Node (maybe convert it to a Bundle first?)
39+
40+
// walk AST
41+
visitNode(sourceFile, n => myVisitor.visit(n))
42+
43+
// show results
44+
console.log(myVisitor.kinds.join('\n'));

0 commit comments

Comments
 (0)