diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 2efe9ef3..b69221ea 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -44,6 +44,42 @@ const link = http://example.com; --> +#### JavaScript: add support for PartialApplication ([#6397] by [@JounQin]) + +Previous versions would not be able to format this syntax, this has been fixed in this version. + + +```js +const addOne = add(1, ?); // apply from the left +addOne(2); // 3 + +const addTen = add(?, 10); // apply from the right +addTen(2); // 12 + +// with pipeline +let newScore = player.score + |> add(7, ?) + |> clamp(0, 100, ?); // shallow stack, the pipe to `clamp` is the same frame as the pipe to `add`. + +// Output (Prettier stable) +SyntaxError: Unexpected token (1:23) +> 1 | const addOne = add(1, ?); // apply from the left + | ^ + 2 | addOne(2); // 3 + 3 | + 4 | const addTen = add(?, 10); // apply from the right + +// Output (Prettier master) +const addOne = add(1, ?); // apply from the left +addOne(2); // 3 + +const addTen = add(?, 10); // apply from the right +addTen(2); // 12 + +// with pipeline +let newScore = player.score |> add(7, ?) |> clamp(0, 100, ?); // shallow stack, the pipe to \`clamp\` is the same frame as the pipe to \`add\`. +``` + #### JavaScript: More readable parentheses for new-call ([#6412] by [@bakkot]) @@ -995,6 +1031,7 @@ class A { [#6340]: https://github.com/prettier/prettier/pull/6340 [#6377]: https://github.com/prettier/prettier/pull/6377 [#6381]: https://github.com/prettier/prettier/pull/6381 +[#6397]: https://github.com/prettier/prettier/pull/6397 [#6404]: https://github.com/prettier/prettier/pull/6404 [#6411]: https://github.com/prettier/prettier/pull/6411 [#6412]: https://github.com/prettier/prettier/pull/6412 diff --git a/src/language-js/parser-babylon.js b/src/language-js/parser-babylon.js index a994ab15..6d81940f 100644 --- a/src/language-js/parser-babylon.js +++ b/src/language-js/parser-babylon.js @@ -39,7 +39,8 @@ function babelOptions(extraOptions, extraPlugins) { "throwExpressions", "logicalAssignment", "classPrivateMethods", - "v8intrinsic" + "v8intrinsic", + "partialApplication" ].concat(extraPlugins) }, extraOptions diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index d73d9429..38669a3a 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3506,6 +3506,9 @@ function printPathNoParens(path, options, print, args) { " as ", path.call(print, "alias") ]); + + case "ArgumentPlaceholder": + return "?"; default: /* istanbul ignore next */ throw new Error("unknown type: " + JSON.stringify(n.type)); diff --git a/tests/partial_application/__snapshots__/jsfmt.spec.js.snap b/tests/partial_application/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..c59cf1d2 --- /dev/null +++ b/tests/partial_application/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,31 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`test.js 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +const addOne = add(1, ?); // apply from the left +addOne(2); // 3 + +const addTen = add(?, 10); // apply from the right +addTen(2); // 12 + +// with pipeline +let newScore = player.score + |> add(7, ?) + |> clamp(0, 100, ?); // shallow stack, the pipe to \`clamp\` is the same frame as the pipe to \`add\`. + +=====================================output===================================== +const addOne = add(1, ?); // apply from the left +addOne(2); // 3 + +const addTen = add(?, 10); // apply from the right +addTen(2); // 12 + +// with pipeline +let newScore = player.score |> add(7, ?) |> clamp(0, 100, ?); // shallow stack, the pipe to \`clamp\` is the same frame as the pipe to \`add\`. + +================================================================================ +`; diff --git a/tests/partial_application/jsfmt.spec.js b/tests/partial_application/jsfmt.spec.js new file mode 100644 index 00000000..8382edde --- /dev/null +++ b/tests/partial_application/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["babel"]); diff --git a/tests/partial_application/test.js b/tests/partial_application/test.js new file mode 100644 index 00000000..e0892a80 --- /dev/null +++ b/tests/partial_application/test.js @@ -0,0 +1,10 @@ +const addOne = add(1, ?); // apply from the left +addOne(2); // 3 + +const addTen = add(?, 10); // apply from the right +addTen(2); // 12 + +// with pipeline +let newScore = player.score + |> add(7, ?) + |> clamp(0, 100, ?); // shallow stack, the pipe to `clamp` is the same frame as the pipe to `add`.