Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix references to enum values with merging #16137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 7 additions & 9 deletions packages/babel-plugin-transform-typescript/src/enum.ts
Expand Up @@ -155,7 +155,9 @@ const enumSelfReferenceVisitor = {
};

export function translateEnumValues(path: NodePath<t.TSEnumDeclaration>, t: t) {
const seen: PreviousEnumMembers = new Map();
const bindingIdentifier = path.scope.getBindingIdentifier(path.node.id.name);
const seen: PreviousEnumMembers = ENUMS.get(bindingIdentifier) ?? new Map();

// Start at -1 so the first enum member is its increment, 0.
let constValue: number | string | undefined = -1;
let lastName: string;
Expand Down Expand Up @@ -320,15 +322,11 @@ function computeConstantValue(
}

if (seen.has(path.node)) return;
seen.add(path.node);

const bindingInitPath = path.resolve(); // It only resolves constant bindings
if (bindingInitPath) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path.resolve() always returns something, so this check wasn't actually guarding anything.

seen.add(path.node);

value = computeConstantValue(bindingInitPath, undefined, seen);
prevMembers?.set(name, value);
return value;
}
value = computeConstantValue(path.resolve(), prevMembers, seen);
prevMembers?.set(name, value);
return value;
}
}

Expand Down
@@ -0,0 +1,14 @@
const Cat = 10;
const Dog = 20;

enum Animals {
Cat = 1
}

enum Animals {
Dog = 2
}

enum Animals {
CatDog = Cat | Dog
}
@@ -0,0 +1,14 @@
const Cat = 10;
const Dog = 20;
var Animals = /*#__PURE__*/function (Animals) {
Animals[Animals["Cat"] = 1] = "Cat";
return Animals;
}(Animals || {});
Animals = /*#__PURE__*/function (Animals) {
Animals[Animals["Dog"] = 2] = "Dog";
return Animals;
}(Animals || {});
Animals = /*#__PURE__*/function (Animals) {
Animals[Animals["CatDog"] = 3] = "CatDog";
return Animals;
}(Animals || {});
@@ -0,0 +1,7 @@
enum Animals {
Cat = 1,
Dog = 2
}
enum Animals {
CatDog = Cat - Dog
}
@@ -0,0 +1,9 @@
var Animals = /*#__PURE__*/function (Animals) {
Animals[Animals["Cat"] = 1] = "Cat";
Animals[Animals["Dog"] = 2] = "Dog";
return Animals;
}(Animals || {});
Animals = /*#__PURE__*/function (Animals) {
Animals[Animals["CatDog"] = -1] = "CatDog";
return Animals;
}(Animals || {});