Skip to content

Commit 561efa7

Browse files
authored
Merge pull request #504 from facebook/bablyon-renames
Add renameTo filters for Babel 6+ node types
2 parents bc2db1a + 5f78598 commit 561efa7

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/collections/VariableDeclarator.js

+27
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ const transformMethods = {
103103
return false;
104104
}
105105

106+
if (
107+
types.ObjectProperty.check(parent) &&
108+
parent.key === path.node &&
109+
!parent.computed
110+
) {
111+
// { oldName: 3 }
112+
return false;
113+
}
114+
115+
if (
116+
types.ObjectMethod.check(parent) &&
117+
parent.key === path.node &&
118+
!parent.computed
119+
) {
120+
// { oldName() {} }
121+
return false;
122+
}
123+
106124
if (
107125
types.MethodDefinition.check(parent) &&
108126
parent.key === path.node &&
@@ -112,6 +130,15 @@ const transformMethods = {
112130
return false;
113131
}
114132

133+
if (
134+
types.ClassMethod.check(parent) &&
135+
parent.key === path.node &&
136+
!parent.computed
137+
) {
138+
// class A { oldName() {} }
139+
return false;
140+
}
141+
115142
if (
116143
types.ClassProperty.check(parent) &&
117144
parent.key === path.node &&

src/collections/__tests__/VariableDeclarator-test.js

+56
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,62 @@ describe('VariableDeclarators', function() {
170170

171171
expect(identifiers.length).toBe(1);
172172
});
173+
174+
describe('parsing with bablylon', function() {
175+
it('does not rename object property', function () {
176+
nodes = [
177+
recast.parse('var foo = 42; var obj = { foo: null };', {
178+
parser: getParser('babylon'),
179+
}).program
180+
];
181+
Collection
182+
.fromNodes(nodes)
183+
.findVariableDeclarators('foo').renameTo('newFoo');
184+
185+
expect(
186+
Collection.fromNodes(nodes).find(types.Identifier, { name: 'newFoo' }).length
187+
).toBe(1);
188+
expect(
189+
Collection.fromNodes(nodes).find(types.Identifier, { name: 'foo' }).length
190+
).toBe(1);
191+
})
192+
193+
it('does not rename object method', function () {
194+
nodes = [
195+
recast.parse('var foo = 42; var obj = { foo() {} };', {
196+
parser: getParser('babylon'),
197+
}).program
198+
];
199+
Collection
200+
.fromNodes(nodes)
201+
.findVariableDeclarators('foo').renameTo('newFoo');
202+
203+
expect(
204+
Collection.fromNodes(nodes).find(types.Identifier, { name: 'newFoo' }).length
205+
).toBe(1);
206+
expect(
207+
Collection.fromNodes(nodes).find(types.Identifier, { name: 'foo' }).length
208+
).toBe(1);
209+
})
210+
211+
it('does not rename class method', function () {
212+
nodes = [
213+
recast.parse('var foo = 42; class A { foo() {} }', {
214+
parser: getParser('babylon'),
215+
}).program
216+
];
217+
Collection
218+
.fromNodes(nodes)
219+
.findVariableDeclarators('foo').renameTo('newFoo');
220+
221+
expect(
222+
Collection.fromNodes(nodes).find(types.Identifier, { name: 'newFoo' }).length
223+
).toBe(1);
224+
expect(
225+
Collection.fromNodes(nodes).find(types.Identifier, { name: 'foo' }).length
226+
).toBe(1);
227+
})
228+
});
173229
});
174230

175231
});

0 commit comments

Comments
 (0)