Why would ESLint or any other linter for that matter, throw an error for this ? it does not seem like just a ‘strict’ rule, but more like it does not make any sense. how can it not distinguish between a variable declaration and a function parameter?
Here is the code:
const name = "Paul";
// The scope of name is too tight
const logEvent = (name, event) => {
console.log(
`${name}'s event is: ${event}.`,
"\n"
);
};
It’s warning about exactly what you’re doing: the name
parameter is shadowing the name
in the outer scope. If this is what you intend to do, then disable the warning for that line. If you’re running into this problem a lot, you might want to limit your scopes more or shadow your variables less.
Or if your code style shadows names in the same scope a lot , just disable the warning in the config. I do recommend not doing a whole lot of shadowing for names in the outer scope simply because it can be confusing to the reader, even if the compiler is completely clear.
1 Like