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.

Temporal Dead Zone variable usage, new-rule/rule-change

See original GitHub issue

I was a little surprised to find that usage of a variable within its temporal dead zone (henceforth “TDZ”) is not considered to be an error in the eslint:recommended ruleset.

Example of TDZ:

{            // <-- block scope, not function scope
    foo;     // <--+
             //    |-- TDZ for `foo`
             // <--+
    let foo; // <-- ReferenceError: foo is not defined
}

Looking back in the archives I see a couple of relevant closed Issues:

  • #10375: concluded that no-use-before-define is sufficient
  • #9677: timed out

I think this space would benefit from either a new rule, or some adjustment. The rest of this post outlines the existing situation and possible solutions. Feedback welcomed!


Existing rule, no-use-before-define

This rule is not enabled by the ruleset eslint:recommended.

The rule can catch TDZ usage, see this ESLint demo.

However, it also catches similar var usage, at which point we have ventured into subjective territory.

It is possible that people wish to use var hoisting in this manner. It is very unlikely that people want to use TDZ ReferenceError as part of control flow.

Proposal(s)

(1) New Rule, e.g. no-temporal-dead-zone-usage

A new rule could be created, and I hope included in the eslint:recommended ruleset to benefit the masses.

It would notify users of situations where a let/const variable is referenced before its declaration.

(2) Extend existing no-use-before-define

Parameters could be added to the existing rule to support this use case. I haven’t fleshed out what these might look like, possibly an additional property. Would have to consider how folks might enable the TDZ checking without also enabling var hoisting errors.

I would hope that the TDZ checking could be made part of eslint:recommended via this avenue too, though this may be trickier.

Comparison

With respect to the Core Rule Guidelines, both approaches are:

  • Widely applicable ✔️
  • Generic ✔️
  • Atomic ✔️
  • Library agnostic ✔️

An additional rule might fall foul of Uniqueness and “No conflicts”.

Issue Template

What category of rule is this?

[X] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [ ] Enforces code style (layout) [ ] Other (please specify:)

Why should this rule be included in ESLint (instead of a plugin)?

This rule would warn about JavaScript language use and would ideally be included in the eslint:recommended ruleset. It would detect usage that is almost universally a bug.

Are you willing to submit a pull request to implement this rule?

Yes

Does this change cause the rule to produce more or fewer warnings?

Depends on configuration. It would cause fewer warnings for someone wishing to block TDZ usage but wanting to use var hoisting.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mysticateacommented, Feb 22, 2019

Thank you for this proposal.

I think that no-undef rule is the best rule to report the clear TDZ accesses.

However, we cannot report all TDZ errors in the static analysis because TDZ is a timing matter rather than a structural matter.

{
    foo(); // TDZ ReferenceError: `x` is not defined.
    [0].forEach(foo); // TDZ ReferenceError: `x` is not defined.
    setTimeout(foo, 0); // OK!
    let x = 0;
    foo(); // OK!

    function foo() { x } //← access `x`
}
0reactions
eslint-deprecated[bot]commented, Mar 26, 2019

Unfortunately, it looks like there wasn’t enough interest from the team or community to implement this change. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach accepted status after 21 days tend to never be accepted, and as such, we close those issues. This doesn’t mean the idea isn’t interesting or useful, just that it’s not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Temporal Dead Zone (TDZ) and Hoisting in JavaScript
A temporal dead zone (TDZ) is the area of a block where a variable is inaccessible until the moment the computer completely initializes...
Read more >
An Introduction to the Temporal Dead Zone in JavaScript for ...
The temporal dead zone is the most straightforward term in JavaScript, but it is often not ... Hence the zone is known as...
Read more >
Don't Use JavaScript Variables Without Knowing Temporal ...
Temporal Dead Zone forbids the access of variables and classes before declaration in JavaScript.
Read more >
What is the temporal dead zone? - javascript - Stack Overflow
the same code will not give an error when we use var for variable 'a',. ex. var sum = a; console.log(sum) //prints undefined...
Read more >
17 - WA.gov
comments on a general area of proposed rule making before the agency ... legislature intends to provide incentives for the greater use of....
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