From 66f1627b11a566d5b925a577e800f99d5c808be2 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 23 May 2020 01:47:38 +0800 Subject: [PATCH] fix(typescript-estree): handle `BigInt` with `_` numeric separator (#2067) --- .../lib/__snapshots__/javascript.ts.snap | 50 ++++++++++ .../bigIntLiterals/numeric-separator.src.js | 1 + packages/typescript-estree/src/convert.ts | 7 +- .../tests/ast-alignment/parse.ts | 1 + .../lib/__snapshots__/javascript.ts.snap | 98 +++++++++++++++++++ .../semantic-diagnostics-enabled.ts.snap | 2 + 6 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 packages/shared-fixtures/fixtures/javascript/bigIntLiterals/numeric-separator.src.js diff --git a/packages/parser/tests/lib/__snapshots__/javascript.ts.snap b/packages/parser/tests/lib/__snapshots__/javascript.ts.snap index 654b09e21ab..48864fb7402 100644 --- a/packages/parser/tests/lib/__snapshots__/javascript.ts.snap +++ b/packages/parser/tests/lib/__snapshots__/javascript.ts.snap @@ -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, diff --git a/packages/shared-fixtures/fixtures/javascript/bigIntLiterals/numeric-separator.src.js b/packages/shared-fixtures/fixtures/javascript/bigIntLiterals/numeric-separator.src.js new file mode 100644 index 00000000000..75db46096c1 --- /dev/null +++ b/packages/shared-fixtures/fixtures/javascript/bigIntLiterals/numeric-separator.src.js @@ -0,0 +1 @@ +1_2_3n; diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 74f97e526a4..f23bac14ecb 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -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(node, { type: AST_NODE_TYPES.Literal, diff --git a/packages/typescript-estree/tests/ast-alignment/parse.ts b/packages/typescript-estree/tests/ast-alignment/parse.ts index 42f6e0f68fc..cd9c2db02f8 100644 --- a/packages/typescript-estree/tests/ast-alignment/parse.ts +++ b/packages/typescript-estree/tests/ast-alignment/parse.ts @@ -29,6 +29,7 @@ function parseWithBabelParser(text: string, jsx = true): any { 'dynamicImport', 'estree', 'bigInt', + 'numericSeparator', 'importMeta', 'optionalChaining', 'nullishCoalescingOperator', diff --git a/packages/typescript-estree/tests/lib/__snapshots__/javascript.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/javascript.ts.snap index 1ae708c8a40..004a3cabe0f 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/javascript.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/javascript.ts.snap @@ -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 [ diff --git a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap index bdc9a9a52dc..f94806437fe 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap @@ -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`] = `