Skip to content

Commit

Permalink
docs(eslint-plugin): [no-shadow] add FAQ about enum members (#5986)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
3 people authored Nov 16, 2022
1 parent dd9b3fa commit ac7669e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/eslint-plugin/docs/rules/no-shadow.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,24 @@ Examples of **correct** code with `{ ignoreFunctionTypeParameterNameValueShadow:
const test = 1;
type Func = (test: string) => typeof test;
```

## FAQ

### Why does the rule report on enum members that share the same name as a variable in a parent scope?

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.

To illustrate this with an example:

```ts
const A = 2;
enum Test {
A = 1,
B = A,
}

console.log(Test.B);
// what should be logged?
```

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 comments on commit ac7669e

Please sign in to comment.