-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Partial application plugin #9474
Merged
nicolo-ribaudo
merged 16 commits into
babel:master
from
byara:partial-application-plugin
Mar 13, 2019
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
aa5cb0f
add partial application syntax and some tests
571e245
add Partial Application Plugin
ad26310
update unwrapArguments function and add replacePlaceholders.
26c2230
update the tests
ea6d52a
Merge remote-tracking branch 'upstream/master'
e3cac87
Merge branch 'master' into partial-application-plugin
45ffe18
remove unnecessary clone
2cc2f42
update readme and description an package.json
ac4bb3a
Merge remote-tracking branch 'upstream/master' into partial-applicati…
fe682e4
Merge remote-tracking branch 'upstream/master' into partial-applicati…
425754d
put options.json one level up and add test for not assigned and chained
86eab9b
add await to fix the test
dabf034
use Promise in the test instead of async await
915b73c
remove unnecessary method test [#9474]
e939cf8
push sequenceExpressions to an array [#9474]
35952e2
Merge remote-tracking branch 'upstream/master' into partial-applicati…
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,4 +96,4 @@ | |
], | ||
"directives": [] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,4 +302,4 @@ | |
], | ||
"directives": [] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,4 +257,4 @@ | |
], | ||
"directives": [] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,4 +227,4 @@ | |
], | ||
"directives": [] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,4 +227,4 @@ | |
], | ||
"directives": [] | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-partial-application/.npmignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
test | ||
*.log |
19 changes: 19 additions & 0 deletions
19
packages/babel-plugin-proposal-partial-application/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# @babel/plugin-proposal-partial-application | ||
|
||
> Introduces a new ? token in an argument list which allows for partially applying an argument list to a call expression. | ||
|
||
See our website [@babel/plugin-proposal-partial-application](https://babeljs.io/docs/en/next/babel-plugin-proposal-partial-application.html) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-proposal-partial-application | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-proposal-partial-application --dev | ||
``` |
25 changes: 25 additions & 0 deletions
25
packages/babel-plugin-proposal-partial-application/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@babel/plugin-proposal-partial-application", | ||
"version": "7.2.0", | ||
"description": "Introduces a new ? token in an argument list which allows for partially applying an argument list to a call expression", | ||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-partial-application", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"babel-plugin" | ||
], | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "^7.0.0", | ||
"@babel/plugin-syntax-partial-application": "^7.2.0" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.0.0-0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.2.0", | ||
"@babel/helper-plugin-test-runner": "^7.0.0" | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
packages/babel-plugin-proposal-partial-application/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import syntaxPartialApplication from "@babel/plugin-syntax-partial-application"; | ||
import { types as t } from "@babel/core"; | ||
|
||
export default declare(api => { | ||
api.assertVersion(7); | ||
|
||
/** | ||
* a function to figure out if a call expression has | ||
* ArgumentPlaceholder as one of its arguments | ||
* @param node a callExpression node | ||
* @returns boolean | ||
*/ | ||
function hasArgumentPlaceholder(node) { | ||
return node.arguments.some(arg => t.isArgumentPlaceholder(arg)); | ||
} | ||
|
||
function unwrapArguments(node, scope) { | ||
const init = []; | ||
for (let i = 0; i < node.arguments.length; i++) { | ||
if ( | ||
!t.isArgumentPlaceholder(node.arguments[i]) && | ||
!t.isImmutable(node.arguments[i]) | ||
) { | ||
const id = scope.generateUidIdentifierBasedOnNode( | ||
node.arguments[i], | ||
"param", | ||
); | ||
scope.push({ id }); | ||
if (t.isSpreadElement(node.arguments[i])) { | ||
init.push( | ||
t.assignmentExpression( | ||
"=", | ||
t.cloneNode(id), | ||
t.arrayExpression([t.spreadElement(node.arguments[i].argument)]), | ||
), | ||
); | ||
node.arguments[i].argument = t.cloneNode(id); | ||
} else { | ||
init.push( | ||
t.assignmentExpression("=", t.cloneNode(id), node.arguments[i]), | ||
); | ||
node.arguments[i] = t.cloneNode(id); | ||
} | ||
} | ||
} | ||
return init; | ||
} | ||
|
||
function replacePlaceholders(node, scope) { | ||
const placeholders = []; | ||
const args = []; | ||
|
||
node.arguments.forEach(arg => { | ||
if (t.isArgumentPlaceholder(arg)) { | ||
const id = scope.generateUid("_argPlaceholder"); | ||
placeholders.push(t.identifier(id)); | ||
args.push(t.identifier(id)); | ||
} else { | ||
args.push(arg); | ||
} | ||
}); | ||
return [placeholders, args]; | ||
} | ||
|
||
return { | ||
name: "proposal-partial-application", | ||
inherits: syntaxPartialApplication, | ||
|
||
visitor: { | ||
CallExpression(path) { | ||
if (!hasArgumentPlaceholder(path.node)) { | ||
return; | ||
} | ||
const { node, scope } = path; | ||
const functionLVal = path.scope.generateUidIdentifierBasedOnNode( | ||
node.callee, | ||
); | ||
|
||
const argsInitializers = unwrapArguments(node, scope); | ||
const [placeholdersParams, args] = replacePlaceholders(node, scope); | ||
|
||
scope.push({ id: functionLVal }); | ||
|
||
if (node.callee.type === "MemberExpression") { | ||
const receiverLVal = path.scope.generateUidIdentifierBasedOnNode( | ||
node.callee.object, | ||
); | ||
scope.push({ id: receiverLVal }); | ||
const finalExpression = t.sequenceExpression([ | ||
t.assignmentExpression( | ||
"=", | ||
t.cloneNode(receiverLVal), | ||
node.callee.object, | ||
), | ||
t.assignmentExpression( | ||
"=", | ||
t.cloneNode(functionLVal), | ||
t.memberExpression( | ||
receiverLVal, | ||
node.callee.property, | ||
false, | ||
false, | ||
), | ||
), | ||
...argsInitializers, | ||
t.functionExpression( | ||
node.callee.property, | ||
placeholdersParams, | ||
t.blockStatement( | ||
[ | ||
t.returnStatement( | ||
t.callExpression( | ||
t.memberExpression( | ||
functionLVal, | ||
t.identifier("call"), | ||
false, | ||
false, | ||
), | ||
[receiverLVal, ...args], | ||
), | ||
), | ||
], | ||
[], | ||
), | ||
false, | ||
false, | ||
), | ||
]); | ||
path.replaceWith(finalExpression); | ||
} else { | ||
const finalExpression = t.sequenceExpression([ | ||
t.assignmentExpression("=", t.cloneNode(functionLVal), node.callee), | ||
...argsInitializers, | ||
t.functionExpression( | ||
node.callee, | ||
placeholdersParams, | ||
t.blockStatement( | ||
[t.returnStatement(t.callExpression(functionLVal, args))], | ||
[], | ||
), | ||
false, | ||
false, | ||
), | ||
]); | ||
path.replaceWith(finalExpression); | ||
} | ||
}, | ||
}, | ||
}; | ||
}); |
16 changes: 16 additions & 0 deletions
16
...ages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Partial { | ||
constructor() { | ||
this.compare = this.compare(?, ?); | ||
} | ||
compare(a, b) { | ||
if(a > b){ | ||
return a; | ||
} | ||
} | ||
} | ||
|
||
const foo = new Partial; | ||
|
||
expect(foo.compare(3,1)).toEqual(3); | ||
expect(foo.compare.length).toEqual(2); | ||
expect(foo.compare.name).toEqual("compare"); |
10 changes: 10 additions & 0 deletions
10
...ges/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Collator { | ||
constructor() { | ||
this.compare = this.compare(?, ?); | ||
} | ||
compare(a, b) { | ||
if (a > b) { | ||
return a; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, but this semi seems unnecessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, unnecessary, I'll remove it. |
||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...es/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Collator { | ||
constructor() { | ||
var _this$compare, _this; | ||
|
||
this.compare = (_this = this, _this$compare = _this.compare, function compare(_argPlaceholder, _argPlaceholder2) { | ||
return _this$compare.call(_this, _argPlaceholder, _argPlaceholder2); | ||
}); | ||
} | ||
|
||
compare(a, b) { | ||
if (a > b) { | ||
return a; | ||
} | ||
|
||
; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
function add(x, y) { return x + y; } | ||
const addOne = add(1, ?); | ||
|
||
expect(addOne(2)).toEqual(3); | ||
expect(addOne.length).toEqual(1); | ||
expect(addOne.name).toEqual("add"); |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
|
||
const foo = bar(?, 1); |
7 changes: 7 additions & 0 deletions
7
packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
|
||
var _bar; | ||
|
||
const foo = (_bar = bar, function bar(_argPlaceholder) { | ||
return _bar(_argPlaceholder, 1); | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
function add(x, y) { return x + y; } | ||
const addTen = add(?, 10); | ||
|
||
expect(addTen(2)).toEqual(12); | ||
expect(addTen.length).toEqual(1); | ||
expect(addTen.name).toEqual("add"); |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
|
||
const foo = bar(1, ?); |
7 changes: 7 additions & 0 deletions
7
...ages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
|
||
var _bar; | ||
|
||
const foo = (_bar = bar, function bar(_argPlaceholder) { | ||
return _bar(1, _argPlaceholder); | ||
}); |
7 changes: 7 additions & 0 deletions
7
...babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
const obj = { add: (x, y) => x + y }; | ||
const addOne = obj.add(1, ?); | ||
|
||
expect(addOne(5)).toEqual(6); | ||
expect(addOne.length).toEqual(1); | ||
expect(addOne.name).toEqual("add"); |
7 changes: 7 additions & 0 deletions
7
...abel-plugin-proposal-partial-application/test/fixtures/general/member-expression/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
|
||
const g = o.f(?, x, 1); | ||
|
||
const h = p.b(1, y, x, 2, ?); | ||
|
||
const j = a.b.c.d.e.foo(?, 1); |
13 changes: 13 additions & 0 deletions
13
...bel-plugin-proposal-partial-application/test/fixtures/general/member-expression/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
|
||
var _x, _o$f, _o, _y, _x2, _p$b, _p, _a$b$c$d$e$foo, _a$b$c$d$e; | ||
|
||
const g = (_o = o, _o$f = _o.f, _x = x, function f(_argPlaceholder) { | ||
return _o$f.call(_o, _argPlaceholder, _x, 1); | ||
}); | ||
const h = (_p = p, _p$b = _p.b, _y = y, _x2 = x, function b(_argPlaceholder2) { | ||
return _p$b.call(_p, 1, _y, _x2, 2, _argPlaceholder2); | ||
}); | ||
const j = (_a$b$c$d$e = a.b.c.d.e, _a$b$c$d$e$foo = _a$b$c$d$e.foo, function foo(_argPlaceholder3) { | ||
return _a$b$c$d$e$foo.call(_a$b$c$d$e, _argPlaceholder3, 1); | ||
}); |
18 changes: 18 additions & 0 deletions
18
...es/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function add(a, b) { | ||
return a + b; | ||
} | ||
|
||
function square(x){ | ||
return x * x; | ||
} | ||
|
||
const foo = add(?, 1); | ||
const bar = square(?); | ||
|
||
expect(bar(foo(2))).toEqual(9); | ||
expect(foo(2)).toEqual(3); | ||
expect(bar(4)).toEqual(16); | ||
expect(foo.length).toEqual(1); | ||
expect(foo.name).toEqual("add"); | ||
expect(bar.length).toEqual(1); | ||
expect(bar.name).toEqual("square"); |
4 changes: 4 additions & 0 deletions
4
...s/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
|
||
const baz = ber(2, 4, ?, ?, 1); | ||
const foo = bar(1, ?, x, 3, ?); |
10 changes: 10 additions & 0 deletions
10
.../babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"use strict"; | ||
|
||
var _ber, _x, _bar; | ||
|
||
const baz = (_ber = ber, function ber(_argPlaceholder, _argPlaceholder2) { | ||
return _ber(2, 4, _argPlaceholder, _argPlaceholder2, 1); | ||
}); | ||
const foo = (_bar = bar, _x = x, function bar(_argPlaceholder3, _argPlaceholder4) { | ||
return _bar(1, _argPlaceholder3, _x, 3, _argPlaceholder4); | ||
}); |
7 changes: 7 additions & 0 deletions
7
...ages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
|
||
function add(x, y) { return x + y; } | ||
const addOne = add(1, ?); | ||
const addTen = add(?, 10); | ||
|
||
expect(addOne(addTen(1))).toEqual(12); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not big deal by any means, but since both of these end with replacing the path with a
sequenceExpression
, we could do:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea! made the necessary changes.