Skip to content

Commit

Permalink
WIP: eslint-parser supports BigInt Literal
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Dec 6, 2019
1 parent c9a6898 commit dbce724
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions eslint/babel-eslint-parser/package.json
Expand Up @@ -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",
Expand Down
Expand Up @@ -70,7 +70,6 @@ const astTransformVisitor = {
}

// modules

if (path.isImportDeclaration()) {
delete node.isType;
}
Expand Down
8 changes: 7 additions & 1 deletion eslint/babel-eslint-parser/test/babel-eslint-parser.js
Expand Up @@ -27,7 +27,7 @@ function parseAndAssertSame(code) {
loc: true,
range: true,
comment: true,
ecmaVersion: 2018,
ecmaVersion: 2020,
sourceType: "module",
});
const babylonAST = parseForESLint(code, {
Expand Down Expand Up @@ -518,5 +518,11 @@ describe("babylon-to-espree", () => {
}
`);
});

it("BigInt", () => {
parseAndAssertSame(`
const a = 1n;
`);
});
});
});
Expand Up @@ -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",
],
};
28 changes: 25 additions & 3 deletions 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";
Expand Down Expand Up @@ -31,6 +33,23 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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");
}
Expand Down Expand Up @@ -244,13 +263,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>

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);

Expand Down

0 comments on commit dbce724

Please sign in to comment.