Skip to content

Commit

Permalink
fix(transform-fast-rest): rest parameters references with closures (#798
Browse files Browse the repository at this point in the history
)

* fix(transform-fast-rest): rest parameters references with closures

* fix(transform-fast-rest): improved the arguments scope check
  • Loading branch information
gdorsi authored May 5, 2021
1 parent 2c9eaa1 commit 7426218
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/lib/transform-fast-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export default function fastRestTransform({ template, types: t }) {
func.arrowFunctionToExpression();
}

if (binding.constant && binding.referencePaths.length === 1) {
if (
binding.constant &&
binding.referencePaths.length === 1 &&
sameArgumentsObject(binding.referencePaths[0], func, t)
) {
// one usage, never assigned - replace usage inline
binding.referencePaths[0].replaceWith(sliced);
} else {
Expand Down Expand Up @@ -88,3 +92,17 @@ export default function fastRestTransform({ template, types: t }) {
},
};
}

function sameArgumentsObject(node, func, t) {
while ((node = node.parentPath)) {
if (node === func) {
return true;
}

if (t.isFunction(node) && !t.isArrowFunctionExpression(node)) {
return false;
}
}

return false;
}
47 changes: 47 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,53 @@ exports[`fixtures build optional-chaining-ts with microbundle 6`] = `
"
`;
exports[`fixtures build parameters-rest-closure with microbundle 1`] = `
"Used script: microbundle
Directory tree:
parameters-rest-closure
dist
parameters-rest-closure.esm.js
parameters-rest-closure.esm.js.map
parameters-rest-closure.js
parameters-rest-closure.js.map
parameters-rest-closure.umd.js
parameters-rest-closure.umd.js.map
package.json
src
index.js
Build \\"parametersRestClosure\\" to dist:
157 B: parameters-rest-closure.js.gz
115 B: parameters-rest-closure.js.br
165 B: parameters-rest-closure.esm.js.gz
124 B: parameters-rest-closure.esm.js.br
247 B: parameters-rest-closure.umd.js.gz
189 B: parameters-rest-closure.umd.js.br"
`;
exports[`fixtures build parameters-rest-closure with microbundle 2`] = `6`;
exports[`fixtures build parameters-rest-closure with microbundle 3`] = `
"function n(n){var c=[].slice.call(arguments,1);return function(){n.apply(void 0,c)}}function c(n){var c=arguments;return function(){n.apply(void 0,[].slice.call(c,1))}}export{n as parametersRestWithClosure,c as parametersRestWithInnerArrowFunction};
//# sourceMappingURL=parameters-rest-closure.esm.js.map
"
`;
exports[`fixtures build parameters-rest-closure with microbundle 4`] = `
"exports.parametersRestWithClosure=function(r){var t=[].slice.call(arguments,1);return function(){r.apply(void 0,t)}},exports.parametersRestWithInnerArrowFunction=function(r){var t=arguments;return function(){r.apply(void 0,[].slice.call(t,1))}};
//# sourceMappingURL=parameters-rest-closure.js.map
"
`;
exports[`fixtures build parameters-rest-closure with microbundle 5`] = `
"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?n(exports):\\"function\\"==typeof define&&define.amd?define([\\"exports\\"],n):n((e||self).parametersRestClosure={})}(this,function(e){e.parametersRestWithClosure=function(e){var n=[].slice.call(arguments,1);return function(){e.apply(void 0,n)}},e.parametersRestWithInnerArrowFunction=function(e){var n=arguments;return function(){e.apply(void 0,[].slice.call(n,1))}}});
//# sourceMappingURL=parameters-rest-closure.umd.js.map
"
`;
exports[`fixtures build pretty with microbundle 1`] = `
"Used script: microbundle
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/parameters-rest-closure/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "parameters-rest-closure"
}
11 changes: 11 additions & 0 deletions test/fixtures/parameters-rest-closure/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function parametersRestWithClosure(fn, ...args) {
return function () {
fn(...args);
};
}

export function parametersRestWithInnerArrowFunction(fn, ...args) {
return () => {
fn(...args);
};
}

0 comments on commit 7426218

Please sign in to comment.