Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule to forbid inline function expressions nested in default parameter value #8

Open
getify opened this Issue Mar 14, 2019 · 1 comment

Comments

Projects
None yet
1 participant
@getify
Copy link
Owner

getify commented Mar 14, 2019

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
@getify

This comment has been minimized.

Copy link
Owner Author

getify commented Mar 14, 2019

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).

@getify getify added the enhancement label Mar 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.