Skip to content

Commit

Permalink
support multiline CoffeeScript comments
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Jan 4, 2024
1 parent 63b2ef5 commit d7e7502
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
22 changes: 17 additions & 5 deletions lib/rewriters/coffeescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ const chunksCoffee = input => {
for (const comment of comments (CoffeeScript.nodes (input))) {
const [start, end] = comment.locationData.range;
chunks.literals.set (offset, input.slice (offset, start));
chunks.comments.set (start, {
indent: ' '.repeat (comment.locationData.first_column),
number: comment.locationData.first_line + 1,
text: comment.content,
});
offset = start + (comment.here ? '###' : '#').length;
const indent = ' '.repeat (comment.locationData.first_column);
let from = 0;
let number = comment.locationData.first_line + 1;
while (true) {
const to = comment.content.indexOf ('\n', from);
if (to < 0) {
const text = comment.content.slice (from);
chunks.comments.set (offset + from, {indent, number, text});
break;
} else {
const text = comment.content.slice (from, to);
chunks.comments.set (offset + from, {indent, number, text});
from = to + '\n'.length;
number += 1;
}
}
offset = end;
}
chunks.literals.set (offset, input.slice (offset));
Expand Down
28 changes: 14 additions & 14 deletions test/shared/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,20 @@ do ->
# ..... .5
# 1234.5

23: 'TODO: multiline comment'
#
# > 3 ** 3 - 2 ** 2
# 23
#

24: 'TODO: multiline comment with wrapped input'
#
# > (["foo", "bar", "baz"]
# . .slice(0, -1)
# . .join(" ")
# . .toUpperCase())
# "FOO BAR"
#
23: 'multiline comment'
###
> 3 ** 3 - 2 ** 2
23
###

24: 'multiline comment with wrapped input'
###
> (["foo", "bar", "baz"]
. .slice(0, -1)
. .join(" ")
. .toUpperCase())
"FOO BAR"
###



Expand Down

0 comments on commit d7e7502

Please sign in to comment.