Skip to content

Commit

Permalink
Add path sibling traversal methods (#5230)
Browse files Browse the repository at this point in the history
* getPrevSibling
* getNextSibling
* getAllNextSiblings
* getAllPrevSiblings
  • Loading branch information
chitchu authored and kangax committed Feb 7, 2017
1 parent badce96 commit 1ba4a3f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
32 changes: 31 additions & 1 deletion packages/babel-traverse/src/path/family.js
Expand Up @@ -57,7 +57,7 @@ export function getCompletionRecords(): Array {
return paths;
}

export function getSibling(key) {
export function getSibling(key): NodePath {
return NodePath.get({
parentPath: this.parentPath,
parent: this.parent,
Expand All @@ -67,6 +67,36 @@ export function getSibling(key) {
});
}

export function getPrevSibling(): NodePath {
return this.getSibling(this.key - 1);
}

export function getNextSibling(): NodePath {
return this.getSibling(this.key + 1);
}

export function getAllNextSiblings(): Array<NodePath> {
let _key = this.key;
let sibling:NodePath = this.getSibling(++_key);
const siblings:Array<NodePath> = [];
while (sibling.node) {
siblings.push(sibling);
sibling = this.getSibling(++_key);
}
return siblings;
}

export function getAllPrevSiblings(): Array<NodePath> {
let _key = this.key;
let sibling:NodePath = this.getSibling(--_key);
const siblings:Array<NodePath> = [];
while (sibling.node) {
siblings.push(sibling);
sibling = this.getSibling(--_key);
}
return siblings;
}

export function get(key: string, context?: boolean | TraversalContext): NodePath {
if (context === true) context = this.context;
const parts = key.split(".");
Expand Down
25 changes: 25 additions & 0 deletions packages/babel-traverse/test/family.js
Expand Up @@ -52,6 +52,31 @@ describe("path/family", function () {
assert.strictEqual(outerNodes[id], outerPaths[id].node, "nodes match");
});
});

});
describe("getSibling", function () {
const ast = parse("var a = 1, {b} = c, [d] = e; function f() {} function g() {}");
let sibling = {}, lastSibling = {};
traverse(ast, {
VariableDeclaration(path) {
sibling = path.getSibling(path.key);
lastSibling = sibling.getNextSibling().getNextSibling();
}
});

it("should return traverse sibling nodes", function () {
assert.ok(sibling.getNextSibling().node, "has property node");
assert.ok(lastSibling.getPrevSibling().node, "has property node");
assert.equal(!!sibling.getPrevSibling().node, false, "out of scope");
assert.equal(!!lastSibling.getNextSibling().node, false, "out of scope");
});

it("should return all preceding and succeeding sibling nodes", function () {
assert.ok(sibling.getAllNextSiblings().length, "Has next sibling");
assert.ok(lastSibling.getAllPrevSiblings().length, "Has prev sibling");
assert.equal(sibling.getAllNextSiblings().length, 2, "Has 2 succeeding sibling");
assert.equal(lastSibling.getAllPrevSiblings().length, 2, "Has 2 preceeding sibling");
});
});
});

Expand Down

0 comments on commit 1ba4a3f

Please sign in to comment.