Skip to content

Commit 64b5c79

Browse files
jakebaileyDanielRosenwasser
authored andcommitted
Revert "Disallow numeric literals with negative numbers (#55268)" (#56422)
1 parent 8e492d6 commit 64b5c79

22 files changed

+566
-31
lines changed

src/compiler/checker.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -8302,7 +8302,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
83028302
return factory.createStringLiteral(name, !!singleQuote);
83038303
}
83048304
if (isNumericLiteralName(name) && startsWith(name, "-")) {
8305-
return factory.createComputedPropertyName(factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(+name))));
8305+
return factory.createComputedPropertyName(factory.createNumericLiteral(+name));
83068306
}
83078307
return createPropertyNameNodeForIdentifierOrLiteral(name, getEmitScriptTarget(compilerOptions), singleQuote, stringNamed, isMethod);
83088308
}
@@ -38956,14 +38956,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3895638956
return hasSkipDirectInferenceFlag(node) ?
3895738957
blockedStringType :
3895838958
getFreshTypeOfLiteralType(getStringLiteralType((node as StringLiteralLike).text));
38959-
case SyntaxKind.NumericLiteral: {
38959+
case SyntaxKind.NumericLiteral:
3896038960
checkGrammarNumericLiteral(node as NumericLiteral);
38961-
const value = +(node as NumericLiteral).text;
38962-
if (!isFinite(value)) {
38963-
return numberType;
38964-
}
38965-
return getFreshTypeOfLiteralType(getNumberLiteralType(value));
38966-
}
38961+
return getFreshTypeOfLiteralType(getNumberLiteralType(+(node as NumericLiteral).text));
3896738962
case SyntaxKind.BigIntLiteral:
3896838963
checkGrammarBigIntLiteral(node as BigIntLiteral);
3896938964
return getFreshTypeOfLiteralType(getBigIntLiteralType({
@@ -47886,9 +47881,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4788647881
if (enumResult) return enumResult;
4788747882
const literalValue = (type as LiteralType).value;
4788847883
return typeof literalValue === "object" ? factory.createBigIntLiteral(literalValue) :
47889-
typeof literalValue === "string" ? factory.createStringLiteral(literalValue) :
47890-
literalValue < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(literalValue))) :
47891-
factory.createNumericLiteral(literalValue);
47884+
typeof literalValue === "number" ? factory.createNumericLiteral(literalValue) :
47885+
factory.createStringLiteral(literalValue);
4789247886
}
4789347887

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

src/compiler/factory/nodeFactory.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import {
4444
CaseOrDefaultClause,
4545
cast,
4646
CatchClause,
47-
CharacterCodes,
4847
ClassDeclaration,
4948
ClassElement,
5049
ClassExpression,
@@ -1254,10 +1253,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
12541253

12551254
// @api
12561255
function createNumericLiteral(value: string | number, numericLiteralFlags: TokenFlags = TokenFlags.None): NumericLiteral {
1257-
const text = typeof value === "number" ? value + "" : value;
1258-
Debug.assert(text.charCodeAt(0) !== CharacterCodes.minus, "Negative numbers should be created in combination with createPrefixUnaryExpression");
12591256
const node = createBaseDeclaration<NumericLiteral>(SyntaxKind.NumericLiteral);
1260-
node.text = text;
1257+
node.text = typeof value === "number" ? value + "" : value;
12611258
node.numericLiteralFlags = numericLiteralFlags;
12621259
if (numericLiteralFlags & TokenFlags.BinaryOrOctalSpecifier) node.transformFlags |= TransformFlags.ContainsES2015;
12631260
return node;

src/compiler/transformers/declarations.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -1798,14 +1798,7 @@ 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-
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);
1801+
return preserveJsDoc(factory.updateEnumMember(m, m.name, constValue !== undefined ? typeof constValue === "string" ? factory.createStringLiteral(constValue) : factory.createNumericLiteral(constValue) : undefined), m);
18091802
})),
18101803
));
18111804
}

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(Number.MAX_SAFE_INTEGER);
2527+
const expression = factory.createNumericLiteral(-1);
25282528
if (labelExpressions[label] === undefined) {
25292529
labelExpressions[label] = [expression];
25302530
}

src/compiler/transformers/ts.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1901,9 +1901,7 @@ 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) :
1905-
value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value))) :
1906-
factory.createNumericLiteral(value);
1904+
return typeof value === "string" ? factory.createStringLiteral(value) : factory.createNumericLiteral(value);
19071905
}
19081906
else {
19091907
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 : number
18+
>0B111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101001010100000010111110001111111111 : Infinity
1919

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

0 commit comments

Comments
 (0)