/
index.js
53 lines (42 loc) · 941 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Test source tree on some condition.
*
* @module ast-test
*/
var types = require('ast-types');
var assert = require('assert');
var visit = types.visit;
var n = types.namedTypes;
/** Test node on condition */
function test(node, rules) {
assert.ok(n.Node.check(node));
assert.ok(rules);
var testResult = true;
visit(node, {
visitNode: function (path) {
var node = path.node, type = node.type;
var nodeTypes = types.getSupertypeNames(type);
//test supertypes first
for (var i = 0, l = nodeTypes.length; i < l; i++) {
type = nodeTypes[i];
if (rules[type]) {
if (!rules[type](node)) {
testResult = false;
this.abort();
return false;
}
}
}
//test specific type
type = node.type;
if (!rules[type] || !rules[type](node)) {
testResult = false;
this.abort();
return false;
}
return false;
}
});
return testResult;
}
module.exports = test;