diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 1ab45e37e4fb7..d66ad3bbb5797 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2996,9 +2996,12 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri if (isNumericLiteral(expression)) { // check if numeric literal is a decimal literal that was originally written with a dot const text = getLiteralTextOfNode(expression as LiteralExpression, /*neverAsciiEscape*/ true, /*jsxAttributeEscape*/ false); - // If the number will be printed verbatim and it doesn't already contain a dot, add one + // If the number will be printed verbatim and it doesn't already contain a dot or an exponent indicator, add one // if the expression doesn't have any comments that will be emitted. - return !(expression.numericLiteralFlags & ~TokenFlags.OctalOrContainsLeadingZero) && !stringContains(text, tokenToString(SyntaxKind.DotToken)!); + return !(expression.numericLiteralFlags & TokenFlags.WithSpecifier) + && !stringContains(text, tokenToString(SyntaxKind.DotToken)!) + && !stringContains(text, String.fromCharCode(CharacterCodes.E)) + && !stringContains(text, String.fromCharCode(CharacterCodes.e)); } else if (isAccessExpression(expression)) { // check if constant enum value is integer diff --git a/src/compiler/types.ts b/src/compiler/types.ts index ea341c2d3345a..1fb6ed23f6b0c 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2814,15 +2814,15 @@ export const enum TokenFlags { /** @internal */ BinaryOrOctalSpecifier = BinarySpecifier | OctalSpecifier, /** @internal */ - OctalOrContainsLeadingZero = Octal | ContainsLeadingZero, + WithSpecifier = HexSpecifier | BinaryOrOctalSpecifier, /** @internal */ StringLiteralFlags = HexEscape | UnicodeEscape | ExtendedUnicodeEscape | ContainsInvalidEscape, /** @internal */ - NumericLiteralFlags = Scientific | OctalOrContainsLeadingZero | HexSpecifier | BinaryOrOctalSpecifier | ContainsSeparator | ContainsInvalidSeparator, + NumericLiteralFlags = Scientific | Octal | ContainsLeadingZero | WithSpecifier | ContainsSeparator | ContainsInvalidSeparator, /** @internal */ TemplateLiteralLikeFlags = HexEscape | UnicodeEscape | ExtendedUnicodeEscape | ContainsInvalidEscape, /** @internal */ - IsInvalid = OctalOrContainsLeadingZero | ContainsInvalidSeparator | ContainsInvalidEscape, + IsInvalid = Octal | ContainsLeadingZero | ContainsInvalidSeparator | ContainsInvalidEscape, } export interface NumericLiteral extends LiteralExpression, Declaration { diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.errors.txt b/tests/baselines/reference/propertyAccessNumericLiterals.errors.txt index 510c905b8c5b0..c4312ee2de48d 100644 --- a/tests/baselines/reference/propertyAccessNumericLiterals.errors.txt +++ b/tests/baselines/reference/propertyAccessNumericLiterals.errors.txt @@ -1,7 +1,11 @@ tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts(6,1): error TS1121: Octal literals are not allowed. Use the syntax '0o0'. +tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts(7,1): error TS1486: Decimals with leading zeros are not allowed. +tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts(8,2): error TS6188: Numeric separators are not allowed here. +tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts(10,1): error TS1486: Decimals with leading zeros are not allowed. +tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts(11,3): error TS6188: Numeric separators are not allowed here. -==== tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts (1 errors) ==== +==== tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts (5 errors) ==== 0xffffffff.toString(); 0o01234.toString(); 0b01101101.toString(); @@ -9,4 +13,19 @@ tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals 1e0.toString(); 000.toString(); ~~~ -!!! error TS1121: Octal literals are not allowed. Use the syntax '0o0'. \ No newline at end of file +!!! error TS1121: Octal literals are not allowed. Use the syntax '0o0'. + 08.8e5.toString(); + ~~~~~~ +!!! error TS1486: Decimals with leading zeros are not allowed. + 0_8.8e5.toString(); + ~ +!!! error TS6188: Numeric separators are not allowed here. + 8.8e5.toString(); + 088e4.toString(); + ~~~~~ +!!! error TS1486: Decimals with leading zeros are not allowed. + 88_e4.toString(); + ~ +!!! error TS6188: Numeric separators are not allowed here. + 88e4.toString(); + 8_8e4.toString(); \ No newline at end of file diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.js b/tests/baselines/reference/propertyAccessNumericLiterals.js index 09910af6aac5b..c5b223fa7c42e 100644 --- a/tests/baselines/reference/propertyAccessNumericLiterals.js +++ b/tests/baselines/reference/propertyAccessNumericLiterals.js @@ -4,7 +4,14 @@ 0b01101101.toString(); 1234..toString(); 1e0.toString(); -000.toString(); +000.toString(); +08.8e5.toString(); +0_8.8e5.toString(); +8.8e5.toString(); +088e4.toString(); +88_e4.toString(); +88e4.toString(); +8_8e4.toString(); //// [propertyAccessNumericLiterals.js] 0xffffffff.toString(); @@ -13,3 +20,10 @@ 1234..toString(); 1e0.toString(); 0..toString(); +880000..toString(); +880000..toString(); +8.8e5.toString(); +880000..toString(); +880000..toString(); +88e4.toString(); +880000..toString(); diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.symbols b/tests/baselines/reference/propertyAccessNumericLiterals.symbols index 4f1d5e6fa220b..15001d9ee6269 100644 --- a/tests/baselines/reference/propertyAccessNumericLiterals.symbols +++ b/tests/baselines/reference/propertyAccessNumericLiterals.symbols @@ -23,3 +23,31 @@ >000.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) >toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +08.8e5.toString(); +>08.8e5.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +0_8.8e5.toString(); +>0_8.8e5.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +8.8e5.toString(); +>8.8e5.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +088e4.toString(); +>088e4.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +88_e4.toString(); +>88_e4.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +88e4.toString(); +>88e4.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + +8_8e4.toString(); +>8_8e4.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --)) + diff --git a/tests/baselines/reference/propertyAccessNumericLiterals.types b/tests/baselines/reference/propertyAccessNumericLiterals.types index a2567ff253716..0fd88ed1b634b 100644 --- a/tests/baselines/reference/propertyAccessNumericLiterals.types +++ b/tests/baselines/reference/propertyAccessNumericLiterals.types @@ -35,3 +35,45 @@ >000 : 0 >toString : (radix?: number) => string +08.8e5.toString(); +>08.8e5.toString() : string +>08.8e5.toString : (radix?: number) => string +>08.8e5 : 880000 +>toString : (radix?: number) => string + +0_8.8e5.toString(); +>0_8.8e5.toString() : string +>0_8.8e5.toString : (radix?: number) => string +>0_8.8e5 : 880000 +>toString : (radix?: number) => string + +8.8e5.toString(); +>8.8e5.toString() : string +>8.8e5.toString : (radix?: number) => string +>8.8e5 : 880000 +>toString : (radix?: number) => string + +088e4.toString(); +>088e4.toString() : string +>088e4.toString : (radix?: number) => string +>088e4 : 880000 +>toString : (radix?: number) => string + +88_e4.toString(); +>88_e4.toString() : string +>88_e4.toString : (radix?: number) => string +>88_e4 : 880000 +>toString : (radix?: number) => string + +88e4.toString(); +>88e4.toString() : string +>88e4.toString : (radix?: number) => string +>88e4 : 880000 +>toString : (radix?: number) => string + +8_8e4.toString(); +>8_8e4.toString() : string +>8_8e4.toString : (radix?: number) => string +>8_8e4 : 880000 +>toString : (radix?: number) => string + diff --git a/tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts b/tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts index 318b05fc0b50a..11feb1932b46d 100644 --- a/tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts +++ b/tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts @@ -1,7 +1,13 @@ -// @target: es3 0xffffffff.toString(); 0o01234.toString(); 0b01101101.toString(); 1234..toString(); 1e0.toString(); -000.toString(); \ No newline at end of file +000.toString(); +08.8e5.toString(); +0_8.8e5.toString(); +8.8e5.toString(); +088e4.toString(); +88_e4.toString(); +88e4.toString(); +8_8e4.toString(); \ No newline at end of file