diff --git a/packages/babel-parser/test/fixtures/experimental/partial-application/call-expr/output.json b/packages/babel-parser/test/fixtures/experimental/partial-application/call-expr/output.json index 1846d675488a..ac7ce3111be1 100644 --- a/packages/babel-parser/test/fixtures/experimental/partial-application/call-expr/output.json +++ b/packages/babel-parser/test/fixtures/experimental/partial-application/call-expr/output.json @@ -96,4 +96,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/partial-application/call-on-SuperProperty/output.json b/packages/babel-parser/test/fixtures/experimental/partial-application/call-on-SuperProperty/output.json index 185313af1242..39bdbf743956 100644 --- a/packages/babel-parser/test/fixtures/experimental/partial-application/call-on-SuperProperty/output.json +++ b/packages/babel-parser/test/fixtures/experimental/partial-application/call-on-SuperProperty/output.json @@ -302,4 +302,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/partial-application/for-any-arg/output.json b/packages/babel-parser/test/fixtures/experimental/partial-application/for-any-arg/output.json index 29f5266444a5..61338f165056 100644 --- a/packages/babel-parser/test/fixtures/experimental/partial-application/for-any-arg/output.json +++ b/packages/babel-parser/test/fixtures/experimental/partial-application/for-any-arg/output.json @@ -257,4 +257,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/partial-application/from-left/output.json b/packages/babel-parser/test/fixtures/experimental/partial-application/from-left/output.json index d90ab651eebf..59529de050d1 100644 --- a/packages/babel-parser/test/fixtures/experimental/partial-application/from-left/output.json +++ b/packages/babel-parser/test/fixtures/experimental/partial-application/from-left/output.json @@ -227,4 +227,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/experimental/partial-application/from-right/output.json b/packages/babel-parser/test/fixtures/experimental/partial-application/from-right/output.json index ec82545ec334..3b558a170e5d 100644 --- a/packages/babel-parser/test/fixtures/experimental/partial-application/from-right/output.json +++ b/packages/babel-parser/test/fixtures/experimental/partial-application/from-right/output.json @@ -227,4 +227,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/packages/babel-plugin-proposal-partial-application/.npmignore b/packages/babel-plugin-proposal-partial-application/.npmignore new file mode 100644 index 000000000000..f9806945836e --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/.npmignore @@ -0,0 +1,3 @@ +src +test +*.log diff --git a/packages/babel-plugin-proposal-partial-application/README.md b/packages/babel-plugin-proposal-partial-application/README.md new file mode 100644 index 000000000000..5a31663cf72f --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/README.md @@ -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 +``` diff --git a/packages/babel-plugin-proposal-partial-application/package.json b/packages/babel-plugin-proposal-partial-application/package.json new file mode 100644 index 000000000000..52ee50ef9414 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/package.json @@ -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" + } +} diff --git a/packages/babel-plugin-proposal-partial-application/src/index.js b/packages/babel-plugin-proposal-partial-application/src/index.js new file mode 100644 index 000000000000..8bd32c72c6f7 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/src/index.js @@ -0,0 +1,150 @@ +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 sequenceParts = []; + 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 }); + sequenceParts.push( + 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, + ), + ); + } else { + sequenceParts.push( + 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(t.sequenceExpression(sequenceParts)); + }, + }, + }; +}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/exec.js new file mode 100644 index 000000000000..56f065b30939 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/exec.js @@ -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"); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/input.js new file mode 100644 index 000000000000..b6e5de8dcb0c --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/input.js @@ -0,0 +1,5 @@ +class Partial { + constructor() { + this.compare = this.compare(?, ?); + } +} diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/output.js new file mode 100644 index 000000000000..24a5f382898c --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/bound-method/output.js @@ -0,0 +1,10 @@ +class Partial { + constructor() { + var _this$compare, _this; + + this.compare = (_this = this, _this$compare = _this.compare, function compare(_argPlaceholder, _argPlaceholder2) { + return _this$compare.call(_this, _argPlaceholder, _argPlaceholder2); + }); + } + +} diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/exec.js new file mode 100644 index 000000000000..6a7a47109f60 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/exec.js @@ -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"); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/input.js new file mode 100644 index 000000000000..fd206f710677 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/input.js @@ -0,0 +1,3 @@ +"use strict"; + +const foo = bar(?, 1); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/output.js new file mode 100644 index 000000000000..8d7e15b9bb55 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-left/output.js @@ -0,0 +1,7 @@ +"use strict"; + +var _bar; + +const foo = (_bar = bar, function bar(_argPlaceholder) { + return _bar(_argPlaceholder, 1); +}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/exec.js new file mode 100644 index 000000000000..173f71479e58 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/exec.js @@ -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"); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/input.js new file mode 100644 index 000000000000..963267eb5f60 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/input.js @@ -0,0 +1,3 @@ +"use strict"; + +const foo = bar(1, ?); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/output.js new file mode 100644 index 000000000000..ec30cf79adb6 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/from-right/output.js @@ -0,0 +1,7 @@ +"use strict"; + +var _bar; + +const foo = (_bar = bar, function bar(_argPlaceholder) { + return _bar(1, _argPlaceholder); +}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/exec.js new file mode 100644 index 000000000000..cfd4c848a88c --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/exec.js @@ -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"); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/input.js new file mode 100644 index 000000000000..e0bac6ec4032 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/input.js @@ -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); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/output.js new file mode 100644 index 000000000000..32027e6ed7cc --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/member-expression/output.js @@ -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); +}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/exec.js new file mode 100644 index 000000000000..02dd85661d71 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/exec.js @@ -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"); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/input.js new file mode 100644 index 000000000000..034235567b0c --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/input.js @@ -0,0 +1,4 @@ +"use strict"; + +const baz = ber(2, 4, ?, ?, 1); +const foo = bar(1, ?, x, 3, ?); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/output.js new file mode 100644 index 000000000000..3a4c01906ab6 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/multiple-calls/output.js @@ -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); +}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/exec.js new file mode 100644 index 000000000000..71e084d6f7e6 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/exec.js @@ -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); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/input.js new file mode 100644 index 000000000000..214217d61430 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/input.js @@ -0,0 +1,3 @@ +"use strict"; + +const foo = bar(baz(?, 1)); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/output.js new file mode 100644 index 000000000000..b1b24f5cffbf --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-calls/output.js @@ -0,0 +1,7 @@ +"use strict"; + +var _baz; + +const foo = bar((_baz = baz, function baz(_argPlaceholder) { + return _baz(_argPlaceholder, 1); +})); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-member-expression/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-member-expression/exec.js new file mode 100644 index 000000000000..b74177523adb --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-member-expression/exec.js @@ -0,0 +1,9 @@ +"use strict"; + +const obj = {a : {b : {c : {add: (x, y) => x + y}}}}; + +const addOne = obj.a.b.c.add(1, ?); + +expect(addOne(4)).toEqual(5); +expect(addOne.length).toEqual(1); +expect(addOne.name).toEqual("add"); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-member-expression/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-member-expression/input.js new file mode 100644 index 000000000000..0a17052ff441 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-member-expression/input.js @@ -0,0 +1,3 @@ +"use strict"; + +const g = a.b.fn().c["d"].e(?, 1, ?, 3, x, y, z); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-member-expression/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-member-expression/output.js new file mode 100644 index 000000000000..353c685720a1 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/nested-member-expression/output.js @@ -0,0 +1,7 @@ +"use strict"; + +var _x, _y, _z, _a$b$fn$c$d$e, _a$b$fn$c$d; + +const g = (_a$b$fn$c$d = a.b.fn().c["d"], _a$b$fn$c$d$e = _a$b$fn$c$d.e, _x = x, _y = y, _z = z, function e(_argPlaceholder, _argPlaceholder2) { + return _a$b$fn$c$d$e.call(_a$b$fn$c$d, _argPlaceholder, 1, _argPlaceholder2, 3, _x, _y, _z); +}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/not-assigned-and-chained/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/not-assigned-and-chained/exec.js new file mode 100644 index 000000000000..71b3cc3ea3d3 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/not-assigned-and-chained/exec.js @@ -0,0 +1,7 @@ +"use strict"; + +function square(n){ + return new Promise(resolve => resolve(n * n)); +} + +square(?)(3).then(res => expect(res).toBe(9)); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/not-assigned-and-chained/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/not-assigned-and-chained/input.js new file mode 100644 index 000000000000..20c9fa88f034 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/not-assigned-and-chained/input.js @@ -0,0 +1,3 @@ +"use strict"; + +foo(?)(b).then(() => {}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/not-assigned-and-chained/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/not-assigned-and-chained/output.js new file mode 100644 index 000000000000..69369d1a752b --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/not-assigned-and-chained/output.js @@ -0,0 +1,7 @@ +"use strict"; + +var _foo; + +(_foo = foo, function foo(_argPlaceholder) { + return _foo(_argPlaceholder); +})(b).then(() => {}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/options.json b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/options.json new file mode 100644 index 000000000000..3ad392344831 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["proposal-partial-application"] +} diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/simple-call/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/simple-call/exec.js new file mode 100644 index 000000000000..ae63040ce976 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/simple-call/exec.js @@ -0,0 +1,11 @@ +"use strict"; + +function square(x){ + return x * x; +} + +const foo = square(?); + +expect(foo(3)).toBe(9); +expect(foo.length).toEqual(1); +expect(foo.name).toEqual("square"); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/simple-call/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/simple-call/input.js new file mode 100644 index 000000000000..e77a8e83a28e --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/simple-call/input.js @@ -0,0 +1,3 @@ +"use strict"; + +const foo = bar(?); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/simple-call/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/simple-call/output.js new file mode 100644 index 000000000000..7b3e1b488824 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/simple-call/output.js @@ -0,0 +1,7 @@ +"use strict"; + +var _bar; + +const foo = (_bar = bar, function bar(_argPlaceholder) { + return _bar(_argPlaceholder); +}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/uncurry-this/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/uncurry-this/exec.js new file mode 100644 index 000000000000..a6656129a76b --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/uncurry-this/exec.js @@ -0,0 +1,7 @@ +"use strict"; + +const slice = Array.prototype.slice.call(?, ?, ?); + +expect(slice({ 0: "a", 1: "b", length: 2 }, 1, 2)).toEqual(["b"]); +expect(slice.length).toEqual(3); +expect(slice.name).toEqual("call"); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/uncurry-this/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/uncurry-this/input.js new file mode 100644 index 000000000000..696426b744a0 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/uncurry-this/input.js @@ -0,0 +1 @@ +const slice = Array.prototype.slice.call(?, ?, ?); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/uncurry-this/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/uncurry-this/output.js new file mode 100644 index 000000000000..0099cf0db61b --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/uncurry-this/output.js @@ -0,0 +1,5 @@ +var _Array$prototype$slic, _Array$prototype$slic2; + +const slice = (_Array$prototype$slic2 = Array.prototype.slice, _Array$prototype$slic = _Array$prototype$slic2.call, function call(_argPlaceholder, _argPlaceholder2, _argPlaceholder3) { + return _Array$prototype$slic.call(_Array$prototype$slic2, _argPlaceholder, _argPlaceholder2, _argPlaceholder3); +}); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/with-spread/exec.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/with-spread/exec.js new file mode 100644 index 000000000000..9ba70df2a91a --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/with-spread/exec.js @@ -0,0 +1,13 @@ +"use strict"; + +const num = [2,3]; + +function multiplyXYZ(x, y, z){ + return x * y * z; +} + +const multiplyBySix = multiplyXYZ(?, ...num); + +expect(multiplyBySix(6)).toBe(36); +expect(multiplyBySix.length).toEqual(1); +expect(multiplyBySix.name).toEqual("multiplyXYZ"); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/with-spread/input.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/with-spread/input.js new file mode 100644 index 000000000000..dca891154318 --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/with-spread/input.js @@ -0,0 +1,7 @@ +"use strict"; + +const q = foo(a, ...b, ?); + +const w = bar(1, x, ?, ...y, ?, 2); + +const z = obj.baz(?, ...d); diff --git a/packages/babel-plugin-proposal-partial-application/test/fixtures/general/with-spread/output.js b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/with-spread/output.js new file mode 100644 index 000000000000..9f4f7ab6f99b --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/fixtures/general/with-spread/output.js @@ -0,0 +1,13 @@ +"use strict"; + +var _a, _param, _foo, _x, _param2, _bar, _param3, _obj$baz, _obj; + +const q = (_foo = foo, _a = a, _param = [...b], function foo(_argPlaceholder) { + return _foo(_a, ..._param, _argPlaceholder); +}); +const w = (_bar = bar, _x = x, _param2 = [...y], function bar(_argPlaceholder2, _argPlaceholder3) { + return _bar(1, _x, _argPlaceholder2, ..._param2, _argPlaceholder3, 2); +}); +const z = (_obj = obj, _obj$baz = _obj.baz, _param3 = [...d], function baz(_argPlaceholder4) { + return _obj$baz.call(_obj, _argPlaceholder4, ..._param3); +}); diff --git a/packages/babel-plugin-proposal-partial-application/test/index.js b/packages/babel-plugin-proposal-partial-application/test/index.js new file mode 100644 index 000000000000..1b534b8fc64a --- /dev/null +++ b/packages/babel-plugin-proposal-partial-application/test/index.js @@ -0,0 +1,3 @@ +import runner from "@babel/helper-plugin-test-runner"; + +runner(__dirname);