Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
78 lines
1.9 KiB
JavaScript
78 lines
1.9 KiB
JavaScript
/* eslint-disable prettier-internal-rules/no-useless-ast-path-callback-parameter */
|
|
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);
|
|
}
|
|
});
|
|
});
|