jashkenas / jashkenas/coffeescript

Bug: comment in array literal is moved into multi-line string

Open
#5,325 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
CoffeeScript
Stars
16.6k
Forks
2k
PR merge metrics
No merged PRs in 30d

Description

### Bug

when compiled, a comment which is inside an array literal is moved into a multi-line string preceding the array literal.

### Input Code

minimal example which exhibits the incorrect behavior:

```coffee
result = db_select sql_transform("""
select *
from users
where id = ?
"""), [
# a comment here
user_id
]
```

results in the following:
```javascript
var result;

result = db_select(sql_transform(`select *
from users
// a comment here
where id = ?`), [user_id]);
```

can also be seen at this link: [Coffeescript Online REPL](https://coffeescript.org/#try:result%20%3D%20db_select%20sql_transform(%22%22%22%0A%09select%20*%0A%09from%20users%0A%09where%20id%20%3D%20%3F%0A%22%22%22)%2C%20%5B%0A%09%23%20a%20comment%20here%0A%09user_id%0A%5D)

example which *does* work correctly: [Coffeescript Online REPL](https://coffeescript.org/#try:result%20%3D%20db_select%20sql_transform(%22%22%22%0A%09select%20*%0A%09from%20users%0A%09where%20id%20%3D%20%3F%0A%09or%20id%20%3D%20%3F%0A%22%22%22)%2C%20%5B%0A%09%23%20a%20comment%20here%0A%09user_id%0A%09%23%20a%20comment%20here%0A%09user_id%0A%5D)

### Expected Behavior

the comment should be within the array literal, or directly preceding it, or even not output at all if it's ambiguous where it should go. it should definitely not be moved inside a string literal. it seems to work correctly when the array literal has *two* comments, because then the array literal remains multi-line, whereas with a single comment, it's changed to a single-line array literal, and the comment is moved a line above it.

### Current Behavior

the comment is moved inside a string.

### Environment

* CoffeeScript version: 2.5.1
* Node.js version: not relevant (issue also seen in online REPL)

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the minimal CoffeeScript input from the report, using CoffeeScript 2.5.1 or the linked online REPL, and inspect the generated JavaScript. Compare the one-comment and two-comment array cases. Done means the comment is no longer emitted inside the multiline string, while the array and string remain correctly represented.

Written by the indexing model from the issue text.

Assessment

Tech stack
coffeescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.