Skip to content

Commit

Permalink
fix(41240): allow emitting numeric with underscored separators as-is …
Browse files Browse the repository at this point in the history
…in esnext target (#41435)
  • Loading branch information
a-tarasyuk committed Nov 24, 2020
1 parent e607533 commit d163ab6
Show file tree
Hide file tree
Showing 27 changed files with 278 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
18 changes: 14 additions & 4 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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

6 changes: 6 additions & 0 deletions tests/cases/compiler/numericUnderscoredSeparator.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d163ab6

Please sign in to comment.