Skip to content

Commit 3f4b03b

Browse files
Fix #4763: Comments at beginning or end of REPL input shouldn't throw errors (#4764)
1 parent a706a64 commit 3f4b03b

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

‎lib/coffeescript/repl.js

+8-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/repl.coffee

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ replDefaults =
3030
try
3131
# Tokenize the clean input.
3232
tokens = CoffeeScript.tokens input
33+
# Filter out tokens generated just to hold comments.
34+
if tokens.length >= 2 and tokens[0].generated and
35+
tokens[0].comments?.length isnt 0 and tokens[0][1] is '' and
36+
tokens[1][0] is 'TERMINATOR'
37+
tokens = tokens[2...]
38+
if tokens.length >= 1 and tokens[tokens.length - 1].generated and
39+
tokens[tokens.length - 1].comments?.length isnt 0 and tokens[tokens.length - 1][1] is ''
40+
tokens.pop()
3341
# Collect referenced variable names just like in `CoffeeScript.compile`.
3442
referencedVars = (token[1] for token in tokens when token[0] is 'IDENTIFIER')
3543
# Generate the AST of the tokens.

‎test/repl.coffee

+8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ testRepl "empty command evaluates to undefined", (input, output) ->
7777
input.emitLine ''
7878
eq 'undefined', output.lastWrite()
7979

80+
testRepl "#4763: comment evaluates to undefined", (input, output) ->
81+
input.emitLine '# comment'
82+
eq 'undefined', output.lastWrite()
83+
84+
testRepl "#4763: multiple comments evaluate to undefined", (input, output) ->
85+
input.emitLine '### a ### ### b ### # c'
86+
eq 'undefined', output.lastWrite()
87+
8088
testRepl "ctrl-v toggles multiline prompt", (input, output) ->
8189
input.emit 'keypress', null, ctrlV
8290
eq '------> ', output.lastWrite(0)

0 commit comments

Comments
 (0)