Skip to content

Commit

Permalink
Block inline JavaScript can end with an escaped backtick character
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Nov 15, 2016
1 parent b9dd310 commit 3200259
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
18 changes: 6 additions & 12 deletions lib/coffee-script/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,11 @@ exports.Lexer = class Lexer
# Matches JavaScript interpolated directly into the source via backticks.
jsToken: ->
return 0 unless @chunk.charAt(0) is '`' and match = JSTOKEN.exec @chunk
[js, here] = match
if here?
script = here
length = here.length + 6 # 6 is the length of the six ` characters
else
script = js[1...-1]
length = js.length
[js] = match
script = if js[0..2] is '```' then js[3...-3] else js[1...-1]
script = script.replace /\\`/g, '`' # Convert escaped backticks to backticks
@token 'JS', script, 0, length
length
@token 'JS', script, 0, js.length
js.length

# Matches regular expression literals, as well as multiline extended ones.
# Lexing regular expressions is difficult to distinguish from division, so we
Expand Down Expand Up @@ -908,7 +903,7 @@ CODE = /^[-=]>/

MULTI_DENT = /^(?:\n[^\n\S]*)+/

JSTOKEN = /^```([\s\S]*?)```|^`[^\\`]*(?:\\.[^\\`]*)*`/
JSTOKEN = /^```([\s\S]*?)(?:\\`(```)|```)|^`[^\\`]*(?:\\.[^\\`]*)*`/

# String-matching-regexes.
STRING_START = /^(?:'''|"""|'|")/
Expand Down
7 changes: 7 additions & 0 deletions test/javascript_literals.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ test "block inline JavaScript containing backticks", ->
eq a + c, 45
eq b, 'foo bar'
eq d, 'foo`bar`'

test "block JavaScript can end with an escaped backtick character", ->
```var a = \`hello\````
```
var b = \`world${'!'}\````
eq a, 'hello'
eq b, 'world!'

0 comments on commit 3200259

Please sign in to comment.