Skip to content

Commit

Permalink
Merge pull request #504 from facebook/bablyon-renames
Browse files Browse the repository at this point in the history
Add renameTo filters for Babel 6+ node types
  • Loading branch information
ElonVolo committed Apr 18, 2022
2 parents bc2db1a + 5f78598 commit 561efa7
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
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
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 561efa7

Please sign in to comment.