/
family.js
82 lines (71 loc) 路 2.93 KB
/
family.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const traverse = require("../lib").default;
const assert = require("assert");
const parse = require("babylon").parse;
describe("path/family", function () {
describe("getBindingIdentifiers", function () {
const ast = parse("var a = 1, {b} = c, [d] = e; function f() {}");
let nodes = {}, paths = {}, outerNodes = {}, outerPaths = {};
traverse(ast, {
VariableDeclaration(path) {
nodes = path.getBindingIdentifiers();
paths = path.getBindingIdentifierPaths();
},
FunctionDeclaration(path) {
outerNodes = path.getOuterBindingIdentifiers();
outerPaths = path.getOuterBindingIdentifierPaths();
}
});
it("should contain keys of nodes in paths", function () {
Object.keys(nodes).forEach((id) => {
assert.strictEqual(hop(paths, id), true, "Node's keys exists in paths");
});
});
it("should contain outer bindings", function () {
Object.keys(outerNodes).forEach((id) => {
assert.strictEqual(hop(outerPaths, id), true, "Has same outer keys");
});
});
it("should return paths", function () {
Object.keys(paths).forEach((id) => {
assert.strictEqual(!!paths[id].node, true, "Has a property node that's not falsy");
assert.strictEqual(paths[id].type, paths[id].node.type, "type matches");
});
Object.keys(outerPaths).forEach((id) => {
assert.strictEqual(!!outerPaths[id].node, true, "has property node");
assert.strictEqual(outerPaths[id].type, outerPaths[id].node.type, "type matches");
});
});
it("should match paths and nodes returned for the same ast", function () {
Object.keys(nodes).forEach((id) => {
assert.strictEqual(nodes[id], paths[id].node, "Nodes match");
});
});
it("should match paths and nodes returned for outer Bindings", function () {
Object.keys(outerNodes).forEach((id) => {
assert.strictEqual(outerNodes[id], outerPaths[id].node, "nodes match");
});
});
});
describe("getSibling", function () {
const ast = parse("var a = 1, {b} = c, [d] = e; function f() {}");
let sibling = {};
traverse(ast, {
VariableDeclaration(path) {
sibling = path.getSibling(path.key);
}
});
it("should return traverse sibling nodes", function () {
assert.ok(sibling.getNextSibling().node, "has property node");
assert.ok(sibling.getNextSibling().getPrevSibling().node, "has property node");
assert.equal(!!sibling.getPrevSibling().node, false, "out of scope");
assert.equal(!!sibling.getNextSibling().getNextSibling().node, false, "out of scope");
});
it("should return all preceding and succeeding sibling nodes", function () {
assert.ok(sibling.getAllNextSiblings().length, "Has next sibling");
assert.ok(sibling.getNextSibling().getAllPrevSiblings().length, "Has prev sibling");
});
});
});
function hop(o, key) {
return Object.hasOwnProperty.call(o, key);
}