Skip to content

Commit

Permalink
fix(transformer): do not rename accessible identifier references (#3623)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
Dunqing committed Jun 13, 2024
1 parent f96b6b1 commit 59666e0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions crates/oxc_transformer/src/typescript/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: 12619ffe

Passed: 3/3
Passed: 4/4

# All Passed:
* babel-plugin-transform-typescript
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var x = 10;

enum Foo {
a = 10,
b = a,
c = b + x,
}
Original file line number Diff line number Diff line change
@@ -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 || {});

0 comments on commit 59666e0

Please sign in to comment.