Search code examples
javascriptecmascript-6automatic-semicolon-insertion

Javascript automatic semicolon insertion for do-while statements


Since ES6, a new case has been added for rule 1 of automatic semicolon insertion:

The previous token is ) and the inserted semicolon would then be parsed as the terminating semicolon of a do-while statement (13.7.2).

This allows one to avoid line termination and write ugly code such as:

do {} while (false) var a = 42

What is the rationale behind this rule? Some useful use-case?


Solution

  • I'm pretty sure that "case" added in ES2015 is only there to standardize rules which browsers had already implemented in order to be compatible with terribly-written (or weirdly minified) scripts. It wasn't exactly a new feature, so much as it was a tweak of the specification to be in line with what browsers were doing already.

    For example, your snippet runs in IE11, which was released in 2013:

    do {} while (false) var a = 42;
    console.log('no parse errors');