Debian Codegraph @

packages/babel-traverse/test/ancestry.js

raw symbols SCIP
1 import { parse } from "@babel/parser";
2
3 import _traverse from "../lib/index.js";
4 const traverse = _traverse.default || _traverse;
5
6 describe("path/ancestry", function () {
7 describe("isAncestor", function () {
8 const ast = parse("var a = 1; 'a';");
9
10 it("returns true if ancestor", function () {
11 const paths = [];
12 traverse(ast, {
13 "Program|NumericLiteral"(path) {
14 paths.push(path);
15 },
16 });
17
18 const [programPath, numberPath] = paths;
19
20 expect(programPath.isAncestor(numberPath)).toBeTruthy();
21 });
22
23 it("returns false if not ancestor", function () {
24 const paths = [];
25 traverse(ast, {
26 "Program|NumericLiteral|StringLiteral"(path) {
27 paths.push(path);
28 },
29 });
30
31 const [, numberPath, stringPath] = paths;
32
33 expect(stringPath.isAncestor(numberPath)).toBeFalsy();
34 });
35 });
36
37 describe("isDescendant", function () {
38 const ast = parse("var a = 1; 'a';");
39
40 it("returns true if descendant", function () {
41 const paths = [];
42 traverse(ast, {
43 "Program|NumericLiteral"(path) {
44 paths.push(path);
45 },
46 });
47
48 const [programPath, numberPath] = paths;
49
50 expect(numberPath.isDescendant(programPath)).toBeTruthy();
51 });
52
53 it("returns false if not descendant", function () {
54 const paths = [];
55 traverse(ast, {
56 "Program|NumericLiteral|StringLiteral"(path) {
57 paths.push(path);
58 },
59 });
60
61 const [, numberPath, stringPath] = paths;
62
63 expect(numberPath.isDescendant(stringPath)).toBeFalsy();
64 });
65 });
66
67 describe("getStatementParent", function () {
68 const ast = parse("var a = 1;");
69 it("should throw", function () {
70 expect(function () {
71 traverse(ast, {
72 Program(path) {
73 path.getStatementParent();
74 },
75 });
76 }).toThrow(/File\/Program node/);
77 });
78 });
79 });