Skip to content

Commit

Permalink
[babel 8] Remove block argument from Scope#rename (#15288)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jan 12, 2023
1 parent 7f13bf9 commit 7a4ca1b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
15 changes: 12 additions & 3 deletions packages/babel-traverse/src/scope/index.ts
Expand Up @@ -619,11 +619,20 @@ export default class Scope {
}
}

rename(oldName: string, newName?: string, block?: t.Pattern | t.Scopable) {
rename(
oldName: string,
newName?: string,
// prettier-ignore
/* Babel 7 - block?: t.Pattern | t.Scopable */
) {
const binding = this.getBinding(oldName);
if (binding) {
newName = newName || this.generateUidIdentifier(oldName).name;
return new Renamer(binding, oldName, newName).rename(block);
newName ||= this.generateUidIdentifier(oldName).name;
const renamer = new Renamer(binding, oldName, newName);
return process.env.BABEL_8_BREAKING
? renamer.rename()
: // @ts-expect-error: babel 7->8
renamer.rename(arguments[2]);
}
}

Expand Down
14 changes: 10 additions & 4 deletions packages/babel-traverse/src/scope/lib/renamer.ts
Expand Up @@ -113,8 +113,7 @@ export default class Renamer {
// );
}

// TODO(Babel 8): Remove this `block` parameter. It's not needed anywhere.
rename(block?: t.Pattern | t.Scopable) {
rename(/* Babel 7 - block?: t.Pattern | t.Scopable */) {
const { binding, oldName, newName } = this;
const { scope, path } = binding;

Expand All @@ -133,8 +132,11 @@ export default class Renamer {
}
}

const blockToTraverse = process.env.BABEL_8_BREAKING
? scope.block
: (arguments[0] as t.Pattern | t.Scopable) || scope.block;
traverseNode(
block || scope.block,
blockToTraverse,
explode(renameVisitor),
scope,
this,
Expand All @@ -144,7 +146,11 @@ export default class Renamer {
{ discriminant: true },
);

if (!block) {
if (process.env.BABEL_8_BREAKING) {
scope.removeOwnBinding(oldName);
scope.bindings[newName] = binding;
this.binding.identifier.name = newName;
} else if (!arguments[0]) {
scope.removeOwnBinding(oldName);
scope.bindings[newName] = binding;
this.binding.identifier.name = newName;
Expand Down

0 comments on commit 7a4ca1b

Please sign in to comment.