Bad Escapement & Unclosed String

不羁岁月 提交于 2019-12-23 13:09:17

问题


var freebie = ' \
    <div class="item'+last+'" data-type="psd" data-visited="false"> \
      <div class="baseBubble" /> \
      <div class="baseStroke" /> \
      <div class="baseThickerStroke" /> \
      <div class="thumbnail"> \
        <div class="overlay" /> \
        <img src="'+item['thumb'][0]+'" width="'+item['thumb'][1]+'" height="'+item['thumb'][2]+'" /> \
      </div> \
    </div>';

Does anyone know why this is returning 2 errors in JSLint?

Here's an image of the 2 errors:

     


回答1:


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

This can be done with:

/*jslint es5: 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.



来源:https://stackoverflow.com/questions/8090062/bad-escapement-unclosed-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!