google / google/closure-compiler

Warn on [] lookup on enums

Open
#1,667 0 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

Currently, you can make this mistake of doing something like:

``` js
/**
* @enum {number}
*/
some.ns.THING = {
FOO: 1
}
```

and try to do:

``` js
var someString = // get some string from somewhere and the value is FOO
var value = some.ns.THING[someString];

// think value is 1 and try to use it as such
```

As a result, value ends up being undefined when the compiler renames things. If a problem with the undefined value happens down the line, it can be hard to track down that the enum lookup shouldn't have been done.

It would be really nice if `[]` lookup could warn (or error) on things that are known to be enums. I can't really think of a good case where you would capture the enum keys (e.g. via `Object.keys`) and try to use them later to get the actual enum value.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.