-
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?
Changes from all commits
1156707
ed2cc57
24cd553
0bcbc62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1901,9 +1901,24 @@ export function transformTypeScript(context: TransformationContext) { | |
function transformEnumMemberDeclarationValue(member: EnumMember): Expression { | ||
const value = resolver.getConstantValue(member); | ||
if (value !== undefined) { | ||
return typeof value === "string" ? factory.createStringLiteral(value) : | ||
value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value))) : | ||
factory.createNumericLiteral(value); | ||
if (typeof value === "string") { | ||
return factory.createStringLiteral(value); | ||
} | ||
if (Number.isNaN(value)) { | ||
return resolver.isNameReferencingGlobalValueAtLocation("NaN", member) | ||
? 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I guess that would emit as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's what I started with :p
and why I changed it to the current version 😉 |
||
if (resolver.isNameReferencingGlobalValueAtLocation("Infinity", member)) { | ||
return value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createIdentifier("Infinity")) : factory.createIdentifier("Infinity"); | ||
} | ||
const dividend = value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(1)) : factory.createNumericLiteral(1); | ||
return factory.createBinaryExpression(dividend, SyntaxKind.SlashToken, factory.createNumericLiteral(0)); | ||
} | ||
return value < 0 | ||
? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value))) | ||
: factory.createNumericLiteral(value); | ||
} | ||
else { | ||
enableSubstitutionForNonQualifiedEnumMembers(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//// [tests/cases/conformance/enums/enumShadowedInfinityNaN2.ts] //// | ||
|
||
//// [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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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”
cant disagree :p the issue was labeled as a bug and not a wontfix, it’s similar to the merged: #55018 |
||
let NaN = 5; | ||
|
||
export enum A { | ||
X = 1 / 0, | ||
Y = -1 / 0, | ||
B = 0 / 0, | ||
} | ||
|
||
|
||
//// [enumShadowedInfinityNaN2.js] | ||
"use strict"; | ||
// repro https://github.com/microsoft/TypeScript/issues/55091 | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.A = void 0; | ||
var Infinity = 3; | ||
var NaN = 5; | ||
var A; | ||
(function (A) { | ||
A[A["X"] = 1 / 0] = "X"; | ||
A[A["Y"] = -1 / 0] = "Y"; | ||
A[A["B"] = 0 / 0] = "B"; | ||
})(A || (exports.A = A = {})); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//// [tests/cases/conformance/enums/enumShadowedInfinityNaN2.ts] //// | ||
|
||
=== enumShadowedInfinityNaN2.ts === | ||
// repro https://github.com/microsoft/TypeScript/issues/55091 | ||
|
||
let Infinity = 3; | ||
>Infinity : Symbol(Infinity, Decl(enumShadowedInfinityNaN2.ts, 2, 3)) | ||
|
||
let NaN = 5; | ||
>NaN : Symbol(NaN, Decl(enumShadowedInfinityNaN2.ts, 3, 3)) | ||
|
||
export enum A { | ||
>A : Symbol(A, Decl(enumShadowedInfinityNaN2.ts, 3, 12)) | ||
|
||
X = 1 / 0, | ||
>X : Symbol(A.X, Decl(enumShadowedInfinityNaN2.ts, 5, 15)) | ||
|
||
Y = -1 / 0, | ||
>Y : Symbol(A.Y, Decl(enumShadowedInfinityNaN2.ts, 6, 14)) | ||
|
||
B = 0 / 0, | ||
>B : Symbol(A.B, Decl(enumShadowedInfinityNaN2.ts, 7, 15)) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//// [tests/cases/conformance/enums/enumShadowedInfinityNaN2.ts] //// | ||
|
||
=== enumShadowedInfinityNaN2.ts === | ||
// repro https://github.com/microsoft/TypeScript/issues/55091 | ||
|
||
let Infinity = 3; | ||
>Infinity : number | ||
>3 : 3 | ||
|
||
let NaN = 5; | ||
>NaN : number | ||
>5 : 5 | ||
|
||
export enum A { | ||
>A : A | ||
|
||
X = 1 / 0, | ||
>X : A.X | ||
>1 / 0 : number | ||
>1 : 1 | ||
>0 : 0 | ||
|
||
Y = -1 / 0, | ||
>Y : A.Y | ||
>-1 / 0 : number | ||
>-1 : -1 | ||
>1 : 1 | ||
>0 : 0 | ||
|
||
B = 0 / 0, | ||
>B : A.B | ||
>0 / 0 : number | ||
>0 : 0 | ||
>0 : 0 | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// repro https://github.com/microsoft/TypeScript/issues/55091 | ||
|
||
let Infinity = 3; | ||
let NaN = 5; | ||
|
||
export enum A { | ||
X = 1 / 0, | ||
Y = -1 / 0, | ||
B = 0 / 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.
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
andInfinity
should not be passed tofactory.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
andNaN
are normalized~ by the proposed changes.