New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scope.rename with renamed re-export #9266

Open
mischnic opened this Issue Jan 1, 2019 · 3 comments

Comments

Projects
None yet
4 participants
@mischnic
Copy link

mischnic commented Jan 1, 2019

Bug Report

Current Behavior
Running this visitor on the input code

{
  FunctionDeclaration(path) {
    path.scope.rename(path.node.id.name, "a")
  }
}

renames the reexport as well:

function a() {
  return "YES";
}export { a as x };

export { a as someFunction } from './lib2.js';

Input Code

export function x() {
  return "YES";
}

export { x as someFunction } from './lib2.js'

Expected behavior/code
The identifier x in the export specifier has nothing to do with the function named x. Renaming the function should not affect the re-export.

function a() {
  return "YES";
}export { a as x };

export { x as someFunction } from './lib2.js';

Babel Configuration (.babelrc, package.json, cli command)

Tested in https://astexplorer.net/

Environment

  • Babel version(s): 7.2.0
  • Node/npm version: -
  • OS: -
  • Monorepo: -
  • How you are using Babel: astexplorer.net
@babel-bot

This comment has been minimized.

Copy link
Collaborator

babel-bot commented Jan 1, 2019

Hey @mischnic! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@mischnic

This comment has been minimized.

Copy link

mischnic commented Jan 2, 2019

A crude fix (if there is no bug deeper in babel): (path.parentPath.parent.source is used to determine re-exports)

diff --git a/packages/babel-traverse/src/scope/lib/renamer.js b/packages/babel-traverse/src/scope/lib/renamer.js
index cdfd9081..6ec401e6 100644
--- a/packages/babel-traverse/src/scope/lib/renamer.js
+++ b/packages/babel-traverse/src/scope/lib/renamer.js
@@ -3,8 +3,13 @@ import splitExportDeclaration from "@babel/helper-split-export-declaration";
 import * as t from "@babel/types";

 const renameVisitor = {
-  ReferencedIdentifier({ node }, state) {
-    if (node.name === state.oldName) {
+  ReferencedIdentifier(path, state) {
+    const { node } = path;
+    if(t.isExportSpecifier(path.parent) && path.parentPath.parent.source !== null){
+      return;
+    }
+
     if (node.name === state.oldName) {
       node.name = state.newName;
     }
   },
@devongovett

This comment has been minimized.

Copy link
Contributor

devongovett commented Jan 12, 2019

Seems like the real issue is that x is in the referencePaths at all. x in the export specifier does not actually reference the local variable x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment