esprima icon indicating copy to clipboard operation
esprima copied to clipboard

ES2019 Feature: Numeric Separator

Open JosephPecoraro opened this issue 6 years ago • 2 comments

Syntax:

New NumericLiteral syntax that allows for an _ separator in numbers.

let fee = 123_00;   // $123 (12300 cents, apparently)
let fee = 12_300;   // $12,300 (woah, that fee!)
let value = 1_000_000_000;

Grammar changes are to NumericLiteral, allowing the _ separator to show up in numbers.

+NumericLiteralSeparator::
+    _

 DecimalIntegerLiteral::
     0
-    NonZeroDigit DecimalDigits?
+    NonZeroDigit
+    NonZeroDigit NumericLiteralSeparator? DecimalDigits

 DecimalDigits::
     DecimalDigit
-    DecimalDigits DecimalDigit
+    DecimalDigits NumericLiteralSeparator? DecimalDigit

 BinaryDigits::
     BinaryDigit
-    BinaryDigits BinaryDigit
+    BinaryDigits NumericLiteralSeparator? BinaryDigit

 OctalDigits::
     OctalDigit
-    OctalDigits OctalDigit
+    OctalDigits NumericLiteralSeparator? OctalDigit

 HexDigits::
     HexDigit
-    HexDigits HexDigit
+    HexDigits NumericLiteralSeparator? HexDigit

Spec:

TC39: https://github.com/tc39/proposal-numeric-separator https://tc39.es/proposal-numeric-separator/

ESTree: No node changes proposed, numeric Literal node value will just be the value

Additional considerations

  • Supported in Chrome, Safari. (Firefox?)
  • Supported in Node, jsc
  • May have interaction with BigInt (https://github.com/jquery/esprima/issues/1988)

Remaining Tasks:

  • [ ] Update Scanner (scanner.ts changes for _ numeric separator)
  • [ ] Provide Unit Tests and Coverage
  • [ ] Update test262 (~test262.git#2ee3864136) / test262-stream (~1.3.0)

Test Cases

Valid:

1_2_3_4
1_000_000
10000_00
1_000000_0_0_0
1_2.3_4e5_6
1_1.2_2
1_1e2_2
0x1_0
0o1_0
0b1_0

Invalid:

1__1
1.2__2
1e2__2
1_
1_.2
1_e2
1._2
1.2_
1e_2
1e2_
0x_1
0x1__1
0o_1
0o1__1
0b_1
0b1__1
001_2   // Leading Zero (Legacy Hex) does not allow
09_1    // Leading Zero does not allow

JosephPecoraro avatar Aug 15 '19 01:08 JosephPecoraro

This is actually ES2021 feature instead of ES2019. See https://github.com/tc39/proposals/blob/master/finished-proposals.md

ljqx avatar Dec 06 '20 14:12 ljqx

I've created a pull for this in my fork: https://github.com/node-projects/esprima-next/pull/12

jogibear9988 avatar Jun 21 '21 20:06 jogibear9988