Skip to content

Commit 3d7c53d

Browse files
committed
Revert "Revert "Disallow numeric literals with negative numbers (microsoft#55268)" (microsoft#56422)"
This reverts commit 3333e90.
1 parent b6121e4 commit 3d7c53d

22 files changed

+31
-566
lines changed

src/compiler/checker.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -8331,7 +8331,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
83318331
return factory.createStringLiteral(name, !!singleQuote);
83328332
}
83338333
if (isNumericLiteralName(name) && startsWith(name, "-")) {
8334-
return factory.createComputedPropertyName(factory.createNumericLiteral(+name));
8334+
return factory.createComputedPropertyName(factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(+name))));
83358335
}
83368336
return createPropertyNameNodeForIdentifierOrLiteral(name, getEmitScriptTarget(compilerOptions), singleQuote, stringNamed, isMethod);
83378337
}
@@ -39021,9 +39021,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3902139021
return hasSkipDirectInferenceFlag(node) ?
3902239022
blockedStringType :
3902339023
getFreshTypeOfLiteralType(getStringLiteralType((node as StringLiteralLike).text));
39024-
case SyntaxKind.NumericLiteral:
39024+
case SyntaxKind.NumericLiteral: {
3902539025
checkGrammarNumericLiteral(node as NumericLiteral);
39026-
return getFreshTypeOfLiteralType(getNumberLiteralType(+(node as NumericLiteral).text));
39026+
const value = +(node as NumericLiteral).text;
39027+
if (!isFinite(value)) {
39028+
return numberType;
39029+
}
39030+
return getFreshTypeOfLiteralType(getNumberLiteralType(value));
39031+
}
3902739032
case SyntaxKind.BigIntLiteral:
3902839033
checkGrammarBigIntLiteral(node as BigIntLiteral);
3902939034
return getFreshTypeOfLiteralType(getBigIntLiteralType({
@@ -47998,8 +48003,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4799848003
if (enumResult) return enumResult;
4799948004
const literalValue = (type as LiteralType).value;
4800048005
return typeof literalValue === "object" ? factory.createBigIntLiteral(literalValue) :
48001-
typeof literalValue === "number" ? factory.createNumericLiteral(literalValue) :
48002-
factory.createStringLiteral(literalValue);
48006+
typeof literalValue === "string" ? factory.createStringLiteral(literalValue) :
48007+
literalValue < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(literalValue))) :
48008+
factory.createNumericLiteral(literalValue);
4800348009
}
4800448010

4800548011
function createLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration, tracker: SymbolTracker) {

src/compiler/factory/nodeFactory.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
CaseOrDefaultClause,
4545
cast,
4646
CatchClause,
47+
CharacterCodes,
4748
ClassDeclaration,
4849
ClassElement,
4950
ClassExpression,
@@ -1254,8 +1255,10 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
12541255

12551256
// @api
12561257
function createNumericLiteral(value: string | number, numericLiteralFlags: TokenFlags = TokenFlags.None): NumericLiteral {
1258+
const text = typeof value === "number" ? value + "" : value;
1259+
Debug.assert(text.charCodeAt(0) !== CharacterCodes.minus, "Negative numbers should be created in combination with createPrefixUnaryExpression");
12571260
const node = createBaseDeclaration<NumericLiteral>(SyntaxKind.NumericLiteral);
1258-
node.text = typeof value === "number" ? value + "" : value;
1261+
node.text = text;
12591262
node.numericLiteralFlags = numericLiteralFlags;
12601263
if (numericLiteralFlags & TokenFlags.BinaryOrOctalSpecifier) node.transformFlags |= TransformFlags.ContainsES2015;
12611264
return node;

src/compiler/transformers/declarations.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,14 @@ export function transformDeclarations(context: TransformationContext) {
17981798
if (shouldStripInternal(m)) return;
17991799
// Rewrite enum values to their constants, if available
18001800
const constValue = resolver.getConstantValue(m);
1801-
return preserveJsDoc(factory.updateEnumMember(m, m.name, constValue !== undefined ? typeof constValue === "string" ? factory.createStringLiteral(constValue) : factory.createNumericLiteral(constValue) : undefined), m);
1801+
const newInitializer = constValue === undefined
1802+
? undefined
1803+
: typeof constValue === "string"
1804+
? factory.createStringLiteral(constValue)
1805+
: constValue < 0
1806+
? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(constValue)))
1807+
: factory.createNumericLiteral(constValue);
1808+
return preserveJsDoc(factory.updateEnumMember(m, m.name, newInitializer), m);
18021809
})),
18031810
));
18041811
}

src/compiler/transformers/generators.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2524,7 +2524,7 @@ export function transformGenerators(context: TransformationContext): (x: SourceF
25242524
labelExpressions = [];
25252525
}
25262526

2527-
const expression = factory.createNumericLiteral(-1);
2527+
const expression = factory.createNumericLiteral(Number.MAX_SAFE_INTEGER);
25282528
if (labelExpressions[label] === undefined) {
25292529
labelExpressions[label] = [expression];
25302530
}

src/compiler/transformers/ts.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,9 @@ export function transformTypeScript(context: TransformationContext) {
19011901
function transformEnumMemberDeclarationValue(member: EnumMember): Expression {
19021902
const value = resolver.getConstantValue(member);
19031903
if (value !== undefined) {
1904-
return typeof value === "string" ? factory.createStringLiteral(value) : factory.createNumericLiteral(value);
1904+
return typeof value === "string" ? factory.createStringLiteral(value) :
1905+
value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value))) :
1906+
factory.createNumericLiteral(value);
19051907
}
19061908
else {
19071909
enableSubstitutionForNonQualifiedEnumMembers();

tests/baselines/reference/binaryIntegerLiteral.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var bin3 = 0B1111111111111111111111111111111111111111111111110100101010000001011
1515

1616
var bin4 = 0B111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101001010100000010111110001111111111;
1717
>bin4 : number
18-
>0B111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101001010100000010111110001111111111 : Infinity
18+
>0B111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101001010100000010111110001111111111 : number
1919

2020
var obj1 = {
2121
>obj1 : { 26: string; a: number; bin1: number; b: number; Infinity: boolean; }

0 commit comments

Comments
 (0)