Skip to content

Commit

Permalink
Fix Node.closest
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Krispel-Samsel authored and fkling committed Jan 7, 2016
1 parent f70fdaa commit faba497
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/collections/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ var traversalMethods = {
var parent = path.parent;
while (
parent &&
!type.check(parent.value) &&
!(filter && matchNode(parent.value, filter))
!(
type.check(parent.value) &&
(!filter || matchNode(parent.value, filter))
)
) {
parent = parent.parent;
}
Expand Down
52 changes: 39 additions & 13 deletions src/collections/__tests__/Node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,14 @@ describe('Collection API', function() {
});

describe('closest', function() {
it('finds closest node (up the tree) of the given type', function() {
var functionDeclaration = ast.body[1];
var decl = Collection.fromNodes([ast])
.find(types.Identifier)
.closest(types.FunctionDeclaration);

expect(decl.size()).toBe(1);
expect(decl.nodes()[0]).toBe(functionDeclaration);
});

it('allows to filter nodes by pattern', function() {
var decl = b.functionDeclaration(
var decl;
beforeEach(()=> {
decl = b.functionDeclaration(
b.identifier('foo'),
[],
b.blockStatement([
b.functionDeclaration(
b.identifier('foo'),
b.identifier('bar'),
[],
b.blockStatement([
b.returnStatement(
Expand All @@ -156,6 +147,19 @@ describe('Collection API', function() {
),
])
);
});

it('finds closest node (up the tree) of the given type', function() {
var functionDeclaration = ast.body[1];
decl = Collection.fromNodes([ast])
.find(types.Identifier)
.closest(types.FunctionDeclaration);

expect(decl.size()).toBe(1);
expect(decl.nodes()[0]).toBe(functionDeclaration);
});

it('allows to filter nodes by pattern', function() {
var literals = Collection.fromNodes([decl])
.find(types.Literal);
expect(literals.get(0).node.value).toBe(3);
Expand All @@ -165,6 +169,28 @@ describe('Collection API', function() {
);
expect(closest.get(0).node.id.name).toBe('foo');
});

it('allows to filter nodes with a filter function', function() {
var literals = Collection.fromNodes([decl])
.find(types.Literal);
expect(literals.get(0).node.value).toBe(3);
var closest = literals.closest(
types.FunctionDeclaration,
(node) => node.id && node.id.name === 'foo'
);
expect(closest.get(0).node.id.name).toBe('foo');
});

it('fails when filter evaluates as false', function() {
var literals = Collection.fromNodes([decl])
.find(types.Literal);
expect(literals.get(0).node.value).toBe(3);
var closest = literals.closest(
types.FunctionDeclaration,
(node) => node.id && node.id.name === 'blue'
);
expect(closest.nodes().length).toBe(0);
});
});

describe('getVariableDeclarators', function() {
Expand Down

0 comments on commit faba497

Please sign in to comment.