diff --git a/packages/babel-generator/src/generators/types.js b/packages/babel-generator/src/generators/types.js index cd69a75be1..f8ef19b2f9 100644 --- a/packages/babel-generator/src/generators/types.js +++ b/packages/babel-generator/src/generators/types.js @@ -143,3 +143,12 @@ export function StringLiteral(node: Object) { return this.token(val); } + +export function BigIntLiteral(node: Object) { + const raw = this.getPossibleRaw(node); + if (!this.format.minified && raw != null) { + this.token(raw); + return; + } + this.token(node.value); +} diff --git a/packages/babel-generator/test/fixtures/types/BigIntLiteral/input.js b/packages/babel-generator/test/fixtures/types/BigIntLiteral/input.js new file mode 100644 index 0000000000..0cb4563d0b --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/BigIntLiteral/input.js @@ -0,0 +1,5 @@ +100n; +9223372036854775807n; +0o16432n; +0xFFF123n; +0b101011101n; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/types/BigIntLiteral/options.json b/packages/babel-generator/test/fixtures/types/BigIntLiteral/options.json new file mode 100644 index 0000000000..2023dd95bd --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/BigIntLiteral/options.json @@ -0,0 +1 @@ +{ "plugins": ["bigInt"] } \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/types/BigIntLiteral/output.js b/packages/babel-generator/test/fixtures/types/BigIntLiteral/output.js new file mode 100644 index 0000000000..0cb4563d0b --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/BigIntLiteral/output.js @@ -0,0 +1,5 @@ +100n; +9223372036854775807n; +0o16432n; +0xFFF123n; +0b101011101n; \ No newline at end of file diff --git a/packages/babel-plugin-syntax-bigint/.npmignore b/packages/babel-plugin-syntax-bigint/.npmignore new file mode 100644 index 0000000000..cace0d6ddc --- /dev/null +++ b/packages/babel-plugin-syntax-bigint/.npmignore @@ -0,0 +1,3 @@ +node_modules +*.log +src diff --git a/packages/babel-plugin-syntax-bigint/README.md b/packages/babel-plugin-syntax-bigint/README.md new file mode 100644 index 0000000000..f4e1484e39 --- /dev/null +++ b/packages/babel-plugin-syntax-bigint/README.md @@ -0,0 +1,36 @@ +# @babel/plugin-syntax-bigint + +> Allow parsing of BigInt literals. + + +## Installation + +```sh +npm install --save-dev @babel/plugin-syntax-bigint +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +```json +{ + "plugins": ["@babel/plugin-syntax-bigint"] +} +``` + +### Via CLI + +```sh +babel --plugins @babel/plugin-syntax-bigint script.js +``` + +### Via Node API + +```javascript +require("babel-core").transform("code", { + plugins: ["@babel/plugin-syntax-bigint"] +}); +``` diff --git a/packages/babel-plugin-syntax-bigint/package.json b/packages/babel-plugin-syntax-bigint/package.json new file mode 100644 index 0000000000..6924b0db89 --- /dev/null +++ b/packages/babel-plugin-syntax-bigint/package.json @@ -0,0 +1,20 @@ +{ + "name": "@babel/plugin-syntax-bigint", + "version": "7.0.0-beta.47", + "description": "Allow parsing of BigInt literals", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-bigint", + "license": "MIT", + "main": "lib/index.js", + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "7.0.0-beta.47" + }, + "peerDependencies": { + "@babel/core": "7.0.0-beta.47" + }, + "devDependencies": { + "@babel/core": "7.0.0-beta.47" + } +} diff --git a/packages/babel-plugin-syntax-bigint/src/index.js b/packages/babel-plugin-syntax-bigint/src/index.js new file mode 100644 index 0000000000..085614f0a6 --- /dev/null +++ b/packages/babel-plugin-syntax-bigint/src/index.js @@ -0,0 +1,11 @@ +import { declare } from "@babel/helper-plugin-utils"; + +export default declare(api => { + api.assertVersion(7); + + return { + manipulateOptions(opts, parserOpts) { + parserOpts.plugins.push("bigInt"); + }, + }; +}); diff --git a/packages/babel-types/src/definitions/experimental.js b/packages/babel-types/src/definitions/experimental.js index 98e78bdfdf..6555d9344c 100644 --- a/packages/babel-types/src/definitions/experimental.js +++ b/packages/babel-types/src/definitions/experimental.js @@ -190,3 +190,13 @@ defineType("PrivateName", { }, }, }); + +defineType("BigIntLiteral", { + builder: ["value"], + fields: { + value: { + validate: assertValueType("string"), + }, + }, + aliases: ["Expression", "Pureish", "Literal", "Immutable"], +});