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

Fix Collection.closest #83

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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