Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/rename-to-export-specifiers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"jscodeshift": patch
---

Fix `renameTo` changing export specifiers

Renaming a variable that is exported with `export { foo }` now produces `export { newFoo as foo }` instead of changing the exported name, and specifiers re-exported from another module (`export { foo } from 'module'`) are no longer renamed.
22 changes: 22 additions & 0 deletions src/collections/VariableDeclarator.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ const transformMethods = {
return false;
}

if (types.ExportSpecifier.check(parent)) {
if (parent.exported === path.node && parent.local !== path.node) {
// export { oldName }
return false;
}
if (path.parent.parent.node.source) {
// export { oldName } from 'module'
return false;
}
}

if (
types.JSXAttribute.check(parent) &&
parent.name === path.node &&
Expand Down Expand Up @@ -192,6 +203,17 @@ const transformMethods = {
path.parent.get('shorthand').replace(false);
}

if (
types.ExportSpecifier.check(parent) &&
parent.local === path.node &&
parent.exported.name === oldName
) {
// `local` and `exported` share the same source range in
// `export { oldName }`, so force recast to reprint the whole
// specifier as `export { newName as oldName }`.
parent.original = null;
}

path.get('name').replace(newName);
}
});
Expand Down
36 changes: 36 additions & 0 deletions src/collections/__tests__/VariableDeclarator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,42 @@ describe('VariableDeclarators', function() {
expect(identifiers.length).toBe(2);
});

it('keeps the exported name when renaming an exported variable', function () {
const ast = recast.parse([
'var foo = 42;',
'export { foo };',
'export { foo as baz };',
].join('\n'), {parser: getParser()});
nodes = [ast.program];

Collection.fromNodes(nodes)
.findVariableDeclarators('foo').renameTo('newFoo');

expect(recast.print(ast).code).toBe([
'var newFoo = 42;',
'export { newFoo as foo };',
'export { newFoo as baz };',
].join('\n'));
});

it('does not rename specifiers re-exported from another module', function () {
const ast = recast.parse([
'var foo = 42;',
'export { foo as baz } from "module";',
'export { foo } from "module2";',
].join('\n'), {parser: getParser()});
nodes = [ast.program];

Collection.fromNodes(nodes)
.findVariableDeclarators('foo').renameTo('newFoo');

expect(recast.print(ast).code).toBe([
'var newFoo = 42;',
'export { foo as baz } from "module";',
'export { foo } from "module2";',
].join('\n'));
});

describe('parsing with bablylon', function() {
it('does not rename object property', function () {
nodes = [
Expand Down