Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Decimal Proposal #8901

Merged
merged 3 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions changelog_unreleased/javascript/pr-8901.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#### Support Decimal Proposal ([#8901](https://github.com/prettier/prettier/pull/8901) by [@fisker](https://github.com/fisker))

Support Stage-1 proposal [Decimal Proposal](https://github.com/tc39/proposal-decimal).

<!-- prettier-ignore -->
```js
// Input
0.3m;

// Prettier stable
SyntaxError: Identifier directly after number (1:4)
> 1 | 0.3m;

// Prettier master
0.3m;
```
4 changes: 4 additions & 0 deletions src/language-js/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function clean(ast, newObj, parent) {
newObj.value = newObj.value.toLowerCase();
}

if (ast.type === "DecimalLiteral") {
newObj.value = Number(newObj.value);
}

// We remove extra `;` and add them when needed
if (ast.type === "EmptyStatement") {
return null;
Expand Down
1 change: 1 addition & 0 deletions src/language-js/parser-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function babelOptions({ sourceType, extraPlugins = [] }) {
"privateIn",
["moduleAttributes", { version: "may-2020" }],
["recordAndTuple", { syntaxType: "hash" }],
"decimal",
...extraPlugins,
],
tokens: true,
Expand Down
2 changes: 2 additions & 0 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,8 @@ function printPathNoParens(path, options, print, args) {
return printRegex(n);
case "NumericLiteral": // Babel 6 Literal split
return printNumber(n.extra.raw);
case "DecimalLiteral":
return printNumber(n.value) + "m";
case "BigIntLiteral":
// babel: n.extra.raw, flow: n.bigint
return (n.bigint || n.extra.raw).toLowerCase();
Expand Down
2 changes: 2 additions & 0 deletions src/language-js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function isLiteral(node) {
node.type === "NullLiteral" ||
node.type === "NumericLiteral" ||
node.type === "BigIntLiteral" ||
node.type === "DecimalLiteral" ||
node.type === "RegExpLiteral" ||
node.type === "StringLiteral" ||
node.type === "TemplateLiteral" ||
Expand Down Expand Up @@ -904,6 +905,7 @@ function isSimpleCallArgument(node, depth) {
if (
node.type === "Literal" ||
node.type === "BigIntLiteral" ||
node.type === "DecimalLiteral" ||
node.type === "BooleanLiteral" ||
node.type === "NullLiteral" ||
node.type === "NumericLiteral" ||
Expand Down
63 changes: 63 additions & 0 deletions tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,69 @@ console.log(Bork.staticFunction()); // > "babelIsCool"
================================================================================
`;

exports[`decimal.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
// https://github.com/babel/babel/pull/11640

100m;
9223372036854775807m;
0.m;
3.1415926535897932m;
100.000m;
.1m;
({ 0m: 0, .1m() {}, get 0.2m(){}, set 3m(_){}, async 4m() {}, *.5m() {} });
1.m;
100m;
9223372036854775807m;
100.m;

// Invalid decimal
2e9m;
016432m;
089m;

// https://github.com/tc39/proposal-decimal
.1m + .2m === .3m;
2.00m;
-0m;
typeof 1m === "bigdecimal";
typeof 1m === "decimal128";


=====================================output=====================================
// https://github.com/babel/babel/pull/11640

100m;
9223372036854775807m;
0m;
3.1415926535897932m;
100.0m;
0.1m;
({ 0m: 0, 0.1m() {}, get 0.2m() {}, set 3m(_) {}, async 4m() {}, *0.5m() {} });
1m;
100m;
9223372036854775807m;
100m;

// Invalid decimal
2e9m;
016432m;
089m;

// https://github.com/tc39/proposal-decimal
0.1m + 0.2m === 0.3m;
2.0m;
-0m;
typeof 1m === "bigdecimal";
typeof 1m === "decimal128";

================================================================================
`;

exports[`decorators.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
Expand Down
26 changes: 26 additions & 0 deletions tests/js/babel-plugins/decimal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// https://github.com/babel/babel/pull/11640

100m;
9223372036854775807m;
0.m;
3.1415926535897932m;
100.000m;
.1m;
({ 0m: 0, .1m() {}, get 0.2m(){}, set 3m(_){}, async 4m() {}, *.5m() {} });
1.m;
100m;
9223372036854775807m;
100.m;

// Invalid decimal
2e9m;
016432m;
089m;

// https://github.com/tc39/proposal-decimal
.1m + .2m === .3m;
2.00m;
-0m;
typeof 1m === "bigdecimal";
typeof 1m === "decimal128";

18 changes: 18 additions & 0 deletions tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ exports[`snippet: #9 error test 1`] = `
| ^"
`;

exports[`snippet: #10 error test 1`] = `
"Invalid decimal (1:1)
> 1 | 0b101011101m;
| ^"
`;

exports[`snippet: #11 error test 1`] = `
"Invalid decimal (1:1)
> 1 | 0x16432m;
| ^"
`;

exports[`snippet: #12 error test 1`] = `
"Invalid decimal (1:1)
> 1 | 0o16432m;
| ^"
`;

exports[`static-import-source-should-not-has-extra-token.js error test 1`] = `
"Unexpected token (4:16)
2 | // be parenthesized, that accidentally break #8016
Expand Down
5 changes: 5 additions & 0 deletions tests/misc/errors/js/jsfmt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ run_spec(
// Don't support `syntaxType: "bar"`
"[| 1 |]",
// "{| a: 1 |}", // babel didn't throw on this

// Invalid decimal
"0b101011101m;",
"0x16432m;",
"0o16432m;",
],
},
["babel"]
Expand Down