Skip to content

Commit 39e5748

Browse files
committed
fix(typescript-estree): error throws on BigInt with numberic separator
1 parent 765ec4b commit 39e5748

File tree

6 files changed

+160
-2
lines changed

6 files changed

+160
-2
lines changed

Diff for: packages/parser/tests/lib/__snapshots__/javascript.ts.snap

+50
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,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1_2_3n;

Diff for: packages/typescript-estree/src/convert.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1931,8 +1931,14 @@ 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`
1935-
const value = typeof BigInt !== 'undefined' ? BigInt(bigint) : null;
1934+
const bigint = rawValue.slice(0, -1); // remove suffix `n`;
1935+
const value =
1936+
typeof BigInt !== 'undefined'
1937+
? BigInt(
1938+
// `BigInt` doesn't accept numeric separator
1939+
bigint.replace(/_/g, ''),
1940+
)
1941+
: null;
19361942
return this.createNode<TSESTree.BigIntLiteral>(node, {
19371943
type: AST_NODE_TYPES.Literal,
19381944
raw: rawValue,

Diff for: packages/typescript-estree/tests/ast-alignment/parse.ts

+1
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',

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

+98
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 [

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

+2
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)