fix(transformer): do not rename accessible identifier references (#3623)

close: #3620

In `Babel`, the expected output is:

```ts
var x = 10;
var Foo = function(Foo) {
       Foo[Foo['a'] = 10] = 'a';
       Foo[Foo['b'] = 10] = 'b';
       Foo[Foo['c'] = 30] = 'c';
       return Foo;
}(Foo || {});
```

IMO, `Foo.b + x` is enough, because `x` is not a const variable. The
output same as with `typescript`
This commit is contained in:
Dunqing 2024-06-13 15:26:38 +08:00 committed by GitHub
parent f96b6b18c9
commit 59666e0127
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 1 deletions

View file

@ -535,6 +535,16 @@ impl<'a, 'b> VisitMut<'a> for IdentifierReferenceRename<'a, 'b> {
None
}
Expression::Identifier(ident) => {
// If the identifier is binding in current/parent scopes,
// and it is not a member of the enum,
// we don't need to rename it.
// `var c = 1; enum A { a = c }` -> `var c = 1; enum A { a = c }
if !self.previous_enum_members.contains_key(&ident.name)
&& self.ctx.scopes().has_binding(self.ctx.current_scope_id(), &ident.name)
{
return;
}
// TODO: shadowed case, e.g. let ident = 1; ident; // ident is not an enum
// enum_name.identifier
let ident_reference = IdentifierReference::new(SPAN, self.enum_name.clone());

View file

@ -1,6 +1,6 @@
commit: 12619ffe
Passed: 3/3
Passed: 4/4
# All Passed:
* babel-plugin-transform-typescript

View file

@ -0,0 +1,7 @@
var x = 10;
enum Foo {
a = 10,
b = a,
c = b + x,
}

View file

@ -0,0 +1,7 @@
var x = 10;
var Foo = function(Foo) {
Foo[Foo['a'] = 10] = 'a';
Foo[Foo['b'] = 10] = 'b';
Foo[Foo['c'] = Foo.b + x] = 'c';
return Foo;
}(Foo || {});