-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (52 loc) · 1.63 KB
/
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
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
import Parser from "tree-sitter";
import Imp from "tree-sitter-imp";
import { readFileSync } from "fs";
const { Query } = Parser;
const args = process.argv.slice(2);
if (args.length != 1) {
console.error("Usage: npm run lint <file to lint>");
process.exit(1);
}
const sourceCode = readFileSync(args[0], "utf8");
const parser = new Parser();
parser.setLanguage(Imp);
// Load the file passed as an argument
const tree = parser.parse(sourceCode);
// Query for redundant assignments
const redundantQuery = new Query(
Imp,
"((asgn name: (id) @left _ @right) (#eq? @left @right)) @redundantAsgn"
);
// Given a raw list of captures, extract the row, column and text.
function formatCaptures(tree, captures) {
return captures.map((c) => {
const node = c.node;
delete c.node;
c.text = tree.getText(node);
c.row = node.startPosition.row;
c.column = node.startPosition.column;
return c;
});
}
// Get the captures corresponding to a capture name
function capturesByName(tree, query, name) {
return formatCaptures(
tree,
query.captures(tree.rootNode).filter((x) => x.name == name)
).map((x) => {
delete x.name;
return x;
});
}
// Lint the tree with a given message, query and match name
function lint(tree, msg, query, name) {
console.log(msg);
console.log(capturesByName(tree, query, name));
}
lint(tree, "Redundant assignments:", redundantQuery, "redundantAsgn");
// Query for redundant if branches
const redundantIfQuery = new Query(
Imp,
"((if condition: _ @c consequent: _ @l alternative: _ @r) (#eq? @l @r)) @redundantIf"
);
lint(tree, "Redundant if statements:", redundantIfQuery, "redundantIf");