Skip to content

Commit ac7669e

Browse files
bradzacherJosh-CenaJoshuaKGoldberg
authored
docs(eslint-plugin): [no-shadow] add FAQ about enum members (#5986)
Co-authored-by: Joshua Chen <[email protected]> Co-authored-by: Josh Goldberg <[email protected]>
1 parent dd9b3fa commit ac7669e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: packages/eslint-plugin/docs/rules/no-shadow.md

+21
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,24 @@ Examples of **correct** code with `{ ignoreFunctionTypeParameterNameValueShadow:
7878
const test = 1;
7979
type Func = (test: string) => typeof test;
8080
```
81+
82+
## FAQ
83+
84+
### Why does the rule report on enum members that share the same name as a variable in a parent scope?
85+
86+
Reporting on this case isn't a bug - it is completely intentional and correct reporting! The rule reports due to a relatively unknown feature of enums - enum members create a variable within the enum scope so that they can be referenced within the enum without a qualifier.
87+
88+
To illustrate this with an example:
89+
90+
```ts
91+
const A = 2;
92+
enum Test {
93+
A = 1,
94+
B = A,
95+
}
96+
97+
console.log(Test.B);
98+
// what should be logged?
99+
```
100+
101+
Naively looking at the above code, it might look like the log should output `2`, because the outer variable `A`'s value is `2` - however, the code instead outputs `1`, which is the value of `Test.A`. This is because the unqualified code `B = A` is equivalent to the fully-qualified code `B = Test.A`. Due to this behavior, the enum member has **shadowed** the outer variable declaration.

0 commit comments

Comments
 (0)