Prevent variables from being used if not guaranteed initialization.
See original GitHub issueWhat rule do you want to change? https://eslint.org/docs/rules/init-declarations It may be it’s own rule because I couldn’t really find a good fit for this.
Does this change cause the rule to produce more or fewer warnings? More
How will the change be implemented? (New option, new default behavior, etc.)? New option
Please provide some example code that this change will affect:
Bad: abc may be left uninitialized before used.
function foo() {
let abc;
if (returnsTrue()) {
abc = 123;
}
useIt(abc); // Oops, abc is used but may be uninitialized. Possible error?
}
foo();
Good: abc is guaranteed to be initialized when used.
function foo() {
let abc;
if (returnsTrue()) {
abc = 123;
} else {
return; // this return will make sure abc is initialized before use
}
useIt(abc);
}
foo();
What does the rule currently do for this code? Nothing.
What will the rule do after it’s changed? It will prevent variables from being used if it’s not guaranteed that they have been initialized.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Guarantee of deferred dynamic initialization of non odr-used ...
Question: is there any guarantee that _ is initialized at some point (I do not care when)?. Now consider the program consisting of...
Read more >Avoiding late variables in Dart - Flutter Senior - Medium
Your program needs to know if a late variable has been initialized or not. For this, the compiler often creates a hidden internal...
Read more >C++ - Initialization of Static Variables - Pablo Arias
One big problem with static variable initialization is that it is not always clear if a variable is being initialized at compile time...
Read more >Initialization - cppreference.com
After all static initialization is completed, dynamic initialization of non-local variables occurs in the following situations:.
Read more >foonathan/atum: Helpers for preventing the static initialization ...
If you're using a nifty initialized global A in the constructor of some variable template or static data member of a template, it...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There is a recently accepted proposal for
no-unassigned-vars
rule in #12497, with the ongoing discussion about its scope, which may include code path analysis. It looks like the suggested enhancements in the discussion there are same as this proposal.I think this rule is getting into a realm of typing (flow/typescript). I think those tools would be better to cover something like this, because ESLint will never be able to check this fully without going through all function calls (which we don’t do).