Skip to content

Commit

Permalink
do binding in remapIdentifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Apr 12, 2024
1 parent 270db5f commit 56aad20
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,6 @@ export function makePlugin<EnvSpecificOptions>(loadOptions: (opts: EnvSpecificOp
},
},

Identifier(path: NodePath<t.Identifier>) {
if (path.node.name) {
const binding = path.scope.getBinding(path.node.name);
if (binding && binding.path.node !== path.parent) {
binding.reference(path);
}
}
},

TaggedTemplateExpression(
path: NodePath<t.TaggedTemplateExpression>,
state: State<EnvSpecificOptions>
Expand Down Expand Up @@ -455,13 +446,18 @@ function buildPrecompileOptions<EnvSpecificOptions>(
// if scope has different keys and values, this function will remap the keys to the values
// you can see an example of this in the test "correctly handles scope if it contains keys and values"
function remapIdentifiers(ast: Babel.types.File, babel: typeof Babel, scopeLocals: ScopeLocals) {
if (!scopeLocals.needsRemapping()) {
// do nothing if all keys are the same as their values
return;
}

babel.traverse(ast, {
Identifier(path: NodePath<t.Identifier>) {
if (path.node.name) {
const binding = path.scope.getBinding(path.node.name);
if (binding && binding.path.node !== path.parent) {
binding.reference(path);
}
}
if (!scopeLocals.needsRemapping()) {
// do nothing if all keys are the same as their values
return;
}
if (scopeLocals.has(path.node.name) && path.node.name !== scopeLocals.get(path.node.name)) {
// replace the path only if the key is different from the value
path.replaceWith(babel.types.identifier(scopeLocals.get(path.node.name)));
Expand Down

0 comments on commit 56aad20

Please sign in to comment.