From dbce7249a142a962e866d1d4ad3a2a4c1e46bc78 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Thu, 5 Dec 2019 23:30:33 -0500 Subject: [PATCH] WIP: eslint-parser supports BigInt Literal --- eslint/babel-eslint-parser/package.json | 1 + .../src/babylon-to-espree/convertAST.js | 1 - .../test/babel-eslint-parser.js | 8 +++++- .../test/fixtures/config/babel.config.js | 1 + packages/babel-parser/src/plugins/estree.js | 28 +++++++++++++++++-- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/eslint/babel-eslint-parser/package.json b/eslint/babel-eslint-parser/package.json index e24970d36b8c..0205facb646d 100644 --- a/eslint/babel-eslint-parser/package.json +++ b/eslint/babel-eslint-parser/package.json @@ -34,6 +34,7 @@ "@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0", "@babel/plugin-proposal-optional-chaining": "^7.0.0", "@babel/plugin-proposal-pipeline-operator": "^7.0.0", + "@babel/plugin-syntax-bigint": "^7.7.4", "@babel/plugin-syntax-dynamic-import": "^7.0.0", "@babel/plugin-syntax-export-default-from": "^7.0.0", "@babel/plugin-syntax-export-namespace-from": "^7.0.0", diff --git a/eslint/babel-eslint-parser/src/babylon-to-espree/convertAST.js b/eslint/babel-eslint-parser/src/babylon-to-espree/convertAST.js index 4acce5fb5036..50d472803a0b 100644 --- a/eslint/babel-eslint-parser/src/babylon-to-espree/convertAST.js +++ b/eslint/babel-eslint-parser/src/babylon-to-espree/convertAST.js @@ -70,7 +70,6 @@ const astTransformVisitor = { } // modules - if (path.isImportDeclaration()) { delete node.isType; } diff --git a/eslint/babel-eslint-parser/test/babel-eslint-parser.js b/eslint/babel-eslint-parser/test/babel-eslint-parser.js index 37f15c03d310..d3aa67223b18 100644 --- a/eslint/babel-eslint-parser/test/babel-eslint-parser.js +++ b/eslint/babel-eslint-parser/test/babel-eslint-parser.js @@ -27,7 +27,7 @@ function parseAndAssertSame(code) { loc: true, range: true, comment: true, - ecmaVersion: 2018, + ecmaVersion: 2020, sourceType: "module", }); const babylonAST = parseForESLint(code, { @@ -518,5 +518,11 @@ describe("babylon-to-espree", () => { } `); }); + + it("BigInt", () => { + parseAndAssertSame(` + const a = 1n; + `); + }); }); }); diff --git a/eslint/babel-eslint-parser/test/fixtures/config/babel.config.js b/eslint/babel-eslint-parser/test/fixtures/config/babel.config.js index 4d49158dba90..56a2f07d9707 100644 --- a/eslint/babel-eslint-parser/test/fixtures/config/babel.config.js +++ b/eslint/babel-eslint-parser/test/fixtures/config/babel.config.js @@ -17,5 +17,6 @@ module.exports = { "@babel/plugin-syntax-export-namespace-from", ["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: false }], ["@babel/plugin-proposal-pipeline-operator", { proposal: "minimal" }], + "@babel/plugin-syntax-bigint", ], }; diff --git a/packages/babel-parser/src/plugins/estree.js b/packages/babel-parser/src/plugins/estree.js index 60bc1effffa7..8692bb6e1d07 100644 --- a/packages/babel-parser/src/plugins/estree.js +++ b/packages/babel-parser/src/plugins/estree.js @@ -1,5 +1,7 @@ // @flow +/* global BigInt */ + import { types as tt, TokenType } from "../tokenizer/types"; import type Parser from "../parser"; import * as N from "../types"; @@ -31,6 +33,23 @@ export default (superClass: Class): Class => return node; } + estreeParseBigIntLiteral(value: any): N.Node { + let bigInt = null; + try { + bigInt = BigInt(value); + } catch (e) { + // In environments that don't support BigInt, this value + // will be null. + } + + // Espree creates a "Numeric" token for BigIntLiterals. + this.state.type = tt.num; + const node = this.estreeParseLiteral(bigInt); + node.bigint = String(node.value); + + return node; + } + estreeParseLiteral(value: any): N.Node { return this.parseLiteral(value, "Literal"); } @@ -244,13 +263,16 @@ export default (superClass: Class): Class => parseExprAtom(refShorthandDefaultPos?: ?Pos): N.Expression { switch (this.state.type) { - case tt.regexp: - return this.estreeParseRegExpLiteral(this.state.value); - case tt.num: case tt.string: return this.estreeParseLiteral(this.state.value); + case tt.regexp: + return this.estreeParseRegExpLiteral(this.state.value); + + case tt.bigint: + return this.estreeParseBigIntLiteral(this.state.value); + case tt._null: return this.estreeParseLiteral(null);