Skip to content

Commit

Permalink
fix(transformer): do not rename accessible identifier references
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jun 11, 2024
1 parent f9c08d1 commit 7c2ee9d
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: 4bd1b2c2

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 7c2ee9d

Please sign in to comment.