Revert "Disallow numeric literals with negative numbers (#55268)" (#56422)

This commit is contained in:
Jake Bailey
2023-11-15 15:51:47 -08:00
committed by GitHub
parent a6367977d4
commit 3333e906dd
22 changed files with 566 additions and 31 deletions
+5 -11
View File
@@ -8328,7 +8328,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return factory.createStringLiteral(name, !!singleQuote);
}
if (isNumericLiteralName(name) && startsWith(name, "-")) {
return factory.createComputedPropertyName(factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(+name))));
return factory.createComputedPropertyName(factory.createNumericLiteral(+name));
}
return createPropertyNameNodeForIdentifierOrLiteral(name, getEmitScriptTarget(compilerOptions), singleQuote, stringNamed, isMethod);
}
@@ -39006,14 +39006,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return hasSkipDirectInferenceFlag(node) ?
blockedStringType :
getFreshTypeOfLiteralType(getStringLiteralType((node as StringLiteralLike).text));
case SyntaxKind.NumericLiteral: {
case SyntaxKind.NumericLiteral:
checkGrammarNumericLiteral(node as NumericLiteral);
const value = +(node as NumericLiteral).text;
if (!isFinite(value)) {
return numberType;
}
return getFreshTypeOfLiteralType(getNumberLiteralType(value));
}
return getFreshTypeOfLiteralType(getNumberLiteralType(+(node as NumericLiteral).text));
case SyntaxKind.BigIntLiteral:
checkGrammarBigIntLiteral(node as BigIntLiteral);
return getFreshTypeOfLiteralType(getBigIntLiteralType({
@@ -47949,9 +47944,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (enumResult) return enumResult;
const literalValue = (type as LiteralType).value;
return typeof literalValue === "object" ? factory.createBigIntLiteral(literalValue) :
typeof literalValue === "string" ? factory.createStringLiteral(literalValue) :
literalValue < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(literalValue))) :
factory.createNumericLiteral(literalValue);
typeof literalValue === "number" ? factory.createNumericLiteral(literalValue) :
factory.createStringLiteral(literalValue);
}
function createLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration, tracker: SymbolTracker) {
+1 -4
View File
@@ -44,7 +44,6 @@ import {
CaseOrDefaultClause,
cast,
CatchClause,
CharacterCodes,
ClassDeclaration,
ClassElement,
ClassExpression,
@@ -1254,10 +1253,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
// @api
function createNumericLiteral(value: string | number, numericLiteralFlags: TokenFlags = TokenFlags.None): NumericLiteral {
const text = typeof value === "number" ? value + "" : value;
Debug.assert(text.charCodeAt(0) !== CharacterCodes.minus, "Negative numbers should be created in combination with createPrefixUnaryExpression");
const node = createBaseDeclaration<NumericLiteral>(SyntaxKind.NumericLiteral);
node.text = text;
node.text = typeof value === "number" ? value + "" : value;
node.numericLiteralFlags = numericLiteralFlags;
if (numericLiteralFlags & TokenFlags.BinaryOrOctalSpecifier) node.transformFlags |= TransformFlags.ContainsES2015;
return node;
+1 -8
View File
@@ -1798,14 +1798,7 @@ export function transformDeclarations(context: TransformationContext) {
if (shouldStripInternal(m)) return;
// Rewrite enum values to their constants, if available
const constValue = resolver.getConstantValue(m);
const newInitializer = constValue === undefined
? undefined
: typeof constValue === "string"
? factory.createStringLiteral(constValue)
: constValue < 0
? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(Math.abs(constValue)))
: factory.createNumericLiteral(constValue);
return preserveJsDoc(factory.updateEnumMember(m, m.name, newInitializer), m);
return preserveJsDoc(factory.updateEnumMember(m, m.name, constValue !== undefined ? typeof constValue === "string" ? factory.createStringLiteral(constValue) : factory.createNumericLiteral(constValue) : undefined), m);
})),
));
}
+1 -1
View File
@@ -2524,7 +2524,7 @@ export function transformGenerators(context: TransformationContext): (x: SourceF
labelExpressions = [];
}
const expression = factory.createNumericLiteral(Number.MAX_SAFE_INTEGER);
const expression = factory.createNumericLiteral(-1);
if (labelExpressions[label] === undefined) {
labelExpressions[label] = [expression];
}
+1 -3
View File
@@ -1901,9 +1901,7 @@ 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);
return typeof value === "string" ? factory.createStringLiteral(value) : factory.createNumericLiteral(value);
}
else {
enableSubstitutionForNonQualifiedEnumMembers();