From 059e9124ffb61b510fc21ec0b5629b3c899cbf1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 29 Jul 2020 16:43:15 -0400 Subject: [PATCH] Add decimal parsing support (#11640) * docs: add DecimalLiteral to AST spec * add decimal support * fix: throw invalid decimal on start * add DecimalLiteral type definitions * update parser typings * add generator support * add syntax-decimal plugin * Add syntax-decimal to babel-standalone * add syntax-decimal to missing plugin helpers * fix incorrect test macro --- .../src/parser/util/missing-plugin-helper.js | 6 + .../babel-generator/src/generators/types.js | 9 + .../minified/decimal-literal/input.js | 5 + .../minified/decimal-literal/options.json | 4 + .../minified/decimal-literal/output.js | 1 + .../fixtures/types/DecimalLiteral/input.js | 5 + .../types/DecimalLiteral/options.json | 3 + .../fixtures/types/DecimalLiteral/output.js | 5 + packages/babel-parser/ast/spec.md | 12 ++ .../babel-parser/src/parser/error-message.js | 1 + .../babel-parser/src/parser/expression.js | 8 +- packages/babel-parser/src/parser/util.js | 3 +- packages/babel-parser/src/plugins/estree.js | 14 ++ packages/babel-parser/src/tokenizer/index.js | 23 ++- packages/babel-parser/src/tokenizer/types.js | 1 + .../experimental/_no-plugin/decimal/input.js | 1 + .../_no-plugin/decimal/options.json | 3 + .../decimal/decimal-as-property-name/input.js | 1 + .../decimal-as-property-name/output.json | 188 ++++++++++++++++++ .../decimal/invalid-binary/input.js | 1 + .../decimal/invalid-binary/options.json | 6 + .../experimental/decimal/invalid-e/input.js | 1 + .../decimal/invalid-e/output.json | 29 +++ .../decimal/invalid-hexadecimal/input.js | 1 + .../decimal/invalid-hexadecimal/options.json | 6 + .../invalid-non-octal-decimal-int/input.js | 1 + .../invalid-non-octal-decimal-int/output.json | 29 +++ .../decimal/invalid-octal-legacy/input.js | 1 + .../decimal/invalid-octal-legacy/output.json | 29 +++ .../decimal/invalid-octal-new/input.js | 1 + .../decimal/invalid-octal-new/options.json | 6 + .../experimental/decimal/options.json | 3 + .../decimal/valid-decimal/input.js | 1 + .../decimal/valid-decimal/output.json | 26 +++ .../decimal/valid-float-decimal/input.js | 1 + .../decimal/valid-float-decimal/output.json | 26 +++ .../experimental/decimal/valid-float/input.js | 1 + .../decimal/valid-float/output.json | 26 +++ .../experimental/decimal/valid-large/input.js | 1 + .../decimal/valid-large/output.json | 26 +++ .../experimental/decimal/valid-small/input.js | 1 + .../decimal/valid-small/output.json | 26 +++ .../babel-parser/typings/babel-parser.d.ts | 1 + .../babel-plugin-syntax-decimal/.npmignore | 3 + .../babel-plugin-syntax-decimal/README.md | 19 ++ .../babel-plugin-syntax-decimal/package.json | 27 +++ .../babel-plugin-syntax-decimal/src/index.js | 13 ++ packages/babel-standalone/package.json | 1 + .../scripts/pluginConfig.json | 1 + .../babel-standalone/src/generated/plugins.js | 3 + .../babel-standalone/src/preset-stage-1.js | 1 + .../test/preset-stage-1.test.js | 13 ++ .../src/asserts/generated/index.js | 3 + .../src/builders/generated/index.js | 4 + .../src/definitions/experimental.js | 10 + .../src/validators/generated/index.js | 18 ++ 56 files changed, 655 insertions(+), 4 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/minified/decimal-literal/input.js create mode 100644 packages/babel-generator/test/fixtures/minified/decimal-literal/options.json create mode 100644 packages/babel-generator/test/fixtures/minified/decimal-literal/output.js create mode 100644 packages/babel-generator/test/fixtures/types/DecimalLiteral/input.js create mode 100644 packages/babel-generator/test/fixtures/types/DecimalLiteral/options.json create mode 100644 packages/babel-generator/test/fixtures/types/DecimalLiteral/output.js create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/decimal/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/_no-plugin/decimal/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/decimal-as-property-name/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/decimal-as-property-name/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-binary/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-binary/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-e/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-e/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-hexadecimal/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-hexadecimal/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-non-octal-decimal-int/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-non-octal-decimal-int/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-legacy/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-legacy/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-new/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-new/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-decimal/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-decimal/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-float-decimal/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-float-decimal/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-float/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-float/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-large/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-large/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-small/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/decimal/valid-small/output.json create mode 100644 packages/babel-plugin-syntax-decimal/.npmignore create mode 100644 packages/babel-plugin-syntax-decimal/README.md create mode 100644 packages/babel-plugin-syntax-decimal/package.json create mode 100644 packages/babel-plugin-syntax-decimal/src/index.js create mode 100644 packages/babel-standalone/test/preset-stage-1.test.js diff --git a/packages/babel-core/src/parser/util/missing-plugin-helper.js b/packages/babel-core/src/parser/util/missing-plugin-helper.js index fad0d136e298..89f6dc91c961 100644 --- a/packages/babel-core/src/parser/util/missing-plugin-helper.js +++ b/packages/babel-core/src/parser/util/missing-plugin-helper.js @@ -31,6 +31,12 @@ const pluginNameMap = { url: "https://git.io/JvpRG", }, }, + decimal: { + syntax: { + name: "@babel/plugin-syntax-decimal", + url: "https://git.io/JfKOH", + }, + }, decorators: { syntax: { name: "@babel/plugin-syntax-decorators", diff --git a/packages/babel-generator/src/generators/types.js b/packages/babel-generator/src/generators/types.js index cae6aed8d345..0b70c7a3d976 100644 --- a/packages/babel-generator/src/generators/types.js +++ b/packages/babel-generator/src/generators/types.js @@ -222,6 +222,15 @@ export function BigIntLiteral(node: Object) { this.token(node.value + "n"); } +export function DecimalLiteral(node: Object) { + const raw = this.getPossibleRaw(node); + if (!this.format.minified && raw != null) { + this.token(raw); + return; + } + this.token(node.value + "m"); +} + export function PipelineTopicExpression(node: Object) { this.print(node.expression, node); } diff --git a/packages/babel-generator/test/fixtures/minified/decimal-literal/input.js b/packages/babel-generator/test/fixtures/minified/decimal-literal/input.js new file mode 100644 index 000000000000..bde8a9a31991 --- /dev/null +++ b/packages/babel-generator/test/fixtures/minified/decimal-literal/input.js @@ -0,0 +1,5 @@ +100m; +9223372036854775807m; +0.m; +3.1415926535897932m; +100.000m; diff --git a/packages/babel-generator/test/fixtures/minified/decimal-literal/options.json b/packages/babel-generator/test/fixtures/minified/decimal-literal/options.json new file mode 100644 index 000000000000..739605a4b674 --- /dev/null +++ b/packages/babel-generator/test/fixtures/minified/decimal-literal/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["decimal"], + "minified": true +} diff --git a/packages/babel-generator/test/fixtures/minified/decimal-literal/output.js b/packages/babel-generator/test/fixtures/minified/decimal-literal/output.js new file mode 100644 index 000000000000..99661ce6bcf6 --- /dev/null +++ b/packages/babel-generator/test/fixtures/minified/decimal-literal/output.js @@ -0,0 +1 @@ +100m;9223372036854775807m;0.m;3.1415926535897932m;100.000m; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/types/DecimalLiteral/input.js b/packages/babel-generator/test/fixtures/types/DecimalLiteral/input.js new file mode 100644 index 000000000000..bde8a9a31991 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/DecimalLiteral/input.js @@ -0,0 +1,5 @@ +100m; +9223372036854775807m; +0.m; +3.1415926535897932m; +100.000m; diff --git a/packages/babel-generator/test/fixtures/types/DecimalLiteral/options.json b/packages/babel-generator/test/fixtures/types/DecimalLiteral/options.json new file mode 100644 index 000000000000..f4a30fe3d611 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/DecimalLiteral/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["decimal"] +} diff --git a/packages/babel-generator/test/fixtures/types/DecimalLiteral/output.js b/packages/babel-generator/test/fixtures/types/DecimalLiteral/output.js new file mode 100644 index 000000000000..b03e65f9b1eb --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/DecimalLiteral/output.js @@ -0,0 +1,5 @@ +100m; +9223372036854775807m; +0.m; +3.1415926535897932m; +100.000m; \ No newline at end of file diff --git a/packages/babel-parser/ast/spec.md b/packages/babel-parser/ast/spec.md index 6e1ba6126da4..0484bc9f1faa 100644 --- a/packages/babel-parser/ast/spec.md +++ b/packages/babel-parser/ast/spec.md @@ -11,6 +11,7 @@ These are the core @babel/parser (babylon) AST node types. - [BooleanLiteral](#booleanliteral) - [NumericLiteral](#numericliteral) - [BigIntLiteral](#bigintliteral) + - [DecimalLiteral](#decimalliteral) - [Programs](#programs) - [Functions](#functions) - [Statements](#statements) @@ -253,6 +254,17 @@ interface BigIntLiteral <: Literal { The `value` property is the string representation of the `BigInt` value. It doesn't include the suffix `n`. +## DecimalLiteral + +```js +interface DecimalLiteral <: Literal { + type: "DecimalLiteral"; + value: string; +} +``` + +The `value` property is the string representation of the `BigDecimal` value. It doesn't include the suffix `m`. + # Programs ```js diff --git a/packages/babel-parser/src/parser/error-message.js b/packages/babel-parser/src/parser/error-message.js index 1651bb344dc3..77953092dc8e 100644 --- a/packages/babel-parser/src/parser/error-message.js +++ b/packages/babel-parser/src/parser/error-message.js @@ -61,6 +61,7 @@ export const ErrorMessages = Object.freeze({ ImportOutsideModule: `'import' and 'export' may appear only with 'sourceType: "module"'`, InvalidBigIntLiteral: "Invalid BigIntLiteral", InvalidCodePoint: "Code point out of bounds", + InvalidDecimal: "Invalid decimal", InvalidDigit: "Expected number in radix %0", InvalidEscapeSequence: "Bad character escape sequence", InvalidEscapeSequenceTemplate: "Invalid escape sequence in template", diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index fc8446548d86..1a7c973e094c 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1032,6 +1032,9 @@ export default class ExpressionParser extends LValParser { case tt.bigint: return this.parseLiteral(this.state.value, "BigIntLiteral"); + case tt.decimal: + return this.parseLiteral(this.state.value, "DecimalLiteral"); + case tt.string: return this.parseLiteral(this.state.value, "StringLiteral"); @@ -1859,7 +1862,10 @@ export default class ExpressionParser extends LValParser { this.state.inPropertyName = true; // We check if it's valid for it to be a private name when we push it. (prop: $FlowFixMe).key = - this.match(tt.num) || this.match(tt.string) || this.match(tt.bigint) + this.match(tt.num) || + this.match(tt.string) || + this.match(tt.bigint) || + this.match(tt.decimal) ? this.parseExprAtom() : this.parseMaybePrivateName(isPrivateNameAllowed); diff --git a/packages/babel-parser/src/parser/util.js b/packages/babel-parser/src/parser/util.js index 8928698e6b70..d6aa742df695 100644 --- a/packages/babel-parser/src/parser/util.js +++ b/packages/babel-parser/src/parser/util.js @@ -273,7 +273,8 @@ export default class UtilParser extends Tokenizer { !!this.state.type.keyword || this.match(tt.string) || this.match(tt.num) || - this.match(tt.bigint) + this.match(tt.bigint) || + this.match(tt.decimal) ); } } diff --git a/packages/babel-parser/src/plugins/estree.js b/packages/babel-parser/src/plugins/estree.js index 6c5f90780ea0..fce302d53047 100644 --- a/packages/babel-parser/src/plugins/estree.js +++ b/packages/babel-parser/src/plugins/estree.js @@ -43,6 +43,17 @@ export default (superClass: Class): Class => return node; } + estreeParseDecimalLiteral(value: any): N.Node { + // https://github.com/estree/estree/blob/master/experimental/decimal.md + // $FlowIgnore + // todo: use BigDecimal when node supports it. + const decimal = null; + const node = this.estreeParseLiteral(decimal); + node.decimal = String(node.value || value); + + return node; + } + estreeParseLiteral(value: any): N.Node { return this.parseLiteral(value, "Literal"); } @@ -229,6 +240,9 @@ export default (superClass: Class): Class => case tt.bigint: return this.estreeParseBigIntLiteral(this.state.value); + case tt.decimal: + return this.estreeParseDecimalLiteral(this.state.value); + case tt._null: return this.estreeParseLiteral(null); diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index d9b4e324e0a9..4657690d2906 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -1095,6 +1095,8 @@ export default class Tokenizer extends ParserErrors { if (next === charCodes.lowercaseN) { ++this.state.pos; isBigInt = true; + } else if (next === charCodes.lowercaseM) { + throw this.raise(start, Errors.InvalidDecimal); } if (isIdentifierStart(this.input.codePointAt(this.state.pos))) { @@ -1116,6 +1118,8 @@ export default class Tokenizer extends ParserErrors { const start = this.state.pos; let isFloat = false; let isBigInt = false; + let isDecimal = false; + let hasExponent = false; let isOctal = false; if (!startsWithDot && this.readInt(10) === null) { @@ -1157,6 +1161,7 @@ export default class Tokenizer extends ParserErrors { } if (this.readInt(10) === null) this.raise(start, Errors.InvalidNumber); isFloat = true; + hasExponent = true; next = this.input.charCodeAt(this.state.pos); } @@ -1174,18 +1179,32 @@ export default class Tokenizer extends ParserErrors { isBigInt = true; } + if (next === charCodes.lowercaseM) { + this.expectPlugin("decimal", this.state.pos); + if (hasExponent || hasLeadingZero) { + this.raise(start, Errors.InvalidDecimal); + } + ++this.state.pos; + isDecimal = true; + } + if (isIdentifierStart(this.input.codePointAt(this.state.pos))) { throw this.raise(this.state.pos, Errors.NumberIdentifier); } - // remove "_" for numeric literal separator, and "n" for BigInts - const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, ""); + // remove "_" for numeric literal separator, and trailing `m` or `n` + const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, ""); if (isBigInt) { this.finishToken(tt.bigint, str); return; } + if (isDecimal) { + this.finishToken(tt.decimal, str); + return; + } + const val = isOctal ? parseInt(str, 8) : parseFloat(str); this.finishToken(tt.num, val); } diff --git a/packages/babel-parser/src/tokenizer/types.js b/packages/babel-parser/src/tokenizer/types.js index acc456e9f952..535e16bd1204 100644 --- a/packages/babel-parser/src/tokenizer/types.js +++ b/packages/babel-parser/src/tokenizer/types.js @@ -86,6 +86,7 @@ function createBinop(name: string, binop: number) { export const types: { [name: string]: TokenType } = { num: new TokenType("num", { startsExpr }), bigint: new TokenType("bigint", { startsExpr }), + decimal: new TokenType("decimal", { startsExpr }), regexp: new TokenType("regexp", { startsExpr }), string: new TokenType("string", { startsExpr }), name: new TokenType("name", { startsExpr }), diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/decimal/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/decimal/input.js new file mode 100644 index 000000000000..f72cfcd590f9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/decimal/input.js @@ -0,0 +1 @@ +.1m; diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/decimal/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/decimal/options.json new file mode 100644 index 000000000000..146c207dab5b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/decimal/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: 'decimal' (1:2)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/decimal-as-property-name/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/decimal-as-property-name/input.js new file mode 100644 index 000000000000..61bed1edcf52 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/decimal-as-property-name/input.js @@ -0,0 +1 @@ +({ 0m: 0, .1m() {}, get 0.2m(){}, set 3m(_){}, async 4m() {}, *.5m() {} }); diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/decimal-as-property-name/output.json b/packages/babel-parser/test/fixtures/experimental/decimal/decimal-as-property-name/output.json new file mode 100644 index 000000000000..fef2948b8d28 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/decimal-as-property-name/output.json @@ -0,0 +1,188 @@ +{ + "type": "File", + "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, + "program": { + "type": "Program", + "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, + "expression": { + "type": "ObjectExpression", + "start":1,"end":73,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":73}}, + "properties": [ + { + "type": "ObjectProperty", + "start":3,"end":8,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":8}}, + "method": false, + "key": { + "type": "DecimalLiteral", + "start":3,"end":5,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":5}}, + "extra": { + "rawValue": "0", + "raw": "0m" + }, + "value": "0" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "NumericLiteral", + "start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + { + "type": "ObjectMethod", + "start":10,"end":18,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":18}}, + "method": true, + "key": { + "type": "DecimalLiteral", + "start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13}}, + "extra": { + "rawValue": ".1", + "raw": ".1m" + }, + "value": ".1" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":16,"end":18,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":18}}, + "body": [], + "directives": [] + } + }, + { + "type": "ObjectMethod", + "start":20,"end":32,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":32}}, + "method": false, + "key": { + "type": "DecimalLiteral", + "start":24,"end":28,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":28}}, + "extra": { + "rawValue": "0.2", + "raw": "0.2m" + }, + "value": "0.2" + }, + "computed": false, + "kind": "get", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":30,"end":32,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":32}}, + "body": [], + "directives": [] + } + }, + { + "type": "ObjectMethod", + "start":34,"end":45,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":45}}, + "method": false, + "key": { + "type": "DecimalLiteral", + "start":38,"end":40,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":40}}, + "extra": { + "rawValue": "3", + "raw": "3m" + }, + "value": "3" + }, + "computed": false, + "kind": "set", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":41,"end":42,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":42},"identifierName":"_"}, + "name": "_" + } + ], + "body": { + "type": "BlockStatement", + "start":43,"end":45,"loc":{"start":{"line":1,"column":43},"end":{"line":1,"column":45}}, + "body": [], + "directives": [] + } + }, + { + "type": "ObjectMethod", + "start":47,"end":60,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":60}}, + "method": true, + "key": { + "type": "DecimalLiteral", + "start":53,"end":55,"loc":{"start":{"line":1,"column":53},"end":{"line":1,"column":55}}, + "extra": { + "rawValue": "4", + "raw": "4m" + }, + "value": "4" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":58,"end":60,"loc":{"start":{"line":1,"column":58},"end":{"line":1,"column":60}}, + "body": [], + "directives": [] + } + }, + { + "type": "ObjectMethod", + "start":62,"end":71,"loc":{"start":{"line":1,"column":62},"end":{"line":1,"column":71}}, + "method": true, + "key": { + "type": "DecimalLiteral", + "start":63,"end":66,"loc":{"start":{"line":1,"column":63},"end":{"line":1,"column":66}}, + "extra": { + "rawValue": ".5", + "raw": ".5m" + }, + "value": ".5" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": true, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":69,"end":71,"loc":{"start":{"line":1,"column":69},"end":{"line":1,"column":71}}, + "body": [], + "directives": [] + } + } + ], + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-binary/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-binary/input.js new file mode 100644 index 000000000000..7e56761a593a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-binary/input.js @@ -0,0 +1 @@ +0b101011101m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-binary/options.json b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-binary/options.json new file mode 100644 index 000000000000..258615f550b5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-binary/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "decimal" + ], + "throws": "Invalid decimal (1:0)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-e/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-e/input.js new file mode 100644 index 000000000000..075e884e4629 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-e/input.js @@ -0,0 +1 @@ +2e9m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-e/output.json b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-e/output.json new file mode 100644 index 000000000000..1ae22702c120 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-e/output.json @@ -0,0 +1,29 @@ +{ + "type": "File", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "errors": [ + "SyntaxError: Invalid decimal (1:0)" + ], + "program": { + "type": "Program", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "expression": { + "type": "DecimalLiteral", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "extra": { + "rawValue": "2e9", + "raw": "2e9m" + }, + "value": "2e9" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-hexadecimal/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-hexadecimal/input.js new file mode 100644 index 000000000000..7b5b4d50d8f8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-hexadecimal/input.js @@ -0,0 +1 @@ +0x16432m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-hexadecimal/options.json b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-hexadecimal/options.json new file mode 100644 index 000000000000..258615f550b5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-hexadecimal/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "decimal" + ], + "throws": "Invalid decimal (1:0)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-non-octal-decimal-int/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-non-octal-decimal-int/input.js new file mode 100644 index 000000000000..b0d749efc714 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-non-octal-decimal-int/input.js @@ -0,0 +1 @@ +089m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-non-octal-decimal-int/output.json b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-non-octal-decimal-int/output.json new file mode 100644 index 000000000000..60c5dbd4f533 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-non-octal-decimal-int/output.json @@ -0,0 +1,29 @@ +{ + "type": "File", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "errors": [ + "SyntaxError: Invalid decimal (1:0)" + ], + "program": { + "type": "Program", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "expression": { + "type": "DecimalLiteral", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "extra": { + "rawValue": "089", + "raw": "089m" + }, + "value": "089" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-legacy/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-legacy/input.js new file mode 100644 index 000000000000..c3fffee2f367 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-legacy/input.js @@ -0,0 +1 @@ +016432m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-legacy/output.json b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-legacy/output.json new file mode 100644 index 000000000000..2c49c0fb79fe --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-legacy/output.json @@ -0,0 +1,29 @@ +{ + "type": "File", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "errors": [ + "SyntaxError: Invalid decimal (1:0)" + ], + "program": { + "type": "Program", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "expression": { + "type": "DecimalLiteral", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "extra": { + "rawValue": "016432", + "raw": "016432m" + }, + "value": "016432" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-new/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-new/input.js new file mode 100644 index 000000000000..af3272769d9c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-new/input.js @@ -0,0 +1 @@ +0o16432m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-new/options.json b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-new/options.json new file mode 100644 index 000000000000..258615f550b5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/invalid-octal-new/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "decimal" + ], + "throws": "Invalid decimal (1:0)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/options.json b/packages/babel-parser/test/fixtures/experimental/decimal/options.json new file mode 100644 index 000000000000..f4a30fe3d611 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["decimal"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-decimal/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/valid-decimal/input.js new file mode 100644 index 000000000000..762f620bbb4e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-decimal/input.js @@ -0,0 +1 @@ +1.m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-decimal/output.json b/packages/babel-parser/test/fixtures/experimental/decimal/valid-decimal/output.json new file mode 100644 index 000000000000..9273cf35d352 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-decimal/output.json @@ -0,0 +1,26 @@ +{ + "type": "File", + "start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}}, + "program": { + "type": "Program", + "start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}}, + "expression": { + "type": "DecimalLiteral", + "start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}}, + "extra": { + "rawValue": "1.", + "raw": "1.m" + }, + "value": "1." + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-float-decimal/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/valid-float-decimal/input.js new file mode 100644 index 000000000000..66c782047353 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-float-decimal/input.js @@ -0,0 +1 @@ +100.0m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-float-decimal/output.json b/packages/babel-parser/test/fixtures/experimental/decimal/valid-float-decimal/output.json new file mode 100644 index 000000000000..3ad9bde5c4c2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-float-decimal/output.json @@ -0,0 +1,26 @@ +{ + "type": "File", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}, + "program": { + "type": "Program", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}, + "expression": { + "type": "DecimalLiteral", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}, + "extra": { + "rawValue": "100.0", + "raw": "100.0m" + }, + "value": "100.0" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-float/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/valid-float/input.js new file mode 100644 index 000000000000..f53b99cf2635 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-float/input.js @@ -0,0 +1 @@ +100m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-float/output.json b/packages/babel-parser/test/fixtures/experimental/decimal/valid-float/output.json new file mode 100644 index 000000000000..bc4b9d85d535 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-float/output.json @@ -0,0 +1,26 @@ +{ + "type": "File", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "program": { + "type": "Program", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "expression": { + "type": "DecimalLiteral", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "extra": { + "rawValue": "100", + "raw": "100m" + }, + "value": "100" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-large/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/valid-large/input.js new file mode 100644 index 000000000000..8d9ca018cf89 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-large/input.js @@ -0,0 +1 @@ +9223372036854775807m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-large/output.json b/packages/babel-parser/test/fixtures/experimental/decimal/valid-large/output.json new file mode 100644 index 000000000000..084218fa1e9b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-large/output.json @@ -0,0 +1,26 @@ +{ + "type": "File", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}}, + "program": { + "type": "Program", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}}, + "expression": { + "type": "DecimalLiteral", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}}, + "extra": { + "rawValue": "9223372036854775807", + "raw": "9223372036854775807m" + }, + "value": "9223372036854775807" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-small/input.js b/packages/babel-parser/test/fixtures/experimental/decimal/valid-small/input.js new file mode 100644 index 000000000000..d36e0ec8ac19 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-small/input.js @@ -0,0 +1 @@ +100.m diff --git a/packages/babel-parser/test/fixtures/experimental/decimal/valid-small/output.json b/packages/babel-parser/test/fixtures/experimental/decimal/valid-small/output.json new file mode 100644 index 000000000000..29cf5bca6c5f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/decimal/valid-small/output.json @@ -0,0 +1,26 @@ +{ + "type": "File", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "program": { + "type": "Program", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "expression": { + "type": "DecimalLiteral", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "extra": { + "rawValue": "100.", + "raw": "100.m" + }, + "value": "100." + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/typings/babel-parser.d.ts b/packages/babel-parser/typings/babel-parser.d.ts index bd406708a8fa..60fabcc52510 100644 --- a/packages/babel-parser/typings/babel-parser.d.ts +++ b/packages/babel-parser/typings/babel-parser.d.ts @@ -100,6 +100,7 @@ export type ParserPlugin = 'classPrivateMethods' | 'classPrivateProperties' | 'classProperties' | + 'decimal' | 'decorators' | 'decorators-legacy' | 'doExpressions' | diff --git a/packages/babel-plugin-syntax-decimal/.npmignore b/packages/babel-plugin-syntax-decimal/.npmignore new file mode 100644 index 000000000000..f9806945836e --- /dev/null +++ b/packages/babel-plugin-syntax-decimal/.npmignore @@ -0,0 +1,3 @@ +src +test +*.log diff --git a/packages/babel-plugin-syntax-decimal/README.md b/packages/babel-plugin-syntax-decimal/README.md new file mode 100644 index 000000000000..b0efd10dff31 --- /dev/null +++ b/packages/babel-plugin-syntax-decimal/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-syntax-decimal + +> Allow parsing of decimal + +See our website [@babel/plugin-syntax-decimal](https://babeljs.io/docs/en/next/babel-plugin-syntax-decimal.html) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-syntax-decimal +``` + +or using yarn: + +```sh +yarn add @babel/plugin-syntax-decimal --dev +``` diff --git a/packages/babel-plugin-syntax-decimal/package.json b/packages/babel-plugin-syntax-decimal/package.json new file mode 100644 index 000000000000..47902609778f --- /dev/null +++ b/packages/babel-plugin-syntax-decimal/package.json @@ -0,0 +1,27 @@ +{ + "name": "@babel/plugin-syntax-decimal", + "version": "7.10.1", + "description": "Allow parsing of decimal", + "repository": { + "type": "git", + "url": "https://github.com/babel/babel.git", + "directory": "packages/babel-plugin-syntax-decimal" + }, + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "main": "lib/index.js", + "exports": { + ".": "./lib/index.js" + }, + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "^7.10.1" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } +} diff --git a/packages/babel-plugin-syntax-decimal/src/index.js b/packages/babel-plugin-syntax-decimal/src/index.js new file mode 100644 index 000000000000..6e7562d73e5e --- /dev/null +++ b/packages/babel-plugin-syntax-decimal/src/index.js @@ -0,0 +1,13 @@ +import { declare } from "@babel/helper-plugin-utils"; + +export default declare(api => { + api.assertVersion(7); + + return { + name: "syntax-decimal", + + manipulateOptions(opts, parserOpts) { + parserOpts.plugins.push("decimal"); + }, + }; +}); diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json index 6bc21815cf0d..948ef473b451 100644 --- a/packages/babel-standalone/package.json +++ b/packages/babel-standalone/package.json @@ -37,6 +37,7 @@ "@babel/plugin-proposal-unicode-property-regex": "^7.10.4", "@babel/plugin-syntax-async-generators": "^7.8.0", "@babel/plugin-syntax-class-properties": "^7.10.4", + "@babel/plugin-syntax-decimal": "^7.10.1", "@babel/plugin-syntax-decorators": "^7.10.4", "@babel/plugin-syntax-do-expressions": "^7.10.4", "@babel/plugin-syntax-export-default-from": "^7.10.4", diff --git a/packages/babel-standalone/scripts/pluginConfig.json b/packages/babel-standalone/scripts/pluginConfig.json index 9127553a4b26..2b2c3f3a4219 100644 --- a/packages/babel-standalone/scripts/pluginConfig.json +++ b/packages/babel-standalone/scripts/pluginConfig.json @@ -2,6 +2,7 @@ "external-helpers", "syntax-async-generators", "syntax-class-properties", + "syntax-decimal", "syntax-decorators", "syntax-do-expressions", "syntax-export-default-from", diff --git a/packages/babel-standalone/src/generated/plugins.js b/packages/babel-standalone/src/generated/plugins.js index 3ddd12be9731..757d39e53cc9 100644 --- a/packages/babel-standalone/src/generated/plugins.js +++ b/packages/babel-standalone/src/generated/plugins.js @@ -6,6 +6,7 @@ import externalHelpers from "@babel/plugin-external-helpers"; import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators"; import syntaxClassProperties from "@babel/plugin-syntax-class-properties"; +import syntaxDecimal from "@babel/plugin-syntax-decimal"; import syntaxDecorators from "@babel/plugin-syntax-decorators"; import syntaxDoExpressions from "@babel/plugin-syntax-do-expressions"; import syntaxExportDefaultFrom from "@babel/plugin-syntax-export-default-from"; @@ -98,6 +99,7 @@ export { externalHelpers, syntaxAsyncGenerators, syntaxClassProperties, + syntaxDecimal, syntaxDecorators, syntaxDoExpressions, syntaxExportDefaultFrom, @@ -191,6 +193,7 @@ export const all = { "external-helpers": externalHelpers, "syntax-async-generators": syntaxAsyncGenerators, "syntax-class-properties": syntaxClassProperties, + "syntax-decimal": syntaxDecimal, "syntax-decorators": syntaxDecorators, "syntax-do-expressions": syntaxDoExpressions, "syntax-export-default-from": syntaxExportDefaultFrom, diff --git a/packages/babel-standalone/src/preset-stage-1.js b/packages/babel-standalone/src/preset-stage-1.js index c11755b6681d..0e0cd18c138c 100644 --- a/packages/babel-standalone/src/preset-stage-1.js +++ b/packages/babel-standalone/src/preset-stage-1.js @@ -21,6 +21,7 @@ export default (_: any, opts: Object = {}) => { ], ], plugins: [ + babelPlugins.syntaxDecimal, [ babelPlugins.syntaxModuleAttributes, { version: moduleAttributesVersion }, diff --git a/packages/babel-standalone/test/preset-stage-1.test.js b/packages/babel-standalone/test/preset-stage-1.test.js new file mode 100644 index 000000000000..fbcb49b6ff0b --- /dev/null +++ b/packages/babel-standalone/test/preset-stage-1.test.js @@ -0,0 +1,13 @@ +(process.env.TEST_TYPE === "cov" ? describe.skip : describe)( + "stage-1 preset", + () => { + const Babel = require("../babel"); + + it("should parser decimal literal", () => { + const output = Babel.transform("0.3m", { + presets: [["stage-1", { decoratorsBeforeExport: true }]], + }).code; + expect(output).toBe("0.3m;"); + }); + }, +); diff --git a/packages/babel-types/src/asserts/generated/index.js b/packages/babel-types/src/asserts/generated/index.js index 9ce3c1565f90..4492e04f4499 100644 --- a/packages/babel-types/src/asserts/generated/index.js +++ b/packages/babel-types/src/asserts/generated/index.js @@ -800,6 +800,9 @@ export function assertRecordExpression(node: Object, opts?: Object = {}): void { export function assertTupleExpression(node: Object, opts?: Object = {}): void { assert("TupleExpression", node, opts); } +export function assertDecimalLiteral(node: Object, opts?: Object = {}): void { + assert("DecimalLiteral", node, opts); +} export function assertTSParameterProperty( node: Object, opts?: Object = {}, diff --git a/packages/babel-types/src/builders/generated/index.js b/packages/babel-types/src/builders/generated/index.js index ec92b4687377..5e9fe9292109 100644 --- a/packages/babel-types/src/builders/generated/index.js +++ b/packages/babel-types/src/builders/generated/index.js @@ -730,6 +730,10 @@ export function tupleExpression(...args: Array): Object { return builder("TupleExpression", ...args); } export { tupleExpression as TupleExpression }; +export function decimalLiteral(...args: Array): Object { + return builder("DecimalLiteral", ...args); +} +export { decimalLiteral as DecimalLiteral }; export function tsParameterProperty(...args: Array): Object { return builder("TSParameterProperty", ...args); } diff --git a/packages/babel-types/src/definitions/experimental.js b/packages/babel-types/src/definitions/experimental.js index c3adc3395094..984e1897da3d 100644 --- a/packages/babel-types/src/definitions/experimental.js +++ b/packages/babel-types/src/definitions/experimental.js @@ -235,3 +235,13 @@ defineType("TupleExpression", { visitor: ["elements"], aliases: ["Expression"], }); + +defineType("DecimalLiteral", { + builder: ["value"], + fields: { + value: { + validate: assertValueType("string"), + }, + }, + aliases: ["Expression", "Pureish", "Literal", "Immutable"], +}); diff --git a/packages/babel-types/src/validators/generated/index.js b/packages/babel-types/src/validators/generated/index.js index 8962a809f033..8b60e308674b 100644 --- a/packages/babel-types/src/validators/generated/index.js +++ b/packages/babel-types/src/validators/generated/index.js @@ -2573,6 +2573,20 @@ export function isTupleExpression(node: ?Object, opts?: Object): boolean { return false; } +export function isDecimalLiteral(node: ?Object, opts?: Object): boolean { + if (!node) return false; + + const nodeType = node.type; + if (nodeType === "DecimalLiteral") { + if (typeof opts === "undefined") { + return true; + } else { + return shallowEqual(node, opts); + } + } + + return false; +} export function isTSParameterProperty(node: ?Object, opts?: Object): boolean { if (!node) return false; @@ -3504,6 +3518,7 @@ export function isExpression(node: ?Object, opts?: Object): boolean { "DoExpression" === nodeType || "RecordExpression" === nodeType || "TupleExpression" === nodeType || + "DecimalLiteral" === nodeType || "TSAsExpression" === nodeType || "TSTypeAssertion" === nodeType || "TSNonNullExpression" === nodeType || @@ -3908,6 +3923,7 @@ export function isPureish(node: ?Object, opts?: Object): boolean { "RegExpLiteral" === nodeType || "ArrowFunctionExpression" === nodeType || "BigIntLiteral" === nodeType || + "DecimalLiteral" === nodeType || (nodeType === "Placeholder" && "StringLiteral" === node.expectedNode) ) { if (typeof opts === "undefined") { @@ -4042,6 +4058,7 @@ export function isLiteral(node: ?Object, opts?: Object): boolean { "RegExpLiteral" === nodeType || "TemplateLiteral" === nodeType || "BigIntLiteral" === nodeType || + "DecimalLiteral" === nodeType || (nodeType === "Placeholder" && "StringLiteral" === node.expectedNode) ) { if (typeof opts === "undefined") { @@ -4074,6 +4091,7 @@ export function isImmutable(node: ?Object, opts?: Object): boolean { "JSXFragment" === nodeType || "JSXOpeningFragment" === nodeType || "JSXClosingFragment" === nodeType || + "DecimalLiteral" === nodeType || (nodeType === "Placeholder" && "StringLiteral" === node.expectedNode) ) { if (typeof opts === "undefined") {