Hopefully last word on Use Strict Directive syntax

David-Sarah Hopwood david-sarah at jacaranda.org
Tue Jun 9 19:33:59 PDT 2009

Douglas Crockford wrote:
> David-Sarah Hopwood wrote:
>> The rationale for using a StringLiteral ExpressionStatement as the syntax
>> for a directive, is that such a statement is always useless. The above
>> example is not useless code; it has a well-defined meaning, equivalent to
>>   "use strict" + x ? delete p : delete q;
>> I hope that we are not going to try to *guess* whether this code is too
>> obscure for the programmer (or code generator) to have really meant it.
> There is no need to guess. The person (or defective code generator) who
> wrote that code has no right to expect it to do anything useful. They
> should be glad to get the syntax error.

What are the exact criteria by which you are proposing that statements
like the one above would be rejected?

If any ExpressionStatement that starts with "use strict" (or some larger
set of string literals) would be rejected, then I think that is quite
broken. It is perfectly reasonable and useful for an ExpressionStatement
to start with a string literal [*]; so excluding some subset of string
literals is irregular, and *does* amount to guessing what the programmer
or code generator really meant. I am strongly opposed to introducing such

If the rule were that an initial StringLiteral in an ExpressionStatement
cannot be followed by a LineTerminator, then that would be adding yet
another special case to the semicolon insertion rules, which are
overcomplicated as it is.

(Again, either of those options might be appropriate for a linter warning,
but not for a syntax restriction.)

Besides, the original example, with the code on two lines, is a special
case of an error [the programmer expects semicolon insertion in a place
where it does not actually occur] that would still be able to happen in
many other contexts. A programmer can avoid it simply by writing semicolons
explicitly and never relying on them to be inserted. It would be helpful
to have a mode where this is enforced as far as it can be, but doing so
only in this special case is pointless and inadequate to solve the real
problem, IMHO.

[*] For example:

      "number" === typeof x ? frobNumber(x) : frobOther(x);

    (This is perhaps dubious coding style, but it is enough that it has
    a well-defined and useful meaning.)

David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com

More information about the es5-discuss mailing list