Bogdan Savluk 4108524856
Update prettier to v2 (#11579)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
2020-06-07 22:21:33 +02:00

78 lines
1.8 KiB
JavaScript

import traverse from "../lib";
import { parse } from "@babel/parser";
describe("path/ancestry", function () {
describe("isAncestor", function () {
const ast = parse("var a = 1; 'a';");
it("returns true if ancestor", function () {
const paths = [];
traverse(ast, {
"Program|NumericLiteral"(path) {
paths.push(path);
},
});
const [programPath, numberPath] = paths;
expect(programPath.isAncestor(numberPath)).toBeTruthy();
});
it("returns false if not ancestor", function () {
const paths = [];
traverse(ast, {
"Program|NumericLiteral|StringLiteral"(path) {
paths.push(path);
},
});
const [, numberPath, stringPath] = paths;
expect(stringPath.isAncestor(numberPath)).toBeFalsy();
});
});
describe("isDescendant", function () {
const ast = parse("var a = 1; 'a';");
it("returns true if descendant", function () {
const paths = [];
traverse(ast, {
"Program|NumericLiteral"(path) {
paths.push(path);
},
});
const [programPath, numberPath] = paths;
expect(numberPath.isDescendant(programPath)).toBeTruthy();
});
it("returns false if not descendant", function () {
const paths = [];
traverse(ast, {
"Program|NumericLiteral|StringLiteral"(path) {
paths.push(path);
},
});
const [, numberPath, stringPath] = paths;
expect(numberPath.isDescendant(stringPath)).toBeFalsy();
});
});
describe("getStatementParent", function () {
const ast = parse("var a = 1;");
it("should throw", function () {
expect(function () {
traverse(ast, {
Program(path) {
path.getStatementParent();
},
});
}).toThrow(/File\/Program node/);
});
});
});