8000 Add decimal parsing support (#11640) · babel/babel@059e912 · GitHub
[go: up one dir, main page]

Skip to content

Commit 059e912

Browse files
authored
Add decimal parsing support (#11640)
* docs: add DecimalLiteral to AST spec * add decimal support * fix: throw invalid decimal on start * add DecimalLiteral type definitions * update parser typings * add generator support * add syntax-decimal plugin * Add syntax-decimal to babel-standalone * add syntax-decimal to missing plugin helpers * fix incorrect test macro
1 parent 9daa50e commit 059e912

File tree

56 files changed

+655
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+655
-4
lines changed

packages/babel-core/src/parser/util/missing-plugin-helper.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ const pluginNameMap = {
3131
url: "https://git.io/JvpRG",
3232
},
3333
},
34+
decimal: {
35+
syntax: {
36+
name: "@babel/plugin-syntax-decimal",
37+
url: "https://git.io/JfKOH",
38+
},
39+
},
3440
decorators: {
3541
syntax: {
3642
name: "@babel/plugin-syntax-decorators",

packages/babel-generator/src/generators/types.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ export function BigIntLiteral(node: Object) {
222222
this.token(node.value + "n");
223223
}
224224

225+
export function DecimalLiteral(node: Object) {
226+
const raw = this.getPossibleRaw(node);
227+
if (!this.format.minified && raw != null) {
228+
this.token(raw);
229+
return;
230+
}
231+
this.token(node.value + "m");
232+
}
233+
225234
export function PipelineTopicExpression(node: Object) {
226235
this.print(node.expression, node);
227236
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
100m;
2+
9223372036854775807m;
3+
0.m;
4+
3.1415926535897932m;
5+
100.000m;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": ["decimal"],
3+
"minified": true
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
100m;9223372036854775807m;0.m;3.1415926535897932m;100.000m;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
100m;
2+
9223372036854775807m;
3+
0.m;
4+
3.1415926535897932m;
5+
100.000m;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["decimal"]
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
100m;
2+
9223372036854775807m;
3+
0.m;
4+
3.1415926535897932m;
5+
100.000m;

packages/babel-parser/ast/spec.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ These are the core @babel/parser (babylon) AST node types.
1111
- [BooleanLiteral](#booleanliteral)
1212
- [NumericLiteral](#numericliteral)
1313
- [BigIntLiteral](#bigintliteral)
14+
- [DecimalLiteral](#decimalliteral)
1415
- [Programs](#programs)
1516
- [Functions](#functions)
1617
- [Statements](#statements)
@@ -253,6 +254,17 @@ interface BigIntLiteral <: Literal {
253254

254255
The `value` property is the string representation of the `BigInt` value. It doesn't include the suffix `n`.
255256

257+
## DecimalLiteral
258+
259+
```js
260+
interface DecimalLiteral <: Literal {
261+
type: "DecimalLiteral";
262+
value: string;
263+
}
264+
```
265+
266+
The `value` property is the string representation of the `BigDecimal` value. It doesn't include the suffix `m`.
267+
256268
# Programs
257269

258270
```js

packages/babel-parser/src/parser/error-message.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const ErrorMessages = Object.freeze({
6161
ImportOutsideModule: `'import' and 'export' may appear only with 'sourceType: "module"'`,
6262
InvalidBigIntLiteral: "Invalid BigIntLiteral",
6363
InvalidCodePoint: "Code point out of bounds",
64+
InvalidDecimal: "Invalid decimal",
6465
InvalidDigit: "Expected number in radix %0",
6566
InvalidEscapeSequence: "Bad character escape sequence",
6667
InvalidEscapeSequenceTemplate: "Invalid escape sequence in template",

0 commit comments

Comments
 (0)
0