-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Flow lint for unnecessary optional chaining
Summary: This diff adds a Flow lint rule to flag unnecessary uses of optional chaining (`?.`) when a normal property access would have sufficed. To do this, the context maintains a map of optional chain locations which marks them as useful or not. In `flow_js`, whenever we see null/undefined flowing to an optional chain, we mark that optional chain as useful. Whenever we see anything else flowing to an optional chain, we mark it non-useful (combining with `||` so that once an optional chain is marked useful, it stays that way). At the end of `merge_js`, we simply extract the list of non-useful optional chains and emit a lint for each one. This approach is limited: consider ``` // flow class Foo { bar: Bar } class Bar { baz: number } declare var foo: ?Foo; (foo?.bar?.baz: number | void); ``` Note that the `?.` between `bar` and `baz` might as well just be `.` and make use of short-circuiting: after all, it's only `foo` that's nullable, not its `bar` field. However, no lint is produced for this example. Reviewed By: gabelevi Differential Revision: D7969730 fbshipit-source-id: 25959203c9751aead30aa38447c2f09e8a726537
- Loading branch information
1 parent
4bd6ee5
commit 3bfe2d4
Showing
15 changed files
with
499 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.