Skip to content

Commit 6ca9df4

Browse files
authored
Accept bigints in t.bigIntLiteral factory (#17378)
* breaking: store BigIntLiteral#value as bigint * update babel-types test cases * update babel-parser test fixtures * feat: support bigint and deprecate string in the bigIntLiteral builder * make node 8 happy * update NodePart typing * address review comments
1 parent f349546 commit 6ca9df4

File tree

86 files changed

+1020
-72
lines changed

Some content is hidden

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

86 files changed

+1020
-72
lines changed

packages/babel-parser/ast/spec.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,10 @@ interface NumericLiteral <: Literal {
251251
```js
252252
interface BigIntLiteral <: Literal {
253253
type: "BigIntLiteral";
254-
value: string;
254+
value: bigint;
255255
}
256256
```
257257

258-
The `value` property is the string representation of the `BigInt` value. It doesn't include the suffix `n`.
259-
260258
## DecimalLiteral
261259

262260
```js

packages/babel-parser/src/parser/expression.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,21 @@ export default abstract class ExpressionParser extends LValParser {
17021702
}
17031703

17041704
parseBigIntLiteral(value: any) {
1705-
return this.parseLiteral<N.BigIntLiteral>(value, "BigIntLiteral");
1705+
if (process.env.BABEL_8_BREAKING) {
1706+
let bigInt: bigint | null;
1707+
try {
1708+
bigInt = BigInt(value);
1709+
} catch {
1710+
// parser supports invalid bigints like `1.0n` or `1e1n` such that it
1711+
// can throw a recoverable error, but BigInt constructor does not
1712+
// support them.
1713+
bigInt = null;
1714+
}
1715+
const node = this.parseLiteral<N.BigIntLiteral>(bigInt, "BigIntLiteral");
1716+
return node;
1717+
} else {
1718+
return this.parseLiteral<N.BigIntLiteral>(value, "BigIntLiteral");
1719+
}
17061720
}
17071721

17081722
// TODO: Remove this in Babel 8

packages/babel-parser/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ export interface NumericLiteral extends NodeBase {
255255

256256
export interface BigIntLiteral extends NodeBase {
257257
type: "BigIntLiteral";
258-
value: number;
258+
value: bigint;
259259
}
260260

261261
export interface DecimalLiteral extends NodeBase {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
({ 0n: 0, 1n() {}, get 2n(){}, set 3n(_){}, async 4n() {}, *5n() {} });
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": false
3+
}

packages/babel-parser/test/fixtures/es2020/bigint/decimal-as-property-name/output.json renamed to packages/babel-parser/test/fixtures/es2020/bigint/decimal-as-property-name-babel-7/output.json

File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": true
3+
}
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":71,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":71,"index":71}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":71,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":71,"index":71}},
7+
"sourceType": "script",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "ExpressionStatement",
12+
"start":0,"end":71,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":71,"index":71}},
13+
"expression": {
14+
"type": "ObjectExpression",
15+
"start":1,"end":69,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":69,"index":69}},
16+
"properties": [
17+
{
18+
"type": "ObjectProperty",
19+
"start":3,"end":8,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":8,"index":8}},
20+
"method": false,
21+
"key": {
22+
"type": "BigIntLiteral",
23+
"start":3,"end":5,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":5,"index":5}},
24+
"extra": {
25+
"rawValue": {
26+
"$$ babel internal serialized type": "bigint",
27+
"value": "0"
28+
},
29+
"raw": "0n"
30+
},
31+
"value": {
32+
"$$ babel internal serialized type": "bigint",
33+
"value": "0"
34+
}
35+
},
36+
"computed": false,
37+
"shorthand": false,
38+
"value": {
39+
"type": "NumericLiteral",
40+
"start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8}},
41+
"extra": {
42+
"rawValue": 0,
43+
"raw": "0"
44+
},
45+
"value": 0
46+
}
47+
},
48+
{
49+
"type": "ObjectMethod",
50+
"start":10,"end":17,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":17,"index":17}},
51+
"method": true,
52+
"key": {
53+
"type": "BigIntLiteral",
54+
"start":10,"end":12,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":12,"index":12}},
55+
"extra": {
56+
"rawValue": {
57+
"$$ babel internal serialized type": "bigint",
58+
"value": "1"
59+
},
60+
"raw": "1n"
61+
},
62+
"value": {
63+
"$$ babel internal serialized type": "bigint",
64+
"value": "1"
65+
}
66+
},
67+
"computed": false,
68+
"kind": "method",
69+
"id": null,
70+
"generator": false,
71+
"async": false,
72+
"params": [],
73+
"body": {
74+
"type": "BlockStatement",
75+
"start":15,"end":17,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":17,"index":17}},
76+
"body": [],
77+
"directives": []
78+
}
79+
},
80+
{
81+
"type": "ObjectMethod",
82+
"start":19,"end":29,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":29,"index":29}},
83+
"method": false,
84+
"key": {
85+
"type": "BigIntLiteral",
86+
"start":23,"end":25,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":25,"index":25}},
87+
"extra": {
88+
"rawValue": {
89+
"$$ babel internal serialized type": "bigint",
90+
"value": "2"
91+
},
92+
"raw": "2n"
93+
},
94+
"value": {
95+
"$$ babel internal serialized type": "bigint",
96+
"value": "2"
97+
}
98+
},
99+
"computed": false,
100+
"kind": "get",
101+
"id": null,
102+
"generator": false,
103+
"async": false,
104+
"params": [],
105+
"body": {
106+
"type": "BlockStatement",
107+
"start":27,"end":29,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":29,"index":29}},
108+
"body": [],
109+
"directives": []
110+
}
111+
},
112+
{
113+
"type": "ObjectMethod",
114+
"start":31,"end":42,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":42,"index":42}},
115+
"method": false,
116+
"key": {
117+
"type": "BigIntLiteral",
118+
"start":35,"end":37,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":37,"index":37}},
119+
"extra": {
120+
"rawValue": {
121+
"$$ babel internal serialized type": "bigint",
122+
"value": "3"
123+
},
124+
"raw": "3n"
125+
},
126+
"value": {
127+
"$$ babel internal serialized type": "bigint",
128+
"value": "3"
129+
}
130+
},
131+
"computed": false,
132+
"kind": "set",
133+
"id": null,
134+
"generator": false,
135+
"async": false,
136+
"params": [
137+
{
138+
"type": "Identifier",
139+
"start":38,"end":39,"loc":{"start":{"line":1,"column":38,"index":38},"end":{"line":1,"column":39,"index":39},"identifierName":"_"},
140+
"name": "_"
141+
}
142+
],
143+
"body": {
144+
"type": "BlockStatement",
145+
"start":40,"end":42,"loc":{"start":{"line":1,"column":40,"index":40},"end":{"line":1,"column":42,"index":42}},
146+
"body": [],
147+
"directives": []
148+
}
149+
},
150+
{
151+
"type": "ObjectMethod",
152+
"start":44,"end":57,"loc":{"start":{"line":1,"column":44,"index":44},"end":{"line":1,"column":57,"index":57}},
153+
"method": true,
154+
"key": {
155+
"type": "BigIntLiteral",
156+
"start":50,"end":52,"loc":{"start":{"line":1,"column":50,"index":50},"end":{"line":1,"column":52,"index":52}},
157+
"extra": {
158+
"rawValue": {
159+
"$$ babel internal serialized type": "bigint",
160+
"value": "4"
161+
},
162+
"raw": "4n"
163+
},
164+
"value": {
165+
"$$ babel internal serialized type": "bigint",
166+
"value": "4"
167+
}
168+
},
169+
"computed": false,
170+
"kind": "method",
171+
"id": null,
172+
"generator": false,
173+
"async": true,
174+
"params": [],
175+
"body": {
176+
"type": "BlockStatement",
177+
"start":55,"end":57,"loc":{"start":{"line":1,"column":55,"index":55},"end":{"line":1,"column":57,"index":57}},
178+
"body": [],
179+
"directives": []
180+
}
181+
},
182+
{
183+
"type": "ObjectMethod",
184+
"start":59,"end":67,"loc":{"start":{"line":1,"column":59,"index":59},"end":{"line":1,"column":67,"index":67}},
185+
"method": true,
186+
"key": {
187+
"type": "BigIntLiteral",
188+
"start":60,"end":62,"loc":{"start":{"line":1,"column":60,"index":60},"end":{"line":1,"column":62,"index":62}},
189+
"extra": {
190+
"rawValue": {
191+
"$$ babel internal serialized type": "bigint",
192+
"value": "5"
193+
},
194+
"raw": "5n"
195+
},
196+
"value": {
197+
"$$ babel internal serialized type": "bigint",
198+
"value": "5"
199+
}
200+
},
201+
"computed": false,
202+
"kind": "method",
203+
"id": null,
204+
"generator": true,
205+
"async": false,
206+
"params": [],
207+
"body": {
208+
"type": "BlockStatement",
209+
"start":65,"end":67,"loc":{"start":{"line":1,"column":65,"index":65},"end":{"line":1,"column":67,"index":67}},
210+
"body": [],
211+
"directives": []
212+
}
213+
}
214+
],
215+
"extra": {
216+
"parenthesized": true,
217+
"parenStart": 0
218+
}
219+
}
220+
}
221+
],
222+
"directives": []
223+
}
224+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
({ 0xbeefban: 0 });
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": false
3+
}

0 commit comments

Comments
 (0)