Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Disable no-var autofixer in some incorrect cases in loops
The autofixer for the no-var rule was converting `var` to `let` within loops, but in some cases that can introduce incorrect behavior because `let` variables in loops only live for the lifetime of their loop iteration, while `var` variables within loops use the same variable across all iterations. We can still convert to `let` in typical cases as long as we check for cases that might cause a behavior difference: * If the variable is referenced from a closure, then that closure might be called after the current loop iteration ends. For `var` declarations, the closure refers to the shared variable across all iterations, and for `let` declarations, the closure refers to the variable just from that one iteration. * If the variable is used before its first assignment in the loop body, then for `var` declarations it will retain its value from the previous iteration, but for `let` declarations it will start as undefined. This change skips the autofixer for any variables referenced by any closure, and for any variables that are not initialized right when they are declared. Some additional static analysis can make both of these cases smarter, but this should handle most common cases.
- Loading branch information
1 parent
3004c1e
commit 427394a
Showing
6 changed files
with
198 additions
and
81 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