Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path/family sibling traversal methods #5230

Merged
merged 1 commit into from Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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