Skip to content

Commit

Permalink
fix(typescript-estree): handle BigInt with _ numeric separator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 22, 2020
1 parent 765ec4b commit 66f1627
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 1 deletion.
50 changes: 50 additions & 0 deletions packages/parser/tests/lib/__snapshots__/javascript.ts.snap
Expand Up @@ -6815,6 +6815,56 @@ Object {
}
`;

exports[`javascript fixtures/bigIntLiterals/numeric-separator.src 1`] = `
Object {
"$id": 1,
"block": Object {
"range": Array [
0,
8,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 0,
"block": Object {
"range": Array [
0,
8,
],
"type": "Program",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [],
"type": "module",
"upperScope": Object {
"$ref": 1,
},
"variableMap": Object {},
"variableScope": Object {
"$ref": 0,
},
"variables": Array [],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 1,
},
"variables": Array [],
}
`;

exports[`javascript fixtures/bigIntLiterals/octal.src 1`] = `
Object {
"$id": 1,
Expand Down
@@ -0,0 +1 @@
1_2_3n;
7 changes: 6 additions & 1 deletion packages/typescript-estree/src/convert.ts
Expand Up @@ -1931,7 +1931,12 @@ export class Converter {
case SyntaxKind.BigIntLiteral: {
const range = getRange(node, this.ast);
const rawValue = this.ast.text.slice(range[0], range[1]);
const bigint = rawValue.slice(0, -1); // remove suffix `n`
const bigint = rawValue
// remove suffix `n`
.slice(0, -1)
// `BigInt` doesn't accept numeric separator
// and `bigint` property should not include numeric separator
.replace(/_/g, '');
const value = typeof BigInt !== 'undefined' ? BigInt(bigint) : null;
return this.createNode<TSESTree.BigIntLiteral>(node, {
type: AST_NODE_TYPES.Literal,
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-estree/tests/ast-alignment/parse.ts
Expand Up @@ -29,6 +29,7 @@ function parseWithBabelParser(text: string, jsx = true): any {
'dynamicImport',
'estree',
'bigInt',
'numericSeparator',
'importMeta',
'optionalChaining',
'nullishCoalescingOperator',
Expand Down
Expand Up @@ -16780,6 +16780,104 @@ Object {
}
`;

exports[`javascript fixtures/bigIntLiterals/numeric-separator.src 1`] = `
Object {
"body": Array [
Object {
"expression": Object {
"bigint": "123",
"loc": Object {
"end": Object {
"column": 6,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
6,
],
"raw": "1_2_3n",
"type": "Literal",
"value": 123n,
},
"loc": Object {
"end": Object {
"column": 7,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
7,
],
"type": "ExpressionStatement",
},
],
"loc": Object {
"end": Object {
"column": 0,
"line": 2,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
8,
],
"sourceType": "script",
"tokens": Array [
Object {
"loc": Object {
"end": Object {
"column": 6,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
6,
],
"type": "Identifier",
"value": "1_2_3n",
},
Object {
"loc": Object {
"end": Object {
"column": 7,
"line": 1,
},
"start": Object {
"column": 6,
"line": 1,
},
},
"range": Array [
6,
7,
],
"type": "Punctuator",
"value": ";",
},
],
"type": "Program",
}
`;

exports[`javascript fixtures/bigIntLiterals/octal.src 1`] = `
Object {
"body": Array [
Expand Down
Expand Up @@ -224,6 +224,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

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

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

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

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

0 comments on commit 66f1627

Please sign in to comment.