Open Bug 1936079 Opened 2 months ago Updated 2 months ago

Ignore ESLint rule no-shadow on initialization

Categories

(Developer Infrastructure :: Lint and Formatting, task)

Tracking

(Not tracked)

People

(Reporter: nordzilla, Unassigned)

References

(Depends on 1 open bug)

Details

Description

Bug 1881496 introduced the no-shadow ESLint rule globally.
I have encountered an unfortunate use case where this rule thinks that variables which are yet to be initialized are able to be shadowed.

Example

(ESLint Playground)

/*eslint no-shadow: ["error", { "ignoreOnInitialization": false }]*/

const objects = [
  { name: "Foo", value: 0 },
  { name: "Bar", value: 1 },
  { name: "Baz", value: 2 },
];

const { name, value } = objects.find(({ name }) => name === "Bar");

if (name && value) {
  console.log(`My name is ${name} and my value is ${value}`);
}

ESLint Output

Error: 'name' is already declared in the upper scope on line 9 column 9.  (no-shadow)

ESLint thinks that variable name, which is yet to be initialized, is shadowed by the anonymous closure's argument name, when the reality is that the outer variable is not accessible within the scope at all.

Recommendation

We should enable the no-shadow configuration option for ignoreOnInitialization.

"ignoreOnInitialization": true

:standard8, would you be okay with enabling this?

Flags: needinfo?(standard8)

I think this would be reasonable to change. Dave/Gijs any objections?

Flags: needinfo?(gijskruitbosch+bugs)
Flags: needinfo?(dtownsend)
Flags: needinfo?(standard8)

Sure.

Flags: needinfo?(gijskruitbosch+bugs)

SGTM

Flags: needinfo?(dtownsend)
You need to log in before you can comment on or make changes to this bug.