Paste if no braces

if (true) {
  let x = 5
}

works as expected (no syntax error), but

if (true) let x = 5

throws SyntaxError: Unexpected strict mode reserved wordin Node 4.1.0 and babel

Is this the expected behavior? I know this is a stupid example. I'm just wondering if this is a mistake or not.

+4
source share
1 answer

Yes, this is the expected behavior. Production rule instructionsif

 if ( Expression[In, ?Yield] ) Statement[?Yield, ?Return]

but the ad is let notStatement and therefore not allowed in this position:

Statement[Yield, Return] :
    BlockStatement[?Yield, ?Return]
    VariableStatement[?Yield]
    EmptyStatement
    ExpressionStatement[?Yield]
    IfStatement[?Yield, ?Return]
    BreakableStatement[?Yield, ?Return]
    ContinueStatement[?Yield]
    BreakStatement[?Yield]
    [+Return] ReturnStatement[?Yield]
    WithStatement[?Yield, ?Return]
    LabelledStatement[?Yield, ?Return]
    ThrowStatement[?Yield]
    TryStatement[?Yield, ?Return]
    DebuggerStatement

Declaration[Yield] :
    HoistableDeclaration[?Yield]
    ClassDeclaration[?Yield]
    LexicalDeclaration[In, ?Yield]

LexicalDeclaration[In, Yield] :
    LetOrConst BindingList[?In, ?Yield] ;
+8
source

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


All Articles