| 1 |
import { parse } from "@babel/parser"; |
| 2 |
import * as t from "@babel/types"; |
| 3 |
|
| 4 |
import _traverse from "../lib/index.js"; |
| 5 |
const traverse = _traverse.default || _traverse; |
| 6 |
|
| 7 |
describe("path/family", function () { |
| 8 |
describe("getBindingIdentifiers", function () { |
| 9 |
const ast = parse("var a = 1, {b} = c, [d] = e; function f() {}"); |
| 10 |
let nodes = {}, |
| 11 |
paths = {}, |
| 12 |
outerNodes = {}, |
| 13 |
outerPaths = {}; |
| 14 |
traverse(ast, { |
| 15 |
VariableDeclaration(path) { |
| 16 |
nodes = path.getBindingIdentifiers(); |
| 17 |
paths = path.getBindingIdentifierPaths(); |
| 18 |
}, |
| 19 |
FunctionDeclaration(path) { |
| 20 |
outerNodes = path.getOuterBindingIdentifiers(); |
| 21 |
outerPaths = path.getOuterBindingIdentifierPaths(); |
| 22 |
}, |
| 23 |
}); |
| 24 |
|
| 25 |
it("should contain keys of nodes in paths", function () { |
| 26 |
Object.keys(nodes).forEach(id => { |
| 27 |
expect(hop(paths, id)).toBe(true); |
| 28 |
}); |
| 29 |
}); |
| 30 |
|
| 31 |
it("should contain outer bindings", function () { |
| 32 |
Object.keys(outerNodes).forEach(id => { |
| 33 |
expect(hop(outerPaths, id)).toBe(true); |
| 34 |
}); |
| 35 |
}); |
| 36 |
|
| 37 |
it("should return paths", function () { |
| 38 |
Object.keys(paths).forEach(id => { |
| 39 |
expect(paths[id].node).toBeTruthy(); |
| 40 |
expect(paths[id].type).toBe(paths[id].node.type); |
| 41 |
}); |
| 42 |
|
| 43 |
Object.keys(outerPaths).forEach(id => { |
| 44 |
expect(outerPaths[id].node).toBeTruthy(); |
| 45 |
expect(outerPaths[id].type).toBe(outerPaths[id].node.type); |
| 46 |
}); |
| 47 |
}); |
| 48 |
|
| 49 |
it("should match paths and nodes returned for the same ast", function () { |
| 50 |
Object.keys(nodes).forEach(id => { |
| 51 |
expect(nodes[id]).toBe(paths[id].node); |
| 52 |
}); |
| 53 |
}); |
| 54 |
|
| 55 |
it("should match paths and nodes returned for outer Bindings", function () { |
| 56 |
Object.keys(outerNodes).forEach(id => { |
| 57 |
expect(outerNodes[id]).toBe(outerPaths[id].node); |
| 58 |
}); |
| 59 |
}); |
| 60 |
}); |
| 61 |
describe("getSibling", function () { |
| 62 |
const ast = parse( |
| 63 |
"var a = 1, {b} = c, [d] = e; function f() {} function g() {}", |
| 64 |
); |
| 65 |
let sibling = {}, |
| 66 |
lastSibling = {}; |
| 67 |
traverse(ast, { |
| 68 |
VariableDeclaration(path) { |
| 69 |
sibling = path.getSibling(path.key); |
| 70 |
lastSibling = sibling.getNextSibling().getNextSibling(); |
| 71 |
}, |
| 72 |
}); |
| 73 |
|
| 74 |
it("should return traverse sibling nodes", function () { |
| 75 |
expect(sibling.getNextSibling().node).toBeTruthy(); |
| 76 |
expect(lastSibling.getPrevSibling().node).toBeTruthy(); |
| 77 |
expect(sibling.getPrevSibling().node).toBeFalsy(); |
| 78 |
expect(lastSibling.getNextSibling().node).toBeFalsy(); |
| 79 |
}); |
| 80 |
|
| 81 |
it("should return all preceding and succeeding sibling nodes", function () { |
| 82 |
expect(sibling.getAllNextSiblings().length).toBeTruthy(); |
| 83 |
expect(lastSibling.getAllPrevSiblings().length).toBeTruthy(); |
| 84 |
expect(sibling.getAllNextSiblings()).toHaveLength(2); |
| 85 |
expect(lastSibling.getAllPrevSiblings()).toHaveLength(2); |
| 86 |
}); |
| 87 |
|
| 88 |
it("should initialize path.scope when needed", function () { |
| 89 |
const ast = parse("if (0) {}"); |
| 90 |
|
| 91 |
let testHasScope = false; |
| 92 |
let consequentHasScope = false; |
| 93 |
|
| 94 |
traverse(ast, { |
| 95 |
IfStatement(path) { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
path.set("test", t.identifier("a")); |
| 101 |
path.set("consequent", t.expressionStatement(t.identifier("b"))); |
| 102 |
|
| 103 |
const testPath = path.get("test"); |
| 104 |
|
| 105 |
testHasScope = !!testPath.scope; |
| 106 |
consequentHasScope = !!testPath.getSibling("consequent").scope; |
| 107 |
}, |
| 108 |
}); |
| 109 |
|
| 110 |
expect(testHasScope).toBe(true); |
| 111 |
expect(consequentHasScope).toBe(true); |
| 112 |
}); |
| 113 |
}); |
| 114 |
describe("getCompletionRecords", function () { |
| 115 |
it("should skip variable declarations", function () { |
| 116 |
const ast = parse("'foo' + 'bar'; var a = 10; let b = 20; const c = 30;"); |
| 117 |
let records = []; |
| 118 |
traverse(ast, { |
| 119 |
Program(path) { |
| 120 |
records = path.getCompletionRecords(); |
| 121 |
}, |
| 122 |
}); |
| 123 |
expect(records).toHaveLength(1); |
| 124 |
expect(records[0].node.type).toBe("ExpressionStatement"); |
| 125 |
expect(records[0].node.expression.type).toBe("BinaryExpression"); |
| 126 |
}); |
| 127 |
|
| 128 |
it("should skip variable declarations in a BlockStatement", function () { |
| 129 |
const ast = parse("'foo' + 'bar'; { var a = 10; }"); |
| 130 |
let records = []; |
| 131 |
traverse(ast, { |
| 132 |
Program(path) { |
| 133 |
records = path.getCompletionRecords(); |
| 134 |
}, |
| 135 |
}); |
| 136 |
expect(records).toHaveLength(1); |
| 137 |
expect(records[0].node.type).toBe("ExpressionStatement"); |
| 138 |
expect(records[0].node.expression.type).toBe("BinaryExpression"); |
| 139 |
}); |
| 140 |
|
| 141 |
it("should be empty if there are only variable declarations", function () { |
| 142 |
const ast = parse("var a = 10; let b = 20; const c = 30;"); |
| 143 |
let records = []; |
| 144 |
traverse(ast, { |
| 145 |
Program(path) { |
| 146 |
records = path.getCompletionRecords(); |
| 147 |
}, |
| 148 |
}); |
| 149 |
expect(records).toHaveLength(0); |
| 150 |
}); |
| 151 |
}); |
| 152 |
}); |
| 153 |
|
| 154 |
function hop(o, key) { |
| 155 |
return Object.hasOwnProperty.call(o, key); |
| 156 |
} |