From 5f785988c820c31b6f2006e7ee30eb2ffdd58641 Mon Sep 17 00:00:00 2001 From: hdineen Date: Thu, 28 Jan 2021 21:48:36 -0500 Subject: [PATCH] Add renameTo filters for Babel 6+ node types --- src/collections/VariableDeclarator.js | 27 +++++++++ .../__tests__/VariableDeclarator-test.js | 56 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/collections/VariableDeclarator.js b/src/collections/VariableDeclarator.js index 7aac377d..00338f1c 100644 --- a/src/collections/VariableDeclarator.js +++ b/src/collections/VariableDeclarator.js @@ -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 && @@ -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 && diff --git a/src/collections/__tests__/VariableDeclarator-test.js b/src/collections/__tests__/VariableDeclarator-test.js index 8bbe26b5..b0eece1a 100644 --- a/src/collections/__tests__/VariableDeclarator-test.js +++ b/src/collections/__tests__/VariableDeclarator-test.js @@ -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); + }) + }); }); });