user619271
user619271

Reputation: 5022

Why this coffeescript does not run?

coffee test.coffee with this code

###
#/usr/local/bin/coffee $0
###
console.log 'test'

prints

Running node v0.11.13
/home/user/tst/test.coffee:3
 */usr/local/bin/coffee $0
                        ^^
SyntaxError: Unexpected identifier
  at exports.runInThisContext (vm.js:69:16)
  at Module._compile (module.js:432:25)

but with this code

###
#usr/local/bin/coffee $0
###
console.log 'test'

it runs ok

Running node v0.11.13
test

my coffee binary is located at /usr/local/bin/coffee but I expected this string to be not involved as this is a comment. Is this a bug?

Upvotes: 0

Views: 76

Answers (1)

Leonid Beschastny
Leonid Beschastny

Reputation: 51500

The problem is that your code compiles to the following JS code:

/*
 */usr/local/bin/coffee $0
 */
console.log('test');

Because Coffee compiles you block comment into its JS equivalent, replacing

###
# block
# comment
###

with its idiomatic JS version

/*
 * block
 * comment
 */

But since your comment starts with /, it compiles to invalid JS.

To fix this problem simply add whitespace between # and /:

###
# /usr/local/bin/coffee $0
###
console.log 'test'

P.S.: I created an issue on CoffeeScript issue tracker based on your question.

Upvotes: 2

Related Questions