Repository navigation
Expand file tree
/
Copy pathast-path.js
More file actions
76 lines (64 loc) · 1.86 KB
/
Copy pathast-path.js
File metadata and controls
76 lines (64 loc) · 1.86 KB
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
import AstPath from "../../src/common/ast-path.js";
const error = new Error("A dummy error");
const throwError = () => {
throw error;
};
describe("AstPath", () => {
const ast = {
property: {
deep: { name: "deep" },
},
children: [{ index: 0 }, { index: 1 }],
};
test("AstPath#call()", () => {
const path = new AstPath(ast);
expect(path.call(() => path.getValue(), "property")).toBe(ast.property);
expect(() => path.call(throwError, "property")).toThrow(error);
expect(path.stack.length).toBe(1);
});
test("AstPath#callParent()", () => {
const path = new AstPath(ast);
path.stack.push("property", ast.property, "deep", ast.property.deep);
expect(path.callParent(() => path.getValue())).toBe(ast.property);
expect(() => path.callParent(throwError)).toThrow(error);
expect(path.stack.length).toBe(5);
});
test("AstPath#each()", () => {
const path = new AstPath(ast);
{
const called = [];
path.each(() => called.push(path.getValue()), "children");
expect(called).toEqual(ast.children);
}
{
const called = [];
expect(() => {
path.each((_, index) => {
if (index === 1) {
throwError();
}
called.push(path.getValue());
}, "children");
}).toThrow(error);
expect(called.length).toBe(1);
expect(path.stack.length).toBe(1);
}
});
test("AstPath#map()", () => {
const path = new AstPath(ast);
expect(path.map(() => path.getValue(), "children")).toEqual(ast.children);
{
let result;
expect(() => {
result = path.map((_, index) => {
if (index === 1) {
throwError();
}
return path.getValue();
}, "children");
}).toThrow(error);
expect(result).toBeUndefined();
expect(path.stack.length).toBe(1);
}
});
});