Skip to content

Commit 988b7d5

Browse files
Lookup following object (in tree order)
1 parent 6d03962 commit 988b7d5

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

lib/SymbolTree.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,40 @@ module.exports = class SymbolTree {
141141
return this._node(object).parent;
142142
}
143143

144+
/**
145+
* Find the following object (A) of the given object (B).
146+
* An object A is following an object B if A and B are in the same tree
147+
* and A comes after B in tree order.
148+
* @method following
149+
* @param {!Object} object
150+
* @param {Object} [treeRoot] If set, `treeRoot` must be an inclusive ancestor
151+
* of the return value (or else null is returned). This check _assumes_
152+
* that `root` is also an inclusive ancestor of the given `node`
153+
* @returns {?Object}
154+
*/
155+
following(object, treeRoot) {
156+
if (this._node(object).first) {
157+
return this._node(object).first;
158+
}
159+
160+
do {
161+
if (object === treeRoot) {
162+
return null;
163+
}
164+
165+
if (this._node(object).next) {
166+
return this._node(object).next;
167+
}
168+
169+
object = this._node(object).parent;
170+
// https://github.com/jscs-dev/node-jscs/commit/07d30b77388a2e182a40b00891c7ac1837281016
171+
// jscs:disable requirePaddingNewlinesBeforeKeywords
172+
} while (object);
173+
// jscs:enable requirePaddingNewlinesBeforeKeywords
174+
175+
return null;
176+
}
177+
144178
/**
145179
* Remove the object from this tree.
146180
* `O(1)`

test/SymbolTree.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,79 @@ test('look up preceding using a specified root', function(t) {
503503

504504
t.end();
505505
});
506+
507+
test('following with a child', function(t) {
508+
const tree = new SymbolTree();
509+
const a = {};
510+
const aa = {};
511+
512+
tree.insertLast(aa, a);
513+
514+
t.equal(aa , tree.following(a));
515+
t.equal(null, tree.following(aa));
516+
517+
t.end();
518+
});
519+
520+
test('following with a next sibling', function(t) {
521+
const tree = new SymbolTree();
522+
const a = {};
523+
const b = {};
524+
525+
tree.insertAfter(b, a);
526+
527+
t.equal(b , tree.following(a));
528+
t.equal(null, tree.following(b));
529+
530+
t.end();
531+
});
532+
533+
test('following with sibling of parent', function(t) {
534+
const tree = new SymbolTree();
535+
const a = {};
536+
const aa = {};
537+
const b = {};
538+
539+
tree.insertLast(aa, a);
540+
tree.insertAfter(b, a);
541+
542+
t.equal(b, tree.following(aa));
543+
544+
t.end();
545+
});
546+
547+
test('following with sibling of grandparent', function(t) {
548+
const tree = new SymbolTree();
549+
const a = {};
550+
const aa = {};
551+
const aaa = {};
552+
const b = {};
553+
554+
tree.insertLast(aa, a);
555+
tree.insertLast(aaa, aa);
556+
tree.insertAfter(b, a);
557+
558+
t.equal(b, tree.following(aaa));
559+
560+
t.end();
561+
});
562+
563+
test('following using a specified root', function(t) {
564+
const tree = new SymbolTree();
565+
const a = {};
566+
const aa = {};
567+
const aaa = {};
568+
const b = {};
569+
570+
tree.insertLast(aa, a);
571+
tree.insertLast(aaa, aa);
572+
tree.insertAfter(b, a);
573+
574+
t.equal(null, tree.following(aaa, aaa));
575+
t.equal(null, tree.following(aaa, aa));
576+
t.equal(null, tree.following(aaa, a));
577+
t.equal(aa , tree.following(a, a));
578+
t.equal(aaa , tree.following(aa, a));
579+
580+
t.end();
581+
});

0 commit comments

Comments
 (0)