-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (33 loc) · 994 Bytes
/
index.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
const Parser = require('tree-sitter');
const { typescript: TypeScript } = require('tree-sitter-typescript');
const fs = require('fs')
function formatCaptures(tree, captures) {
return captures.map((c) => {
const node = c.node;
// delete c.node;
c.text = tree.getText(node);
return c;
});
}
function formatMatches(tree, matches) {
return matches.map(({ pattern, captures }) => ({
pattern,
captures: formatCaptures(tree, captures),
}));
}
const source = fs.readFileSync('./test.ts').toString()
const parser = new Parser();
parser.setLanguage(TypeScript);
const tree = parser.parse(source)
const query = new Parser.Query(TypeScript, `
(switch_case
(string
((string_fragment) @case_name)
)
(#eq? @case_name "executeEditorAction")
) @select
`);
const matches = query.matches(tree.rootNode)
const formattedMatches = formatMatches(tree, matches)
.map(match => match.captures)
console.log(matches, formattedMatches);