Skip to content

Commit 3200259

Browse files
committed
Block inline JavaScript can end with an escaped backtick character
1 parent b9dd310 commit 3200259

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

lib/coffee-script/lexer.js

Lines changed: 6 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lexer.coffee

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,11 @@ exports.Lexer = class Lexer
293293
# Matches JavaScript interpolated directly into the source via backticks.
294294
jsToken: ->
295295
return 0 unless @chunk.charAt(0) is '`' and match = JSTOKEN.exec @chunk
296-
[js, here] = match
297-
if here?
298-
script = here
299-
length = here.length + 6 # 6 is the length of the six ` characters
300-
else
301-
script = js[1...-1]
302-
length = js.length
296+
[js] = match
297+
script = if js[0..2] is '```' then js[3...-3] else js[1...-1]
303298
script = script.replace /\\`/g, '`' # Convert escaped backticks to backticks
304-
@token 'JS', script, 0, length
305-
length
299+
@token 'JS', script, 0, js.length
300+
js.length
306301

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

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

911-
JSTOKEN = /^```([\s\S]*?)```|^`[^\\`]*(?:\\.[^\\`]*)*`/
906+
JSTOKEN = /^```([\s\S]*?)(?:\\`(```)|```)|^`[^\\`]*(?:\\.[^\\`]*)*`/
912907

913908
# String-matching-regexes.
914909
STRING_START = /^(?:'''|"""|'|")/

test/javascript_literals.coffee

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ test "block inline JavaScript containing backticks", ->
3838
eq a + c, 45
3939
eq b, 'foo bar'
4040
eq d, 'foo`bar`'
41+
42+
test "block JavaScript can end with an escaped backtick character", ->
43+
```var a = \`hello\````
44+
```
45+
var b = \`world${'!'}\````
46+
eq a, 'hello'
47+
eq b, 'world!'

0 commit comments

Comments
 (0)