Skip to content

Commit f0c3c3f

Browse files
authored
Disallow numeric literals with negative numbers (microsoft#55268)
1 parent c395d17 commit f0c3c3f

File tree

9 files changed

+31
-13
lines changed

9 files changed

+31
-13
lines changed

src/compiler/checker.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -8111,7 +8111,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
81118111
return factory.createStringLiteral(name, !!singleQuote);
81128112
}
81138113
if (isNumericLiteralName(name) && startsWith(name, "-")) {
8114-
return factory.createComputedPropertyName(factory.createNumericLiteral(+name));
8114+
return factory.createComputedPropertyName(factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(+name))));
81158115
}
81168116
return createPropertyNameNodeForIdentifierOrLiteral(name, getEmitScriptTarget(compilerOptions));
81178117
}
@@ -38006,9 +38006,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3800638006
return hasSkipDirectInferenceFlag(node) ?
3800738007
wildcardType :
3800838008
getFreshTypeOfLiteralType(getStringLiteralType((node as StringLiteralLike).text));
38009-
case SyntaxKind.NumericLiteral:
38009+
case SyntaxKind.NumericLiteral: {
3801038010
checkGrammarNumericLiteral(node as NumericLiteral);
38011-
return getFreshTypeOfLiteralType(getNumberLiteralType(+(node as NumericLiteral).text));
38011+
const value = +(node as NumericLiteral).text;
38012+
if (!isFinite(value)) {
38013+
return numberType;
38014+
}
38015+
return getFreshTypeOfLiteralType(getNumberLiteralType(value));
38016+
}
3801238017
case SyntaxKind.BigIntLiteral:
3801338018
checkGrammarBigIntLiteral(node as BigIntLiteral);
3801438019
return getFreshTypeOfLiteralType(getBigIntLiteralType({
@@ -46782,8 +46787,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4678246787
if (enumResult) return enumResult;
4678346788
const literalValue = (type as LiteralType).value;
4678446789
return typeof literalValue === "object" ? factory.createBigIntLiteral(literalValue) :
46785-
typeof literalValue === "number" ? factory.createNumericLiteral(literalValue) :
46786-
factory.createStringLiteral(literalValue);
46790+
typeof literalValue === "string" ? factory.createStringLiteral(literalValue) :
46791+
literalValue < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(literalValue))) :
46792+
factory.createNumericLiteral(literalValue);
4678746793
}
4678846794

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

src/compiler/factory/nodeFactory.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
CaseOrDefaultClause,
4444
cast,
4545
CatchClause,
46+
CharacterCodes,
4647
ClassDeclaration,
4748
ClassElement,
4849
ClassExpression,
@@ -1100,8 +1101,10 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
11001101

11011102
// @api
11021103
function createNumericLiteral(value: string | number, numericLiteralFlags: TokenFlags = TokenFlags.None): NumericLiteral {
1104+
const text = typeof value === "number" ? value + "" : value;
1105+
Debug.assert(text.charCodeAt(0) !== CharacterCodes.minus, "Negative numbers should be created in combination with createPrefixUnaryExpression");
11031106
const node = createBaseDeclaration<NumericLiteral>(SyntaxKind.NumericLiteral);
1104-
node.text = typeof value === "number" ? value + "" : value;
1107+
node.text = text;
11051108
node.numericLiteralFlags = numericLiteralFlags;
11061109
if (numericLiteralFlags & TokenFlags.BinaryOrOctalSpecifier) node.transformFlags |= TransformFlags.ContainsES2015;
11071110
return node;

src/compiler/transformers/declarations.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,14 @@ export function transformDeclarations(context: TransformationContext) {
17651765
if (shouldStripInternal(m)) return;
17661766
// Rewrite enum values to their constants, if available
17671767
const constValue = resolver.getConstantValue(m);
1768-
return preserveJsDoc(factory.updateEnumMember(m, m.name, constValue !== undefined ? typeof constValue === "string" ? factory.createStringLiteral(constValue) : factory.createNumericLiteral(constValue) : undefined), m);
1768+
const newInitializer = constValue === undefined
1769+
? undefined
1770+
: typeof constValue === "string"
1771+
? factory.createStringLiteral(constValue)
1772+
: constValue < 0
1773+
? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(constValue)))
1774+
: factory.createNumericLiteral(constValue);
1775+
return preserveJsDoc(factory.updateEnumMember(m, m.name, newInitializer), m);
17691776
}))));
17701777
}
17711778
}

src/compiler/transformers/generators.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2528,7 +2528,7 @@ export function transformGenerators(context: TransformationContext): (x: SourceF
25282528
labelExpressions = [];
25292529
}
25302530

2531-
const expression = factory.createNumericLiteral(-1);
2531+
const expression = factory.createNumericLiteral(Number.MAX_SAFE_INTEGER);
25322532
if (labelExpressions[label] === undefined) {
25332533
labelExpressions[label] = [expression];
25342534
}

src/compiler/transformers/ts.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,9 @@ export function transformTypeScript(context: TransformationContext) {
18881888
function transformEnumMemberDeclarationValue(member: EnumMember): Expression {
18891889
const value = resolver.getConstantValue(member);
18901890
if (value !== undefined) {
1891-
return typeof value === "string" ? factory.createStringLiteral(value) : factory.createNumericLiteral(value);
1891+
return typeof value === "string" ? factory.createStringLiteral(value) :
1892+
value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(value))) :
1893+
factory.createNumericLiteral(value);
18921894
}
18931895
else {
18941896
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)