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: Infinity in enums #15774

Merged
merged 1 commit into from Jul 12, 2023
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
4 changes: 4 additions & 0 deletions packages/babel-plugin-transform-typescript/src/enum.ts
Expand Up @@ -301,6 +301,10 @@ function computeConstantValue(
} else if (path.isIdentifier()) {
const name = path.node.name;

if (["Infinity", "NaN"].includes(name)) {
return Number(name);
}

let value = prevMembers?.get(name);
if (value !== undefined) {
return value;
Expand Down
@@ -0,0 +1,11 @@
const v = 42;
const v2 = Infinity;
var Infinity = 1; // Known inconsistencies
Copy link
Member

Choose a reason for hiding this comment

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

Note that in scripts this doesn't actually change the value of Infinity. However, there is another case in which it does and TS is not behaving as I would expect: microsoft/TypeScript#54981

enum StateEnum {
okay = 0,
neg = -Infinity,
pos = Infinity,
nan = NaN,
ext = v,
ext2 = v2,
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-typescript"]
}
@@ -0,0 +1,12 @@
const v = 42;
const v2 = Infinity;
var Infinity = 1; // Known inconsistencies
var StateEnum = /*#__PURE__*/function (StateEnum) {
StateEnum[StateEnum["okay"] = 0] = "okay";
StateEnum[StateEnum["neg"] = -Infinity] = "neg";
StateEnum[StateEnum["pos"] = Infinity] = "pos";
StateEnum[StateEnum["nan"] = NaN] = "nan";
StateEnum[StateEnum["ext"] = 42] = "ext";
StateEnum[StateEnum["ext2"] = 1] = "ext2";
Copy link
Member Author

Choose a reason for hiding this comment

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

This is inconsistent with TS, if we want to align, it is most convenient to add a feature in traverse.

return StateEnum;
}(StateEnum || {});