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

feat: add support for PartialApplication #6397

Merged
merged 1 commit into from Oct 17, 2019
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
37 changes: 37 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -44,6 +44,42 @@ const link = <a href="example.com">http://example.com</a>;

-->

#### 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.

<!-- prettier-ignore -->
```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])

<!-- prettier-ignore -->
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/language-js/parser-babylon.js
Expand Up @@ -39,7 +39,8 @@ function babelOptions(extraOptions, extraPlugins) {
"throwExpressions",
"logicalAssignment",
"classPrivateMethods",
"v8intrinsic"
"v8intrinsic",
"partialApplication"
].concat(extraPlugins)
},
extraOptions
Expand Down
3 changes: 3 additions & 0 deletions src/language-js/printer-estree.js
Expand Up @@ -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));
Expand Down
31 changes: 31 additions & 0 deletions 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\`.

================================================================================
`;
1 change: 1 addition & 0 deletions tests/partial_application/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(__dirname, ["babel"]);
10 changes: 10 additions & 0 deletions 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`.