eslint-plugin-proper-arrows
eslint-plugin-proper-arrows copied to clipboard
Add rule to forbid inline function expressions nested in default parameter value
When an inline function expression appears in a default parameter value, it can create a closure over the normally-indistinguishable "parameter scope", which is super confusing, and should almost always be avoided.
For example:
var f = (x,cb = () => x) => { var x = 2; return [x,cb()]; };
f(5); // [2,5]
The x parameter is in a separate scope from the var x in the function body, and in this case they are shown to have different values, via the closure.
For more info: https://gist.github.com/getify/0978136c0c66c0357f611e2c7233f105
This is confusing enough for regular functions, but it's significantly more confusing for arrow functions.
This rule would forbid inline function expressions in an arrow function's parameter default value position. It will have 3 modes:
- "all" - forbid all inline function expressions (arrow or normal)
- "closure" - only forbid inline function expressions that actually close over a parameter in the parameter scope
- "none" - disable the rule
There would also be two additional flags (both default to true), in effect only for "all" and "closure" modes:
- "arrow": forbids inline arrow function expressions
- "function": forbids inline regular function expressions
NOTE: I think this may be better as its own standalone plugin/rule, instead of only applying to arrow functions (in this "proper-arrows" plugin).