question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: "function-default-params" scope

See original GitHub issue

From #33, eslint/eslint#9335.

As the step 27.a of 9.2.12 FunctionDeclarationInstantiation( func, argumentsList ), eslint-scope should make new scope for the function body only if the parameters has any default value.

It’s similar to function-expression-name scope which separates the variable of function expression names from function bodies. So I propose new function-default-params scope which separates the references in default parameters from function bodies.

  1. If the current function has one or more default parameters in traversing, the referencer defines a function-default-params scope instead of function scope.
    • Note: the function-default-params scope has implicit arguments variable except if the function is arrow function expression.
    • Note: the function-default-params scope is the child scope of function-expression-name scope if the function is function expression.
  2. The referencer evaluates the parameters.
  3. The referencer defines function scope.
  4. The referencer copies the all variables from the function-default-params scope to the function scope.
    • Note: those copies handle redeclaration such as function f(a = 1) { let a } correctly.
    • Note: at least, no-shadow rule has to handle those copies as special.
    • Note: I think we should use shallow copy here because Variable#defs and Variable#references should be shared with both scopes but Reference#resolved property cannot have multiple variable objects. For example, in function f(a, b = a) { g(a) } case, the two references of a should be aggregated into one Variable#references array.
  5. The referencer evaluates the function body.
  6. The referencer closes both function-default-params and function scopes at the end of the function.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mysticateacommented, Jun 27, 2019

@kaicataldo No. The #33 was a superficial fix because I avoided breaking changes. And this is a follow‐up issue to make the correct scope structure.

But probably I should write an RFC.

1reaction
not-an-aardvarkcommented, Jun 26, 2019

I don’t think this issue has been fixed. If I’m understanding correctly, #33 fixed https://github.com/estools/escope/issues/130, which is a separate issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default parameters - JavaScript - MDN Web Docs
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
Read more >
JS: Default function parameter values and scope
I'm a bit confused about how scope & variable assignment within a function seems to change when assigning a default parameter value for...
Read more >
Proposal to change the default ownership for passing parameters ...
Currently the calling convention for non-trivial parameters in swift is that the caller transfers ownership to the callee. In terms of reference counting, ......
Read more >
Understanding Default Parameters in JavaScript - DigitalOcean
Function Calls as Default Parameters​​ You can even use built-in methods, like those on the Math object, and use the value returned in...
Read more >
Default Parameters - Learn JavaScript: Functions and Scope
Default parameters allow parameters to have a predetermined value in case there is no argument passed into the function or if the argument...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found