Don't emit enum members as evaluated Infinity/NaN when their symbols are shadowed#55107
Don't emit enum members as evaluated Infinity/NaN when their symbols are shadowed#55107Andarist wants to merge 4 commits intomicrosoft:mainfrom
Infinity/NaN when their symbols are shadowed#55107Conversation
| if (typeof value === "string") { | ||
| return factory.createStringLiteral(value); | ||
| } | ||
| if (Number.isNaN(value)) { |
There was a problem hiding this comment.
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.
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.
I guess that would emit as -(1/0)?
There was a problem hiding this comment.
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.
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.
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