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.

No lexical declarations inside CaseClause StatementList

See original GitHub issue

The following code has bad semantics:

let x = 2;
switch (2) {
  case 1:
    let x = 42;
    break;
  case 2:
    let y = x;  // TDZ error for x since it was never initialized.
   break;
}

because let/const (and class and functions) are scoped to the switch block.

It would be nice to have a lint rule saying that let, const, class and function are not allowed (as direct children) in the StatementList of a CaseClause (and same for DefaultClause).

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
arvcommented, Oct 28, 2015

That is a different issue. That was a bad example. Let me try to clarify.

The problem is that lexical declarations are “hoisted” to the nearest block which is the SwitchStatement. The problem is that the CaseClause is not a block in JS. Here is another example:

switch (2) {
  case 1:
    let x = 1;
    print(x);
    break;
  case 2:
    let y = x;  // This always fails.
    print(y);
    break;
 }

The right way to write this is:

switch (2) {
  case 1: {
    let x = 1;
    print(x);
    break;
  }
  case 2: {
    let y = x;  // eslint: x is undefined
    print(y);
    break;
  }
 }
0reactions
nzakascommented, Oct 29, 2015

Thanks for explaining. Go ahead and create a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-case-declarations - ESLint - Pluggable JavaScript Linter
This rule disallows lexical declarations ( let , const , function and class ) in case / default clauses. The reason is that...
Read more >
eslint - unexpected lexical declaration in case block
ESLint doesn't like let statements inside case blocks inside a reducer, Why? This is discouraged because it results in the variable being in...
Read more >
12 Statements # T E ① A — Annotated ES5
A LabelledStatement has no semantic meaning other than the introduction of a label to a label set. The label set of an IterationStatement...
Read more >
ECMAScript class static initialization blocks - TC39
Let names be LexicallyDeclaredNames of StatementList . ... If the second CaseClauses is not present, return declarations .
Read more >
ECMAScript 2015 §ECMAScript Language: Statements and ...
It is a Syntax Error if the LexicallyDeclaredNames of StatementList contains ... If a LexicalBinding in a let declaration does not have an...
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