-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Don't emit enum members as evaluated Infinity
/NaN
when their symbols are shadowed
#55107
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
base: main
Are you sure you want to change the base?
Don't emit enum members as evaluated Infinity
/NaN
when their symbols are shadowed
#55107
Conversation
if (typeof value === "string") { | ||
return factory.createStringLiteral(value); | ||
} | ||
if (Number.isNaN(value)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternatively, I could just not use the constant value in those cases and fallback to the other branch - that would likely work. But I feel that NaN
and Infinity
should not be passed to factory.createNumericLiteral
so some changes to this branch of code here would have to be done anyway. I also like that those evaluated, yet shadowed, Infinity
and NaN
are normalized~ by the proposed changes.
Infinity
/NaN
when their values are shadowedInfinity
/NaN
when their symbols are shadowed
31c3e64
to
24cd553
Compare
Just for completeness’ sake, how does this interact with |
|
? factory.createIdentifier("NaN") | ||
: factory.createBinaryExpression(factory.createNumericLiteral(0), SyntaxKind.SlashToken, factory.createNumericLiteral(0)); | ||
} | ||
if (!isFinite(value)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of this alternative way to write it?
if (!isFinite(value) {
let result: Node = resolver.isNameReferencingGlobalValueAtLocation("Infinity", member) ?
factory.createIdentifier("Infinity") :
factory.createBinaryExpression(factory.createNumericLiteral(0), SyntaxKind.SlashToken, factory.createNumericLiteral(0));
if (value < 0) {
result = factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, result);
}
return result;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that would emit as -(1/0)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of this alternative way to write it?
That's what I started with :p
ed2cc57
I guess that would emit as -(1/0)?
and why I changed it to the current version 😉
//// [enumShadowedInfinityNaN2.ts] | ||
// repro https://github.com/microsoft/TypeScript/issues/55091 | ||
|
||
let Infinity = 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any more convincing global names that could get shadowed than Infinity and NaN? It's a stretch for people to create local names Infinity
or NaN
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think those are the only 2 that can be produced by the „constant evaluation”
It's a stretch for people to create local names Infinity or NaN.
cant disagree :p the issue was labeled as a bug and not a wontfix, it’s similar to the merged: #55018
c8001bd
to
dc59819
Compare
dc59819
to
0bcbc62
Compare
fixes #55091