Skip to content Skip to sidebar Skip to footer

Bad Escapement & Unclosed String

var freebie = ' \
\
\
\

Solution 1:

Short answer, you need to configure JSLint to tolerate ECMAScript 5.

This can be done with:

/*jslintes5:true,all-your-other-jslint-options*/

Long answer:

The sequence, \ followed by a line terminator, was not actually valid Javascript until recently (version 5, December 2009).

From the previous iteration (version 3) of the ECMAScript (ECMA-262) standard, section 7.8.4 states: (with some irrelevant entries removed):

StringLiteral ::
    " DoubleStringCharacters(opt) "
    ' SingleStringCharacters(opt) '

SingleStringCharacters ::
    SingleStringCharacter SingleStringCharacters(opt)

SingleStringCharacter ::
    SourceCharacter but not single-quote ' or backslash \ or LineTerminator
    \ EscapeSequence

So the sequence you have ends up at that last line above, a single \ followed by the syntax element EscapeSequence. Examining that further:

EscapeSequence ::
    CharacterEscapeSequence
    0 [lookahead ∉ DecimalDigit]
    HexEscapeSequence
    UnicodeEscapeSequence

CharacterEscapeSequence ::
    SingleEscapeCharacter
    NonEscapeCharacter

SingleEscapeCharacter :: one of
    ' " \ b f n r t v

NonEscapeCharacter ::
    SourceCharacter but not EscapeCharacter or LineTerminator

EscapeCharacter ::
    SingleEscapeCharacter
    DecimalDigit
    x
    u

HexEscapeSequence ::
    x HexDigit HexDigit

UnicodeEscapeSequence ::
    u HexDigit HexDigit HexDigit HexDigit 

Since the next character after \ is neither 0, x or u, the only alternative is CharacterEscapeSequence which boils down to either SingleEscapeCharacter (not the case since the line terminator is not one of those characters listed ) or NonEscapeCharacter (which explicitly excludes the line terminator as a possibility).

There's also this note at the bottom of that section:

NOTE: A 'LineTerminator' character cannot appear in a string literal, even if preceded by a backslash . The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.


Now, ECMAScript 5 changed that a little. Starting from there, they modified the definition of SingleStringCharacter thus:

SingleStringCharacter ::
    SourceCharacter but not one of ' or \ or LineTerminator
    \ EscapeSequence 
    LineContinuation

LineContinuation ::
    \ LineTerminatorSequence

and modified the note to be:

NOTE: A line terminator character cannot appear in a string literal, except as part of a LineContinuation to produce the empty character sequence. The correct way to cause a line terminator character to be part of the String value of a string literal is to use an escape sequence such as \n or \u000A.

And, rather than break JSLint for all the current scripts out there, the authors intelligently decided to make ECMAScript 5 support optional, requiring a change to the JSLint options to activate it. That way, it will only allow ECMAScript 5 if you explicitly tell it so.

You can visit the http://www.jslint.com/ website and confirm this:

Code:
    var xyzzy = ' \
    hello";
Error:
    Problem at line 1 character 16: This is an ES5 feature.
var xyzzy = ' \
    Problem at line 2 character 13: Unclosed string.
hello";
    Problem at line 2 character 13: Stopping. (66% scanned).

If you then scroll down to the flags section, there's an entry for Tolerate ES5 syntax which will, when set, remove that error.

Post a Comment for "Bad Escapement & Unclosed String"