Skip to content

Commit 66f1627

Browse files
authored
fix(typescript-estree): handle BigInt with _ numeric separator (typescript-eslint#2067)
1 parent 765ec4b commit 66f1627

File tree

6 files changed

+158
-1
lines changed

6 files changed

+158
-1
lines changed

packages/parser/tests/lib/__snapshots__/javascript.ts.snap

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6815,6 +6815,56 @@ Object {
68156815
}
68166816
`;
68176817

6818+
exports[`javascript fixtures/bigIntLiterals/numeric-separator.src 1`] = `
6819+
Object {
6820+
"$id": 1,
6821+
"block": Object {
6822+
"range": Array [
6823+
0,
6824+
8,
6825+
],
6826+
"type": "Program",
6827+
},
6828+
"childScopes": Array [
6829+
Object {
6830+
"$id": 0,
6831+
"block": Object {
6832+
"range": Array [
6833+
0,
6834+
8,
6835+
],
6836+
"type": "Program",
6837+
},
6838+
"childScopes": Array [],
6839+
"functionExpressionScope": false,
6840+
"isStrict": true,
6841+
"references": Array [],
6842+
"throughReferences": Array [],
6843+
"type": "module",
6844+
"upperScope": Object {
6845+
"$ref": 1,
6846+
},
6847+
"variableMap": Object {},
6848+
"variableScope": Object {
6849+
"$ref": 0,
6850+
},
6851+
"variables": Array [],
6852+
},
6853+
],
6854+
"functionExpressionScope": false,
6855+
"isStrict": false,
6856+
"references": Array [],
6857+
"throughReferences": Array [],
6858+
"type": "global",
6859+
"upperScope": null,
6860+
"variableMap": Object {},
6861+
"variableScope": Object {
6862+
"$ref": 1,
6863+
},
6864+
"variables": Array [],
6865+
}
6866+
`;
6867+
68186868
exports[`javascript fixtures/bigIntLiterals/octal.src 1`] = `
68196869
Object {
68206870
"$id": 1,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1_2_3n;

packages/typescript-estree/src/convert.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1931,7 +1931,12 @@ export class Converter {
19311931
case SyntaxKind.BigIntLiteral: {
19321932
const range = getRange(node, this.ast);
19331933
const rawValue = this.ast.text.slice(range[0], range[1]);
1934-
const bigint = rawValue.slice(0, -1); // remove suffix `n`
1934+
const bigint = rawValue
1935+
// remove suffix `n`
1936+
.slice(0, -1)
1937+
// `BigInt` doesn't accept numeric separator
1938+
// and `bigint` property should not include numeric separator
1939+
.replace(/_/g, '');
19351940
const value = typeof BigInt !== 'undefined' ? BigInt(bigint) : null;
19361941
return this.createNode<TSESTree.BigIntLiteral>(node, {
19371942
type: AST_NODE_TYPES.Literal,

packages/typescript-estree/tests/ast-alignment/parse.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function parseWithBabelParser(text: string, jsx = true): any {
2929
'dynamicImport',
3030
'estree',
3131
'bigInt',
32+
'numericSeparator',
3233
'importMeta',
3334
'optionalChaining',
3435
'nullishCoalescingOperator',

packages/typescript-estree/tests/lib/__snapshots__/javascript.ts.snap

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16780,6 +16780,104 @@ Object {
1678016780
}
1678116781
`;
1678216782

16783+
exports[`javascript fixtures/bigIntLiterals/numeric-separator.src 1`] = `
16784+
Object {
16785+
"body": Array [
16786+
Object {
16787+
"expression": Object {
16788+
"bigint": "123",
16789+
"loc": Object {
16790+
"end": Object {
16791+
"column": 6,
16792+
"line": 1,
16793+
},
16794+
"start": Object {
16795+
"column": 0,
16796+
"line": 1,
16797+
},
16798+
},
16799+
"range": Array [
16800+
0,
16801+
6,
16802+
],
16803+
"raw": "1_2_3n",
16804+
"type": "Literal",
16805+
"value": 123n,
16806+
},
16807+
"loc": Object {
16808+
"end": Object {
16809+
"column": 7,
16810+
"line": 1,
16811+
},
16812+
"start": Object {
16813+
"column": 0,
16814+
"line": 1,
16815+
},
16816+
},
16817+
"range": Array [
16818+
0,
16819+
7,
16820+
],
16821+
"type": "ExpressionStatement",
16822+
},
16823+
],
16824+
"loc": Object {
16825+
"end": Object {
16826+
"column": 0,
16827+
"line": 2,
16828+
},
16829+
"start": Object {
16830+
"column": 0,
16831+
"line": 1,
16832+
},
16833+
},
16834+
"range": Array [
16835+
0,
16836+
8,
16837+
],
16838+
"sourceType": "script",
16839+
"tokens": Array [
16840+
Object {
16841+
"loc": Object {
16842+
"end": Object {
16843+
"column": 6,
16844+
"line": 1,
16845+
},
16846+
"start": Object {
16847+
"column": 0,
16848+
"line": 1,
16849+
},
16850+
},
16851+
"range": Array [
16852+
0,
16853+
6,
16854+
],
16855+
"type": "Identifier",
16856+
"value": "1_2_3n",
16857+
},
16858+
Object {
16859+
"loc": Object {
16860+
"end": Object {
16861+
"column": 7,
16862+
"line": 1,
16863+
},
16864+
"start": Object {
16865+
"column": 6,
16866+
"line": 1,
16867+
},
16868+
},
16869+
"range": Array [
16870+
6,
16871+
7,
16872+
],
16873+
"type": "Punctuator",
16874+
"value": ";",
16875+
},
16876+
],
16877+
"type": "Program",
16878+
}
16879+
`;
16880+
1678316881
exports[`javascript fixtures/bigIntLiterals/octal.src 1`] = `
1678416882
Object {
1678516883
"body": Array [

packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e
224224

225225
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/bigIntLiterals/hex.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
226226

227+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/bigIntLiterals/numeric-separator.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
228+
227229
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/bigIntLiterals/octal.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
228230

229231
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/binaryLiterals/invalid.src 1`] = `

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy