Skip to content

Commit

Permalink
fix(transform-fast-rest): improved the arguments scope check
Browse files Browse the repository at this point in the history
  • Loading branch information
gdorsi authored and Guido D'Orsi committed Feb 3, 2021
1 parent 259d13f commit ab39334
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
15 changes: 10 additions & 5 deletions src/lib/transform-fast-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export default function fastRestTransform({ template, types: t }) {
if (
binding.constant &&
binding.referencePaths.length === 1 &&
// arguments access requires the same function scope
getParentFunction(binding.referencePaths[0], t) === func
sameArgumentsObject(binding.referencePaths[0], func, t)
) {
// one usage, never assigned - replace usage inline
binding.referencePaths[0].replaceWith(sliced);
Expand Down Expand Up @@ -94,10 +93,16 @@ export default function fastRestTransform({ template, types: t }) {
};
}

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

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

return false;
}
18 changes: 9 additions & 9 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2366,30 +2366,30 @@ parameters-rest-closure
Build \\"parametersRestClosure\\" to dist:
126 B: parameters-rest-closure.js.gz
85 B: parameters-rest-closure.js.br
131 B: parameters-rest-closure.esm.js.gz
98 B: parameters-rest-closure.esm.js.br
218 B: parameters-rest-closure.umd.js.gz
155 B: parameters-rest-closure.umd.js.br"
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)}}export{n as parametersRestWithClosure};
"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(e){var r=[].slice.call(arguments,1);return function(){e.apply(void 0,r)}};
"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,t){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?t(exports):\\"function\\"==typeof define&&define.amd?define([\\"exports\\"],t):t((e||self).parametersRestClosure={})}(this,function(e){e.parametersRestWithClosure=function(e){var t=[].slice.call(arguments,1);return function(){e.apply(void 0,t)}}});
"!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
"
`;
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/parameters-rest-closure/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export function parametersRestWithClosure(fn, ...args) {
fn(...args);
};
}

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

0 comments on commit ab39334

Please sign in to comment.