Skip to content

Commit

Permalink
WIP: working on third test case
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jun 7, 2019
1 parent 5780360 commit fa11361
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion packages/babel-traverse/src/scope/lib/renamer.js
Expand Up @@ -29,6 +29,24 @@ const renameVisitor = {
},
};

function isSafeBinding(scope, node) {
if (!scope.hasOwnBinding(node.name)) return true;
const { kind } = scope.getOwnBinding(node.name);
return kind === "param" || kind === "local";
}

const outerBindingVisitor = {
ReferencedIdentifier(path, state) {
const { node } = path;
if (node.name === state.oldName && !isSafeBinding(state.scope, node)) {
state.paramDefaultOuterBinding = true;
path.stop();
}
},
Scope(path) {
path.skip();
},
};
export default class Renamer {
constructor(binding: Binding, oldName: string, newName: string) {
this.newName = newName;
Expand Down Expand Up @@ -116,7 +134,37 @@ export default class Renamer {
}
}

scope.traverse(block || scope.block, renameVisitor, this);
const state = {
paramDefaultOuterBinding: false,
scope,
oldName,
};
if (scope.path.isFunction()) {
const params = scope.path.get("params");
for (let i = 0; i < params.length; i++) {
const param = params[i];
if (param.isAssignmentPattern()) {
const right = param.get("right");
if (!state.paramDefaultOuterBinding) {
if (
right.isIdentifier() &&
right.node.body.name === oldName &&
!isSafeBinding(scope, right.node.body)
) {
state.paramDefaultOuterBinding = true;
break;
} else {
right.traverse(outerBindingVisitor, state);
}
}
}
}
}
if (state.paramDefaultOuterBinding) {
scope.traverse(block || scope.block.body, renameVisitor, this);
} else {
scope.traverse(block || scope.block, renameVisitor, this);
}

if (!block) {
scope.removeOwnBinding(oldName);
Expand Down

0 comments on commit fa11361

Please sign in to comment.