Skip to content

Commit d163ab6

Browse files
authored
fix(41240): allow emitting numeric with underscored separators as-is in esnext target (#41435)
1 parent e607533 commit d163ab6

27 files changed

+278
-5
lines changed

src/compiler/emitter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4600,7 +4600,8 @@ namespace ts {
46004600

46014601
const flags = (neverAsciiEscape ? GetLiteralTextFlags.NeverAsciiEscape : 0)
46024602
| (jsxAttributeEscape ? GetLiteralTextFlags.JsxAttributeEscape : 0)
4603-
| (printerOptions.terminateUnterminatedLiterals ? GetLiteralTextFlags.TerminateUnterminatedLiterals : 0);
4603+
| (printerOptions.terminateUnterminatedLiterals ? GetLiteralTextFlags.TerminateUnterminatedLiterals : 0)
4604+
| (printerOptions.target && printerOptions.target === ScriptTarget.ESNext ? GetLiteralTextFlags.AllowNumericSeparator : 0);
46044605

46054606
return getLiteralText(node, currentSourceFile!, flags);
46064607
}

src/compiler/utilities.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,15 +616,13 @@ namespace ts {
616616
NeverAsciiEscape = 1 << 0,
617617
JsxAttributeEscape = 1 << 1,
618618
TerminateUnterminatedLiterals = 1 << 2,
619+
AllowNumericSeparator = 1 << 3
619620
}
620621

621622
export function getLiteralText(node: LiteralLikeNode, sourceFile: SourceFile, flags: GetLiteralTextFlags) {
622623
// If we don't need to downlevel and we can reach the original source text using
623624
// the node's parent reference, then simply get the text as it was originally written.
624-
if (!nodeIsSynthesized(node) && node.parent && !(flags & GetLiteralTextFlags.TerminateUnterminatedLiterals && node.isUnterminated) && !(
625-
(isNumericLiteral(node) && node.numericLiteralFlags & TokenFlags.ContainsSeparator) ||
626-
isBigIntLiteral(node)
627-
)) {
625+
if (canUseOriginalText(node, flags)) {
628626
return getSourceTextOfNodeFromSourceFile(sourceFile, node);
629627
}
630628

@@ -677,6 +675,18 @@ namespace ts {
677675
return Debug.fail(`Literal kind '${node.kind}' not accounted for.`);
678676
}
679677

678+
function canUseOriginalText(node: LiteralLikeNode, flags: GetLiteralTextFlags): boolean {
679+
if (nodeIsSynthesized(node) || !node.parent || (flags & GetLiteralTextFlags.TerminateUnterminatedLiterals && node.isUnterminated)) {
680+
return false;
681+
}
682+
683+
if (isNumericLiteral(node) && node.numericLiteralFlags & TokenFlags.ContainsSeparator) {
684+
return !!(flags & GetLiteralTextFlags.AllowNumericSeparator);
685+
}
686+
687+
return !isBigIntLiteral(node);
688+
}
689+
680690
export function getTextOfConstantValue(value: string | number) {
681691
return isString(value) ? '"' + escapeNonAsciiString(value) + '"' : "" + value;
682692
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [numericUnderscoredSeparator.ts]
2+
1_000_000_000_000
3+
0b1010_0001_1000_0101
4+
0b1010_0001_1000_0101
5+
0xA0_B0_C0
6+
7+
8+
//// [numericUnderscoredSeparator.js]
9+
1000000000000;
10+
41349;
11+
41349;
12+
10531008;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
No type information for this code.0b1010_0001_1000_0101
4+
No type information for this code.0b1010_0001_1000_0101
5+
No type information for this code.0xA0_B0_C0
6+
No type information for this code.
7+
No type information for this code.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
>1_000_000_000_000 : 1000000000000
4+
5+
0b1010_0001_1000_0101
6+
>0b1010_0001_1000_0101 : 41349
7+
8+
0b1010_0001_1000_0101
9+
>0b1010_0001_1000_0101 : 41349
10+
11+
0xA0_B0_C0
12+
>0xA0_B0_C0 : 10531008
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [numericUnderscoredSeparator.ts]
2+
1_000_000_000_000
3+
0b1010_0001_1000_0101
4+
0b1010_0001_1000_0101
5+
0xA0_B0_C0
6+
7+
8+
//// [numericUnderscoredSeparator.js]
9+
1000000000000;
10+
41349;
11+
41349;
12+
10531008;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
No type information for this code.0b1010_0001_1000_0101
4+
No type information for this code.0b1010_0001_1000_0101
5+
No type information for this code.0xA0_B0_C0
6+
No type information for this code.
7+
No type information for this code.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
>1_000_000_000_000 : 1000000000000
4+
5+
0b1010_0001_1000_0101
6+
>0b1010_0001_1000_0101 : 41349
7+
8+
0b1010_0001_1000_0101
9+
>0b1010_0001_1000_0101 : 41349
10+
11+
0xA0_B0_C0
12+
>0xA0_B0_C0 : 10531008
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [numericUnderscoredSeparator.ts]
2+
1_000_000_000_000
3+
0b1010_0001_1000_0101
4+
0b1010_0001_1000_0101
5+
0xA0_B0_C0
6+
7+
8+
//// [numericUnderscoredSeparator.js]
9+
1000000000000;
10+
41349;
11+
41349;
12+
10531008;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
No type information for this code.0b1010_0001_1000_0101
4+
No type information for this code.0b1010_0001_1000_0101
5+
No type information for this code.0xA0_B0_C0
6+
No type information for this code.
7+
No type information for this code.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
>1_000_000_000_000 : 1000000000000
4+
5+
0b1010_0001_1000_0101
6+
>0b1010_0001_1000_0101 : 41349
7+
8+
0b1010_0001_1000_0101
9+
>0b1010_0001_1000_0101 : 41349
10+
11+
0xA0_B0_C0
12+
>0xA0_B0_C0 : 10531008
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [numericUnderscoredSeparator.ts]
2+
1_000_000_000_000
3+
0b1010_0001_1000_0101
4+
0b1010_0001_1000_0101
5+
0xA0_B0_C0
6+
7+
8+
//// [numericUnderscoredSeparator.js]
9+
1000000000000;
10+
41349;
11+
41349;
12+
10531008;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
No type information for this code.0b1010_0001_1000_0101
4+
No type information for this code.0b1010_0001_1000_0101
5+
No type information for this code.0xA0_B0_C0
6+
No type information for this code.
7+
No type information for this code.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
>1_000_000_000_000 : 1000000000000
4+
5+
0b1010_0001_1000_0101
6+
>0b1010_0001_1000_0101 : 41349
7+
8+
0b1010_0001_1000_0101
9+
>0b1010_0001_1000_0101 : 41349
10+
11+
0xA0_B0_C0
12+
>0xA0_B0_C0 : 10531008
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [numericUnderscoredSeparator.ts]
2+
1_000_000_000_000
3+
0b1010_0001_1000_0101
4+
0b1010_0001_1000_0101
5+
0xA0_B0_C0
6+
7+
8+
//// [numericUnderscoredSeparator.js]
9+
1000000000000;
10+
41349;
11+
41349;
12+
10531008;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
No type information for this code.0b1010_0001_1000_0101
4+
No type information for this code.0b1010_0001_1000_0101
5+
No type information for this code.0xA0_B0_C0
6+
No type information for this code.
7+
No type information for this code.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
>1_000_000_000_000 : 1000000000000
4+
5+
0b1010_0001_1000_0101
6+
>0b1010_0001_1000_0101 : 41349
7+
8+
0b1010_0001_1000_0101
9+
>0b1010_0001_1000_0101 : 41349
10+
11+
0xA0_B0_C0
12+
>0xA0_B0_C0 : 10531008
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [numericUnderscoredSeparator.ts]
2+
1_000_000_000_000
3+
0b1010_0001_1000_0101
4+
0b1010_0001_1000_0101
5+
0xA0_B0_C0
6+
7+
8+
//// [numericUnderscoredSeparator.js]
9+
1000000000000;
10+
41349;
11+
41349;
12+
10531008;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
No type information for this code.0b1010_0001_1000_0101
4+
No type information for this code.0b1010_0001_1000_0101
5+
No type information for this code.0xA0_B0_C0
6+
No type information for this code.
7+
No type information for this code.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
>1_000_000_000_000 : 1000000000000
4+
5+
0b1010_0001_1000_0101
6+
>0b1010_0001_1000_0101 : 41349
7+
8+
0b1010_0001_1000_0101
9+
>0b1010_0001_1000_0101 : 41349
10+
11+
0xA0_B0_C0
12+
>0xA0_B0_C0 : 10531008
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [numericUnderscoredSeparator.ts]
2+
1_000_000_000_000
3+
0b1010_0001_1000_0101
4+
0b1010_0001_1000_0101
5+
0xA0_B0_C0
6+
7+
8+
//// [numericUnderscoredSeparator.js]
9+
1000000000000;
10+
41349;
11+
41349;
12+
10531008;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
No type information for this code.0b1010_0001_1000_0101
4+
No type information for this code.0b1010_0001_1000_0101
5+
No type information for this code.0xA0_B0_C0
6+
No type information for this code.
7+
No type information for this code.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
>1_000_000_000_000 : 1000000000000
4+
5+
0b1010_0001_1000_0101
6+
>0b1010_0001_1000_0101 : 41349
7+
8+
0b1010_0001_1000_0101
9+
>0b1010_0001_1000_0101 : 41349
10+
11+
0xA0_B0_C0
12+
>0xA0_B0_C0 : 10531008
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [numericUnderscoredSeparator.ts]
2+
1_000_000_000_000
3+
0b1010_0001_1000_0101
4+
0b1010_0001_1000_0101
5+
0xA0_B0_C0
6+
7+
8+
//// [numericUnderscoredSeparator.js]
9+
1_000_000_000_000;
10+
0b1010_0001_1000_0101;
11+
0b1010_0001_1000_0101;
12+
0xA0_B0_C0;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
No type information for this code.0b1010_0001_1000_0101
4+
No type information for this code.0b1010_0001_1000_0101
5+
No type information for this code.0xA0_B0_C0
6+
No type information for this code.
7+
No type information for this code.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/numericUnderscoredSeparator.ts ===
2+
1_000_000_000_000
3+
>1_000_000_000_000 : 1000000000000
4+
5+
0b1010_0001_1000_0101
6+
>0b1010_0001_1000_0101 : 41349
7+
8+
0b1010_0001_1000_0101
9+
>0b1010_0001_1000_0101 : 41349
10+
11+
0xA0_B0_C0
12+
>0xA0_B0_C0 : 10531008
13+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: es3,es5,es2015,es2016,es2017,es2018,es2019,esnext
2+
3+
1_000_000_000_000
4+
0b1010_0001_1000_0101
5+
0b1010_0001_1000_0101
6+
0xA0_B0_C0

0 commit comments

Comments
 (0)