Switch statement and scope in ES2015

Consider this ES2015 module and behavior when working in node v4.4.5.

'use strict'

const outer = 1

switch ('foo') {
  case 'bar':
    const heyBar = 'HEY_BAR'
    break
  case 'baz':
    const heyBaz = 'HEY_BAZ'
    break
  default:
    const heyDefault = 'HEY_DEFAULT'
}
console.log(
  outer, // 1, makes sense, same top-level scope
  heyBar, // undefined. huh? I thought switch did NOT create a child scope
  heyBaz, // undefined. huh? I thought switch did NOT create a child scope
  heyDefault) // 'HEY_DEFAULT' makes sense

This seems internally inconsistent to me. If the switch statement did NOT create a lexical scope, I would expect all variables to hey*be part of the main scope and all to behave sequentially. If the switch statement created a lexical scope, I would still expect them to be consistent, but the variables declared in the sentences casewill behave as if they are in a child scope, while the variables in the sentence defaultbehave like they are in external area.

: - , switch, , , ?

node v6.4.0 . , switch .

ReferenceError: heyBar is not defined

.

+4
4

. ReferenceError (Node 6.4.0 Firefox):

ReferenceError: heyBar is not defined

. AFAIK switch DO , , . case .

foo switch, ReferenceError:

'use strict'

const outer = 1

switch ('foo') {
  case 'bar':
    const heyBar = 'HEY_BAR'
    break
  case 'baz':
    const heyBaz = 'HEY_BAZ'
    break
  case 'foo':
    const heyFoo = 'HEY_FOO'
    break
  default:
    const heyDefault = 'HEY_DEFAULT'
}
console.log(
  outer,
  heyFoo,
  heyBar,
  heyBaz,
  heyDefault) // ReferenceError: heyFoo is not defined

: 13.12.11 :

5. Let blockEnv be NewDeclarativeEnvironment(oldEnv).
6. Perform BlockDeclarationInstantiation(CaseBlock, blockEnv).

CaseBlock - switch.

:

switch (switch { <-here-> }) (, let, const ).

+9

ReferenceError: heyDefault is not defined , heyBar, ReferenceError: heyBar .

switch switch (...) { ... }, case . . .

+3

switch . case default .

switch - , , ES2016. , , , , . , .

-, switch :

SwitchStatement:
    switch ( Expression ) CaseBlock

CaseBlock:

CaseBlock:
    { CaseClauses }
    { CaseClauses DefaultClause CaseClauses }

CaseClauses:
    CaseClause
    CaseClauses CaseClause

CaseClause:
    case Expression : StatementList

DefaultClause:
    default : StatementList

, CaseBlock switch (, ).

, , CaseBlock, , .

CaseBlock Scope

13.2.14 : BlockDeclarationInstantiation (code, env), , CaseBlock .

Block CaseBlock, , , , , .

CaseBlock switch, , switch ( let/const).

CaseClause ( )

, 13.12.6 : LexicallyScopedDeclarations, , . ( ):

CaseBlock: {CaseClauses DefaultClause CaseClauses}

  • CaseClauses, LexicalScopedDeclarations CaseClauses.
  • Else let .
  • LexicallyScopedDeclarations DefaultClause.
  • CaseClauses , .
  • Else LexicalScopedDeclarations caseClaus.

CaseClauses: CaseClauses CaseClause

  • LexicallyScopedDeclarations caseClauses.
  • LexicalScopedDeclarations CaseClause.
  • .

CaseClause: case Expression: StatementList

  • StatementList, LexicallyScopedDeclarations StatementList.
  • Else .

DefaultClause: default: StatementList

  • StatementList, LexicallyScopedDeclarations StatementList.
  • Else .

, , , caseClause LexicallyScopedDeclarations. DefaultClause CaseClause, . .

A CaseClause declarations, . , , .

, , CaseClause, , CaseClause , .

:

, 13.12.11 :

SwitchStatement: () CaseBlock

  • exprRef - .
  • switchValue ? (exprRef).
  • oldEnv - LexicalEnvironment.
  • blockEnv NewDeclarativeEnvironment (oldEnv).
  • BlockDeclarationInstantiation (CaseBlock, blockEnv).
  • LexicalEnvironment blockEnv.
  • R - CaseBlockEvaluation CaseBlock switchValue.
  • LexicalEnvironment oldEnv.
  • R.

4 5, CaseBlock . 13.12.11, CaseClause CaseBlock .

CaseClause: case Expression: StatementList

  • StatementList.

, . A CaseBlock . A CaseClause ( CaseClause).

+2

{ } case, :

const var1 = true;

switch (var1) {
  case true: {
    const var2 = 0;
    break;
  }
  case false: {
    const var2 = 1;
    break;
  }
  default: {
    const var2 = 2;
  }
}
0

Source: https://habr.com/ru/post/1651576/


All Articles