-
-
Notifications
You must be signed in to change notification settings - Fork 30
Fix: wrong resolution about default parameters #33
Conversation
[This code](https://github.com/eslint/eslint-scope/blob/57889f1b7f9602566f73f5ccbb5cd78a64fd8caa/tests/es6-destructuring-assignments.js#L571) is syntax error in ES2015. It's valid since ES2016.
@mysticatea Can you explain a bit more about that new scope? functions already create their own scope, would that be another extra scope on top of that? Why is that necessary? |
@ilyavolodin That new scope separates the variables in the body from default parameters. For example: let x = 1
function foo(a = x, b = y) {
let x = 1, y = 2
} // Scopes
{
type: "global",
variables: ["x", "foo"],
references: [],
childScopes: [
{
type: "function",
variables: ["arguments", "a", "b"],
references: ["x", "y"], // This `x` comes from the global scope. This `y` is not defined.
childScopes: [
//
// Ideally, requires this function-body scope only if the function has default parameters.
// This scope separates the variables in the body from default parameters.
//
// EDIT: This `function-body`scope has the variables which are copied from `function` scope.
// Those copied variables prevent redeclarations of parameters.
// `No-shadow` rule should not report the copied variables.
//
{
type: "functionBody",
variables: ["arguments", "a", "b", "x", "y"],
references: [],
childScopes: [],
},
],
},
],
} If the // Scopes
{
type: "global",
variables: ["x", "foo"],
references: [],
childScopes: [
{
type: "function",
variables: ["arguments", "a", "b", "x", "y"],
references: ["x", "y"], // These `x` and `y` comes from this scope because it's defined.
childScopes: [],
},
],
} |
Thanks for the explanation! I see how this might result in a lot of breaking changes. Could we do it in the next major version? I'd prefer to create scopes as appropriate based on the specifications. We can merge this in now, but create another issue to introduce extra scope for the default parameters in the next major version of ESLint. |
Sure. I can. |
Do you think we should merge this in now, and do a correct fix in the next major version of ESLint? Keep in mind, we are probably about half-a-year away from the next major release. |
I'm happy if this is merged and I make the breaking change when the next major release. As a side note, I'd like to make one more change in the next major release: removing |
Fixes eslint/eslint#9335.
This PR fixes wrong resolutions about the references in default parameters. In the following case:
After this PR, the
a
in default parameters is resolved to (A) correctly.This fixing is not ideal.
As the step 27.a of 9.2.12 FunctionDeclarationInstantiation( func, argumentsList ) mentioned, it should make new scope for the function body if the parameters has any default value. However, I guessed that it's a huge breaking change which breaks many ESLint rules. So I kept scope structure and modified the resolution logic.