Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript-estree): handle BigInt with _ numeric separator #2067

Merged
merged 2 commits into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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