jquery / esprima

ECMAScript parsing infrastructure for multipurpose analysis
http://esprima.org
BSD 2-Clause "Simplified" License
7.03k stars 783 forks source link

ES2019 Feature: Numeric Separator #1989

Open JosephPecoraro opened 5 years ago

JosephPecoraro commented 5 years ago

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

Remaining Tasks:

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
ljqx commented 3 years ago

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

jogibear9988 commented 3 years ago

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