Skip to content

Commit

Permalink
Fix filter application in .closest (fixes #41)
Browse files Browse the repository at this point in the history
  • Loading branch information
fkling committed Aug 20, 2015
1 parent 2a0f8a7 commit a1be267
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/collections/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var traversalMethods = {
while (
parent &&
!type.check(parent.value) &&
(!filter || matchNode(parent.value, filter))
!(filter && matchNode(parent.value, filter))
) {
parent = parent.parent;
}
Expand Down
8 changes: 8 additions & 0 deletions src/collections/__tests__/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"globals": {
"jest": true
},
"env": {
"jasmine": true
}
}
96 changes: 51 additions & 45 deletions src/collections/__tests__/Node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('Collection API', function() {
var Collection;
var NodeCollection;
var recast;
var NodePath;
var types;
var b;

Expand All @@ -26,7 +25,6 @@ describe('Collection API', function() {
NodeCollection = require('../Node');
recast = require('recast');

NodePath = recast.types.NodePath;
types = recast.types.namedTypes;
b = recast.types.builders;

Expand Down Expand Up @@ -57,9 +55,9 @@ describe('Collection API', function() {
describe('Traversal', function() {
describe('find', function() {
it('finds nodes by type', function() {
var ast = b.sequenceExpression([
var ast = b.sequenceExpression([ // eslint-disable-line no-shadow
b.identifier('foo'),
b.literal("asd"),
b.literal('asd'),
b.identifier('bar'),
]);
var vars = Collection.fromNodes([ast]).find(types.Identifier);
Expand All @@ -70,7 +68,7 @@ describe('Collection API', function() {
it('doesn\'t find the nodes in the collection itself', function() {
var nodes = [
b.identifier('foo'),
b.literal("asd"),
b.literal('asd'),
b.identifier('bar'),
];
var vars = Collection.fromNodes(nodes).find(types.Identifier);
Expand All @@ -79,9 +77,9 @@ describe('Collection API', function() {
});

it('finds nodes by type and properties', function() {
var ast = b.sequenceExpression([
var ast = b.sequenceExpression([ // eslint-disable-line no-shadow
b.identifier('foo'),
b.literal("asd"),
b.literal('asd'),
b.identifier('bar'),
]);
var vars = Collection.fromNodes([ast])
Expand Down Expand Up @@ -121,7 +119,6 @@ describe('Collection API', function() {

describe('closestScope', function() {
it('gets the closest scope', function() {
var program = ast;
var functionDeclaration = ast.body[1];
var scopes = Collection.fromNodes([ast])
.find(types.Identifier)
Expand All @@ -143,20 +140,30 @@ describe('Collection API', function() {
expect(decl.nodes()[0]).toBe(functionDeclaration);
});

it('allows to filter nodes', function() {
var functionDeclaration = ast.body[1];
var decl = Collection.fromNodes([ast])
.find(types.Identifier, {name: 'f'})
.closest(types.FunctionDeclaration);

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

decl = Collection.fromNodes([ast])
.find(types.Identifier, {name: 'foo'})
.closest(types.FunctionDeclaration);

expect(decl.size()).toBe(0);
it('allows to filter nodes by pattern', function() {
var decl = b.functionDeclaration(
b.identifier('foo'),
[],
b.blockStatement([
b.functionDeclaration(
b.identifier('foo'),
[],
b.blockStatement([
b.returnStatement(
b.literal(3)
)
])
),
])
);
var literals = Collection.fromNodes([decl])
.find(types.Literal);
expect(literals.get(0).node.value).toBe(3);
var closest = literals.closest(
types.FunctionDeclaration,
{id: {name: 'foo'}}
);
expect(closest.get(0).node.id.name).toBe('foo');
});
});

Expand All @@ -182,9 +189,9 @@ describe('Collection API', function() {
describe('Mutation', function() {
describe('replaceWith', function() {
it('handles simple AST node replacement', function() {
var ast = b.sequenceExpression([
var ast = b.sequenceExpression([ // eslint-disable-line no-shadow
b.identifier('foo'),
b.literal("asd"),
b.literal('asd'),
b.identifier('bar'),
]);
var newNode = b.identifier('xyz');
Expand All @@ -197,9 +204,9 @@ describe('Collection API', function() {
});

it('accepts an array as replacement', function() {
var ast = b.sequenceExpression([
var ast = b.sequenceExpression([ // eslint-disable-line no-shadow
b.identifier('foo'),
b.literal("asd"),
b.literal('asd'),
b.identifier('bar'),
]);
var newNode1 = b.identifier('xyz');
Expand All @@ -214,9 +221,9 @@ describe('Collection API', function() {
});

it('accepts a function as replacement ', function() {
var ast = b.sequenceExpression([
var ast = b.sequenceExpression([ // eslint-disable-line no-shadow
b.identifier('foo'),
b.literal("asd"),
b.literal('asd'),
b.identifier('bar'),
]);

Expand All @@ -230,7 +237,6 @@ describe('Collection API', function() {
return b.identifier(path.value.name + i);
});

var newNode = b.variableDeclarator(b.identifier('xyz'), null);
var S = Collection.fromNodes([ast]);
S.find(types.Identifier)
.replaceWith(replaceFunction);
Expand All @@ -246,13 +252,13 @@ describe('Collection API', function() {

describe('insertBefore', function() {
it('inserts a new node before the current one', function() {
var ast = b.variableDeclaration(
var ast = b.variableDeclaration( // eslint-disable-line no-shadow
'var',
[b.variableDeclarator(b.identifier('foo'), null)]
);
var one = b.variableDeclarator(b.identifier('one'), null);

var S = Collection.fromNodes([ast])
Collection.fromNodes([ast])
.find(types.VariableDeclarator)
.insertBefore(one);

Expand All @@ -261,14 +267,14 @@ describe('Collection API', function() {
});

it('accepts an array of nodes', function() {
var ast = b.variableDeclaration(
var ast = b.variableDeclaration( // eslint-disable-line no-shadow
'var',
[b.variableDeclarator(b.identifier('foo'), null)]
);
var one = b.variableDeclarator(b.identifier('one'), null);
var two = b.variableDeclarator(b.identifier('two'), null);

var S = Collection.fromNodes([ast])
Collection.fromNodes([ast])
.find(types.VariableDeclarator)
.insertBefore([one, two]);

Expand All @@ -281,9 +287,9 @@ describe('Collection API', function() {
var x = b.identifier('x');
var foo = b.identifier('foo');
var bar = b.identifier('bar');
var ast = b.sequenceExpression([foo, bar]);
var ast = b.sequenceExpression([foo, bar]); // eslint-disable-line no-shadow

var S = Collection.fromNodes([ast])
Collection.fromNodes([ast])
.find(types.Identifier)
.insertBefore(function() {
return x;
Expand All @@ -298,13 +304,13 @@ describe('Collection API', function() {

describe('insertAfter', function() {
it('inserts a new node after the current one', function() {
var ast = b.variableDeclaration(
var ast = b.variableDeclaration( // eslint-disable-line no-shadow
'var',
[b.variableDeclarator(b.identifier('foo'), null)]
);
var one = b.variableDeclarator(b.identifier('one'), null);

var S = Collection.fromNodes([ast])
Collection.fromNodes([ast])
.find(types.VariableDeclarator)
.insertAfter(one);

Expand All @@ -313,14 +319,14 @@ describe('Collection API', function() {
});

it('accepts an array of nodes', function() {
var ast = b.variableDeclaration(
var ast = b.variableDeclaration( // eslint-disable-line no-shadow
'var',
[b.variableDeclarator(b.identifier('foo'), null)]
);
var one = b.variableDeclarator(b.identifier('one'), null);
var two = b.variableDeclarator(b.identifier('two'), null);

var S = Collection.fromNodes([ast])
Collection.fromNodes([ast])
.find(types.VariableDeclarator)
.insertAfter([one, two]);

Expand All @@ -333,9 +339,9 @@ describe('Collection API', function() {
var x = b.identifier('x');
var foo = b.identifier('foo');
var bar = b.identifier('bar');
var ast = b.sequenceExpression([foo, bar]);
var ast = b.sequenceExpression([foo, bar]); // eslint-disable-line no-shadow

var S = Collection.fromNodes([ast])
Collection.fromNodes([ast])
.find(types.Identifier)
.insertAfter(function() {
return x;
Expand All @@ -352,9 +358,9 @@ describe('Collection API', function() {
it('removes a node if it is part of the body of a statement', function() {
var x = b.expressionStatement(b.identifier('x'));
var y = b.expressionStatement(b.identifier('y'));
var ast = b.program([x, y]);
var ast = b.program([x, y]); // eslint-disable-line no-shadow

var S = Collection.fromNodes([ast])
Collection.fromNodes([ast])
.find(types.Identifier, {name: 'x'})
.remove();

Expand All @@ -365,12 +371,12 @@ describe('Collection API', function() {
it('removes a node if it is a function param', function() {
var x = b.identifier('x');
var y = b.identifier('y');
var ast = b.arrowFunctionExpression(
var ast = b.arrowFunctionExpression( // eslint-disable-line no-shadow
[x, b.identifier('z')],
y
);

var S = Collection.fromNodes([ast])
Collection.fromNodes([ast])
.find(types.Identifier, {name: 'z'})
.remove();
expect(ast.params.length).toBe(1);
Expand Down

0 comments on commit a1be267

Please sign in to comment.