From 0eeedee95e04563af8092f28fcb75dc65b1da583 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Fri, 31 Mar 2023 12:35:17 +0100 Subject: [PATCH] [hir] Rewrite identifiers to be consistent If we resolve identifiers to be different from the binding, update the binding to the new name. --- compiler/forget/src/HIR/HIRBuilder.ts | 6 +++++- .../_bug.lambda-reassign-shadowed-primitive.expect.md | 2 +- ...nction-renamed-ref.js => capturing-function-renamed-ref} | 0 ...f.expect.md => capturing-function-renamed-ref.expect.md} | 2 +- ...ct.expect.md => lambda-mutate-shadowed-object.expect.md} | 2 +- ...-shadowed-object.js => lambda-mutate-shadowed-object.js} | 0 6 files changed, 8 insertions(+), 4 deletions(-) rename compiler/forget/src/__tests__/fixtures/compiler/{_bug.capturing-function-renamed-ref.js => capturing-function-renamed-ref} (100%) rename compiler/forget/src/__tests__/fixtures/compiler/{_bug.capturing-function-renamed-ref.expect.md => capturing-function-renamed-ref.expect.md} (96%) rename compiler/forget/src/__tests__/fixtures/compiler/{_bug.lambda-mutate-shadowed-object.expect.md => lambda-mutate-shadowed-object.expect.md} (96%) rename compiler/forget/src/__tests__/fixtures/compiler/{_bug.lambda-mutate-shadowed-object.js => lambda-mutate-shadowed-object.js} (100%) diff --git a/compiler/forget/src/HIR/HIRBuilder.ts b/compiler/forget/src/HIR/HIRBuilder.ts index 76a2c61f6e..bd39ffee1b 100644 --- a/compiler/forget/src/HIR/HIRBuilder.ts +++ b/compiler/forget/src/HIR/HIRBuilder.ts @@ -219,7 +219,11 @@ export default class HIRBuilder { } } - return this.resolveBinding(binding.identifier); + const resolvedBinding = this.resolveBinding(binding.identifier); + if (resolvedBinding.name && resolvedBinding.name !== originalName) { + binding.scope.rename(originalName, resolvedBinding.name); + } + return resolvedBinding; } resolveBinding(node: t.Identifier): Identifier { diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-shadowed-primitive.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-shadowed-primitive.expect.md index 8a917699fd..3655fff153 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-shadowed-primitive.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-shadowed-primitive.expect.md @@ -31,7 +31,7 @@ function Component() { const x = t0; const fn = function () { - x = 42; + x_0 = 42; }; fn(); return x; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-function-renamed-ref.js b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-renamed-ref similarity index 100% rename from compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-function-renamed-ref.js rename to compiler/forget/src/__tests__/fixtures/compiler/capturing-function-renamed-ref diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-function-renamed-ref.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-renamed-ref.expect.md similarity index 96% rename from compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-function-renamed-ref.expect.md rename to compiler/forget/src/__tests__/fixtures/compiler/capturing-function-renamed-ref.expect.md index d2f4fdc4b9..41a0241da9 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-function-renamed-ref.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-renamed-ref.expect.md @@ -33,7 +33,7 @@ function component(a, b) { const z_0 = { b }; (function () { - mutate(z); + mutate(z_0); })(); return z; } diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-mutate-shadowed-object.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.expect.md similarity index 96% rename from compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-mutate-shadowed-object.expect.md rename to compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.expect.md index 042e24d871..402749fe4e 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-mutate-shadowed-object.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.expect.md @@ -32,7 +32,7 @@ function Component() { const x_0 = []; const fn = function () { - mutate(x); + mutate(x_0); }; fn(); return x; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-mutate-shadowed-object.js b/compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.js similarity index 100% rename from compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-mutate-shadowed-object.js rename to compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.js