No lexical declarations inside CaseClause StatementList
See original GitHub issueThe 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:
- Created 8 years ago
- Comments:9 (8 by maintainers)
Top 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 >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
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:
The right way to write this is:
Thanks for explaining. Go ahead and create a PR.