ramda: dead code elimination cause problems with _arity
Some of the dead code eliminators (like babel-plugin-minify-dead-code-elimination) remove arguments from function’s definition. Which cause problem with _arity.js
from:
case 2: return function(a0, a1) { return fn.apply(this, arguments); };
will become:
case 2: return function() { return fn.apply(this, arguments); };
and it will cause problems in combinations like uncurryN and compose because they are depending on the function.length returned from _arity.
I would like to open discussion if _arity can be more defensive. Because the function really depends on the dead code.
One of the possible solutions can be
const arity2 = function() { return fn.apply(this, arguments);
arity2.arity = 2;
return arity2;
And then it is possible to check a function length by specified arity (if not specified than fallback to length).
What do you think about that?
problematic example:
const lastFn = curry((firstArg, secondArg) => {
console.log("lastFn-firstArg", firstArg);
console.log("lastFn-secondArg", secondArg);
return "finish";
});
const firstFn = (firstArg) => {
console.log("firstFn-firstArg", firstArg); // there will be undefined when minified
return firstArg;
};
const incorrectBehaviour = uncurryN(2, compose(lastFn, firstFn));
you can play with this example at
https://github.com/aizerin/dead-code-issue-example
just type yarn and yarn start
I have encountered the same problem with less complicated and usual code, but I cannot remember now.
note: args elimination can be disabled in the plugin mentioned above.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (6 by maintainers)
It’s not an issue with Ramda - it’s an issue with
babel-plugin-minify-dead-code-elimination. To avoid it passkeepFnArgs: trueinto the plugin config.@Gregjarvez: I don’t see any way.
Technically the solution to this particular issue (with the babel plugin) is pass
keepFnArgs: trueinto the plugin configuration, but it would be nice if you didn’t have to.The choice isn’t ours to make. Ramda uses the length of any function supplied to it in a number of places, and the whole point of arity is to create functions that properly report this.
But probably the easiest fix would be to revert 07ecdac.