How do I comment in CoffeeScript? "/* this */" doesn't work
Asked Answered
C

5

151

In what ways can you comment in CoffeeScript?

The documentation say you can use three hash symbols to start and close a comment block:

###
  Comments
  go
  here
###

I've found that I can sometimes use the following two formats

`// backticks allow for straight-JavaScript,
 // but the closing backtick can't be on a comment line (I think?)
`

Are there a simpler way to insert short comments in CoffeeScript?

Do NOT use this style**

Since this is getting a lot of views, I want to emphasize that

/* Comment goes here */

produces a MATH error when the /* is on its own line.

As Trevor pointed out in a comment on the question, this is a regular expression, NOT a comment!

Cheng answered 16/10, 2011 at 0:59 Comment(3)
If a /*...*/ comment "works," it's because the CoffeeScript compiler is interpreting it as a regex. Definitely not recommended!Priority
So I guess there is NO WAY in CoffeeScript to have an intra-statement (between characters) comment?Wrennie
Since I'm limited in formatting comments, I'll post an answer at the bottom.Nude
B
281

Use a single # sign

# like this

One character seems pretty minimal ;)

Also:

###
This block comment (useful for ©-Copyright info) also gets 
passed on to the browsers HTML /* like this! */
###
Banka answered 16/10, 2011 at 1:8 Comment(3)
This is usually how you will want to comment; the triple hash is most often used when you want the comment to fall through to the javascript (copyright messages, usually).Bruit
Ah sigh. The official docs use the single # form all through their examples, but never actually mention it in the text explanations, it only talks about the block comments.Santamaria
Unfortunately no way to have block comments that don't show up in output.Spaceband
I
24

The main way to comment is sh/Perl/Ruby/... style # comments:

# This comment goes to the end of the line
# and it won't appear in the "compiled"
# JavaScript version.

You use the block style ### comments when you want a comment to appear in the JavaScript version:

Sometimes you'd like to pass a block comment through to the generated JavaScript. For example, when you need to embed a licensing header at the top of a file. Block comments, which mirror the syntax for heredocs, are preserved in the generated code.

So if you start with

###
PancakeParser is Public Domain
###

then you'd get this JavaScript comment in the generated JavaScript:

/*
PancakeParser is Public Domain
*/
Individualize answered 16/10, 2011 at 1:11 Comment(0)
P
4

Beware of ###! If you use ### to separate sections of code (as I do) it's awfully surprising when that code stops working as a result.

Patten answered 18/4, 2013 at 17:36 Comment(5)
Do you know why? We have the code working locally but not on the build server with ###.Neuritis
Unfortunately, I noticed this months ago, and I'm not "in that space" right now to have a look at it.Patten
Because a pair makes a block comment?Zink
Would not be so surprising if you used a syntax-highlighting editor, with comments appearing in a different colorSynectics
Why downvote? It's a valid warning. Really, it's saying don't use a solid line of # as a section separator, or you may occasionally get unbalanced block comment pairs.Nip
N
1

For comments in the middle of a line, try:

if somecond `/* hi */` && some other cond, etc.

The backticks cause CoffeeScript to put the enclosed text into the generated JavaScript as is.

Nude answered 19/1, 2023 at 2:46 Comment(0)
N
0

Unfortunately, CoffeeScript is inconsistent with whether comments introduced with a single # character make it to the output JavaScript. For example:

# testme.coffee

fName = 'John'
lName = 'Smith'
fullName = "#{fName} #{lName}" # |||| $:
if true #comment
    console.log `/* hi */` 'Hello, World!'

compiles to

// Generated by CoffeeScript 2.7.0
(function() {
  // testme.coffee
  var fName, fullName, lName;

  fName = 'John';

  lName = 'Smith';

  fullName = `${fName} ${lName}`;

  if (true) { //comment
    console.log(/* hi */`Hello, World!`);
  }

}).call(this);

Note the missing comment on the line that assigns to fullName.

Nude answered 19/1, 2023 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.