fix(41240): allow emitting numeric with underscored separators as-is in esnext target (#41435)

This commit is contained in:
Oleksandr T 2020-11-24 03:01:42 +02:00 committed by GitHub
parent e60753339f
commit d163ab67c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 278 additions and 5 deletions

View file

@ -4600,7 +4600,8 @@ namespace ts {
const flags = (neverAsciiEscape ? GetLiteralTextFlags.NeverAsciiEscape : 0)
| (jsxAttributeEscape ? GetLiteralTextFlags.JsxAttributeEscape : 0)
| (printerOptions.terminateUnterminatedLiterals ? GetLiteralTextFlags.TerminateUnterminatedLiterals : 0);
| (printerOptions.terminateUnterminatedLiterals ? GetLiteralTextFlags.TerminateUnterminatedLiterals : 0)
| (printerOptions.target && printerOptions.target === ScriptTarget.ESNext ? GetLiteralTextFlags.AllowNumericSeparator : 0);
return getLiteralText(node, currentSourceFile!, flags);
}

View file

@ -616,15 +616,13 @@ namespace ts {
NeverAsciiEscape = 1 << 0,
JsxAttributeEscape = 1 << 1,
TerminateUnterminatedLiterals = 1 << 2,
AllowNumericSeparator = 1 << 3
}
export function getLiteralText(node: LiteralLikeNode, sourceFile: SourceFile, flags: GetLiteralTextFlags) {
// If we don't need to downlevel and we can reach the original source text using
// the node's parent reference, then simply get the text as it was originally written.
if (!nodeIsSynthesized(node) && node.parent && !(flags & GetLiteralTextFlags.TerminateUnterminatedLiterals && node.isUnterminated) && !(
(isNumericLiteral(node) && node.numericLiteralFlags & TokenFlags.ContainsSeparator) ||
isBigIntLiteral(node)
)) {
if (canUseOriginalText(node, flags)) {
return getSourceTextOfNodeFromSourceFile(sourceFile, node);
}
@ -677,6 +675,18 @@ namespace ts {
return Debug.fail(`Literal kind '${node.kind}' not accounted for.`);
}
function canUseOriginalText(node: LiteralLikeNode, flags: GetLiteralTextFlags): boolean {
if (nodeIsSynthesized(node) || !node.parent || (flags & GetLiteralTextFlags.TerminateUnterminatedLiterals && node.isUnterminated)) {
return false;
}
if (isNumericLiteral(node) && node.numericLiteralFlags & TokenFlags.ContainsSeparator) {
return !!(flags & GetLiteralTextFlags.AllowNumericSeparator);
}
return !isBigIntLiteral(node);
}
export function getTextOfConstantValue(value: string | number) {
return isString(value) ? '"' + escapeNonAsciiString(value) + '"' : "" + value;
}

View file

@ -0,0 +1,12 @@
//// [numericUnderscoredSeparator.ts]
1_000_000_000_000
0b1010_0001_1000_0101
0b1010_0001_1000_0101
0xA0_B0_C0
//// [numericUnderscoredSeparator.js]
1000000000000;
41349;
41349;
10531008;

View file

@ -0,0 +1,7 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0xA0_B0_C0
No type information for this code.
No type information for this code.

View file

@ -0,0 +1,13 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
>1_000_000_000_000 : 1000000000000
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0xA0_B0_C0
>0xA0_B0_C0 : 10531008

View file

@ -0,0 +1,12 @@
//// [numericUnderscoredSeparator.ts]
1_000_000_000_000
0b1010_0001_1000_0101
0b1010_0001_1000_0101
0xA0_B0_C0
//// [numericUnderscoredSeparator.js]
1000000000000;
41349;
41349;
10531008;

View file

@ -0,0 +1,7 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0xA0_B0_C0
No type information for this code.
No type information for this code.

View file

@ -0,0 +1,13 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
>1_000_000_000_000 : 1000000000000
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0xA0_B0_C0
>0xA0_B0_C0 : 10531008

View file

@ -0,0 +1,12 @@
//// [numericUnderscoredSeparator.ts]
1_000_000_000_000
0b1010_0001_1000_0101
0b1010_0001_1000_0101
0xA0_B0_C0
//// [numericUnderscoredSeparator.js]
1000000000000;
41349;
41349;
10531008;

View file

@ -0,0 +1,7 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0xA0_B0_C0
No type information for this code.
No type information for this code.

View file

@ -0,0 +1,13 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
>1_000_000_000_000 : 1000000000000
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0xA0_B0_C0
>0xA0_B0_C0 : 10531008

View file

@ -0,0 +1,12 @@
//// [numericUnderscoredSeparator.ts]
1_000_000_000_000
0b1010_0001_1000_0101
0b1010_0001_1000_0101
0xA0_B0_C0
//// [numericUnderscoredSeparator.js]
1000000000000;
41349;
41349;
10531008;

View file

@ -0,0 +1,7 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0xA0_B0_C0
No type information for this code.
No type information for this code.

View file

@ -0,0 +1,13 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
>1_000_000_000_000 : 1000000000000
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0xA0_B0_C0
>0xA0_B0_C0 : 10531008

View file

@ -0,0 +1,12 @@
//// [numericUnderscoredSeparator.ts]
1_000_000_000_000
0b1010_0001_1000_0101
0b1010_0001_1000_0101
0xA0_B0_C0
//// [numericUnderscoredSeparator.js]
1000000000000;
41349;
41349;
10531008;

View file

@ -0,0 +1,7 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0xA0_B0_C0
No type information for this code.
No type information for this code.

View file

@ -0,0 +1,13 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
>1_000_000_000_000 : 1000000000000
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0xA0_B0_C0
>0xA0_B0_C0 : 10531008

View file

@ -0,0 +1,12 @@
//// [numericUnderscoredSeparator.ts]
1_000_000_000_000
0b1010_0001_1000_0101
0b1010_0001_1000_0101
0xA0_B0_C0
//// [numericUnderscoredSeparator.js]
1000000000000;
41349;
41349;
10531008;

View file

@ -0,0 +1,7 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0xA0_B0_C0
No type information for this code.
No type information for this code.

View file

@ -0,0 +1,13 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
>1_000_000_000_000 : 1000000000000
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0xA0_B0_C0
>0xA0_B0_C0 : 10531008

View file

@ -0,0 +1,12 @@
//// [numericUnderscoredSeparator.ts]
1_000_000_000_000
0b1010_0001_1000_0101
0b1010_0001_1000_0101
0xA0_B0_C0
//// [numericUnderscoredSeparator.js]
1000000000000;
41349;
41349;
10531008;

View file

@ -0,0 +1,7 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0xA0_B0_C0
No type information for this code.
No type information for this code.

View file

@ -0,0 +1,13 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
>1_000_000_000_000 : 1000000000000
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0xA0_B0_C0
>0xA0_B0_C0 : 10531008

View file

@ -0,0 +1,12 @@
//// [numericUnderscoredSeparator.ts]
1_000_000_000_000
0b1010_0001_1000_0101
0b1010_0001_1000_0101
0xA0_B0_C0
//// [numericUnderscoredSeparator.js]
1_000_000_000_000;
0b1010_0001_1000_0101;
0b1010_0001_1000_0101;
0xA0_B0_C0;

View file

@ -0,0 +1,7 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0b1010_0001_1000_0101
No type information for this code.0xA0_B0_C0
No type information for this code.
No type information for this code.

View file

@ -0,0 +1,13 @@
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
1_000_000_000_000
>1_000_000_000_000 : 1000000000000
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0b1010_0001_1000_0101
>0b1010_0001_1000_0101 : 41349
0xA0_B0_C0
>0xA0_B0_C0 : 10531008

View file

@ -0,0 +1,6 @@
// @target: es3,es5,es2015,es2016,es2017,es2018,es2019,esnext
1_000_000_000_000
0b1010_0001_1000_0101
0b1010_0001_1000_0101
0xA0_B0_C0