mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
796080f4d1797ba2819b736e1e2ce2cfb6512d98
The [temporal dead zone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#temporal_dead_zone_tdz), often abbreviated TDZ, is the period between the start of its declaring block and the line that contains the let/const/class declaration. Not all instances of TDZ can be detected statically, because whether or not a TDZ error will occur at runtime is a property of which control flow path is taken and whether some other code has initialized the value. However, a subset of cases can be detected, and that's what we implement here. When we encounter a variable reference we record the next declaration id at that point in time. Then when resolving references after visiting the program, we can check: did the reference end up referring to a let/const/class binding whose id is equal or greater to that "next declaration"? If so, it means the reference refers to a variable that is provably declared later and is a known TDZ violation. The catch is that when resolving references, we reset the "next declaration" limit value when we bubble up out of a function scope. That's because references to let/const within a function may occur after the declaration, and we can't statically validate them.
Description
Languages
JavaScript
67.1%
TypeScript
29.4%
HTML
1.5%
CSS
1.1%
C++
0.6%
Other
0.2%