What are the rules for JavaScript's automatic semicolon insertion (ASI)? -


OK, first of all I should ask that this browser is dependent.

I have read that if an invalid token is found, but that segment of code is valid unless an invalid token is inserted before a semicolon token, if it occurs before a line break .
However, a common example is given for bugs caused by semicolon insertion:

  back _a + b;  

That does not follow this rule, because _a will be a valid token. On the other hand, breaking the call chain works as expected:

  $ ('# mybutton'). Do anybody have a more detailed description of the rules?   

< First class you should know which statements are automatically affected by semicolon insertion (briefly known as ASI). ):

  • The empty statement
  • var
  • Expression statement
  • two-hour statement
  • issued statement
  • pauses
  • W C
  • throw statement
  • Three cases were described

    Ol>

  • When a token ( LineTerminator or } ) is grammar Permission is not granted, then a semicolon is inserted before it:

    • The token is separated from the last token with at least one LineTerminator . < For example:

        {1 2} 3 // to {/>  
    • token }

    1; 2;} has been changed to 3;

  • Numerical literals 1 meets the first condition, the following token is a line terminator. 2 meets the second condition, the following token } .

  • When the tokens input stream ends and the parser is unable to parse the input token stream as a complete program, at the end of the input stream A semicolon is automatically inserted.

    Example:

      A = B ++ c // It is converted to: A = B; ++ C;  
  • This occurs when tokens are allowed by some production of grammar, but production is a restricted product , a semicolon Automatically insert before a restricted token. Restricted presentations:

      Update expression: Left hand side expense [no line not guarantor] ++ LeftHandSideExpression [No line terminator here] - Continue: Continue; Continue [no linearminator here] LabelIdentifier; BreakStatainment: Break; Break [No linearminator here] LabelIdentifier; Return Status: Return; Return [No linear terminator here] expression; ThrowStatement: Throw [no linearminator] expression; ArrowFunction: ArrowParameters [no LineTerminator here] => ConciseBody YieldExpression: Production [No line terminator is not here] * AssignmentAxample yield [no linemaninator] AssignmentExpress  

    ReturnStatement :

    return "something"; // changed for return; "something";

  • Comments