diff --git a/lib/scope.ts b/lib/scope.ts index 1a2f33e1..3c9d05f0 100644 --- a/lib/scope.ts +++ b/lib/scope.ts @@ -1,5 +1,6 @@ import { Fork } from "../types"; import typesPlugin from "./types"; +import pathVisitor from './path-visitor'; var hasOwn = Object.prototype.hasOwnProperty; @@ -391,6 +392,27 @@ export default function scopePlugin(fork: Fork) { scope = scope.parent; return scope; }; - + + Sp.rename = function(oldName, newName) { + var scope = this; + var parentPath = scope.path.parentPath; + + var targetScopeNode = scope.lookup(oldName).getBindings()[oldName][0].node; + + var PathVisitor = fork.use(pathVisitor); + PathVisitor.visit(parentPath, { + visitIdentifier: function(path) { + if (path.node.name === oldName) { + var pathScope = path.scope.lookup(oldName); + var scopeNode = pathScope.getBindings()[oldName][0].node; + if (scopeNode === targetScopeNode && path.name !== 'property') { + path.node.name = newName; + } + } + return false; + } + }); + } + return Scope; };