Skip to content

Commit

Permalink
Add renameTo filters for Babel 6+ node types
Browse files Browse the repository at this point in the history
  • Loading branch information
henryqdineen authored and ElonVolo committed Apr 18, 2022
1 parent 9bc2dcd commit 5f78598
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/collections/VariableDeclarator.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ const transformMethods = {
return false;
}

if (
types.ObjectProperty.check(parent) &&
parent.key === path.node &&
!parent.computed
) {
// { oldName: 3 }
return false;
}

if (
types.ObjectMethod.check(parent) &&
parent.key === path.node &&
!parent.computed
) {
// { oldName() {} }
return false;
}

if (
types.MethodDefinition.check(parent) &&
parent.key === path.node &&
Expand All @@ -112,6 +130,15 @@ const transformMethods = {
return false;
}

if (
types.ClassMethod.check(parent) &&
parent.key === path.node &&
!parent.computed
) {
// class A { oldName() {} }
return false;
}

if (
types.ClassProperty.check(parent) &&
parent.key === path.node &&
Expand Down
56 changes: 56 additions & 0 deletions src/collections/__tests__/VariableDeclarator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,62 @@ describe('VariableDeclarators', function() {

expect(identifiers.length).toBe(1);
});

describe('parsing with bablylon', function() {
it('does not rename object property', function () {
nodes = [
recast.parse('var foo = 42; var obj = { foo: null };', {
parser: getParser('babylon'),
}).program
];
Collection
.fromNodes(nodes)
.findVariableDeclarators('foo').renameTo('newFoo');

expect(
Collection.fromNodes(nodes).find(types.Identifier, { name: 'newFoo' }).length
).toBe(1);
expect(
Collection.fromNodes(nodes).find(types.Identifier, { name: 'foo' }).length
).toBe(1);
})

it('does not rename object method', function () {
nodes = [
recast.parse('var foo = 42; var obj = { foo() {} };', {
parser: getParser('babylon'),
}).program
];
Collection
.fromNodes(nodes)
.findVariableDeclarators('foo').renameTo('newFoo');

expect(
Collection.fromNodes(nodes).find(types.Identifier, { name: 'newFoo' }).length
).toBe(1);
expect(
Collection.fromNodes(nodes).find(types.Identifier, { name: 'foo' }).length
).toBe(1);
})

it('does not rename class method', function () {
nodes = [
recast.parse('var foo = 42; class A { foo() {} }', {
parser: getParser('babylon'),
}).program
];
Collection
.fromNodes(nodes)
.findVariableDeclarators('foo').renameTo('newFoo');

expect(
Collection.fromNodes(nodes).find(types.Identifier, { name: 'newFoo' }).length
).toBe(1);
expect(
Collection.fromNodes(nodes).find(types.Identifier, { name: 'foo' }).length
).toBe(1);
})
});
});

});

0 comments on commit 5f78598

Please sign in to comment.