Proposal: Modify automatic semicolon insertion in strict mode

David-Sarah Hopwood david.hopwood at
Wed Dec 10 22:05:09 PST 2008

Neil Mix wrote:
> On Dec 9, 2008, at 1:00 AM, David-Sarah Hopwood wrote:
>>> Why impose unnecessary work on someone trying to use strict?
>> I repeat, not even checking that code still parses after adding
>> "use strict;" is obviously silly, and we should not claim that strict
>> mode is usable without doing that -- whether or not it does semicolon
>> insertion. In other words, the work is necessary.
> It's more than just making sure the file parses correctly -- it's adding
> the semicolons too.

You've lost the context of my original post, which answered this point:

| Brendan Eich wrote:
| > Never mind that many JS developers would have our heads for removing ASI
| > from strict mode, the problem remains that programmers don't know where
| > they depend on ASI, and if we try to force them to care, we'll fail --
| > "they" may not even be around, and the content errors will be foisted on
| > innocent end users of the incumbent content.
| Why? That would only happen if you added "use strict;" to a program fragment
| without testing that the resulting program still parses correctly, which is
| an obviously silly thing to do. If it fails to parse, *and* no programmer
| who can fix it is available, then don't add the "use strict;".
| Alternatively, put the source through a filter that adds the semicolons
| automatically, once and for all.

I will write the filter, if there is agreement not to do semicolon insertion
in strict mode. I'm implementing an ANTLRv3 parser for ES3.1 anyway
(starting with Patrick Hulsmeijer's ES3 grammar), so this will not be much
extra work.

> Are you assuming that most programs will parse correctly without semicolon
> insertion?

No, I'm not assuming that.

> = function() {
>   //...
> } // <-- whoops, no semicolon
> For me that qualifies as "unnecessary work," aka "pedantic."

I hope it's not too pedantic of me to point out that "unnecessary work"
means something quite different from "pedantic". Or maybe it is pedantic,
but necessary ;-)


Whether the semicolon is unnecessary depends on what is on the next line
after your example. For instance: = function() {

This will unintentionally apply the function to (anything) and set Foo
to the result. So the semicolon is necessary here.

Since the above code is valid ECMAScript, the filter can't reject it,
but it can heuristically warn about code that is likely to have omitted
semicolons where they are needed.

In this case, "(anything)" is at the same indentation level as "Foo...",
which is a dead giveaway that the programmer intended it to be a new
statement. Almost all common coding styles require a continuation of a
statement on the previous line to be indented.

Similarly, if a semicolon would be inserted at the end of a line and
the next line is indented further to the right, then that is also probably
a mistake.

Clearly, this kind of heuristic can't be put in the language spec, because
ECMAScript does not use indentation-based syntax and cannot be compatibly
changed to do so. It can only be checked by a tool that is not strictly
part of the language implementation (although it could be integrated with
an IDE, editor, or debugger).

> I don't think it's right to ask "what are the reasons someone wouldn't
> migrate to strict mode?"  Non-migration is the de-facto "winning"
> position.  The real question is, "why would someone bother to migrate?"

Isn't that obvious? To provide a better chance of detecting errors in
their code. But I suspect that strict mode will be *more* relevant for
new code. If it is used in a large proportion of new code and in heavily
relied-on code (libraries like Prototype, jQuery, etc., and internally
within browser implementations), then it will have been successful.

David-Sarah Hopwood

More information about the Es-discuss mailing list