question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent variables from being used if not guaranteed initialization.

See original GitHub issue

What 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Nov 17, 2019

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.

1reaction
ilyavolodincommented, Nov 17, 2019

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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found