Comments (11)
Your example here is now fixed on master. It's a larger issue though:
CoffeeScript passing comments through to the compiled JavaScript is a nice feature to have. It goes well with the pretty printing, and helps keep your compiled JS debuggable. Unfortunately, parsing comments that violate the significant whitespace rules can really screw things up. Currently this is handled in Rewriter#adjust_comments, but it's quite hacky. Perhaps the lexer (when consuming an indent or outdent) needs to look ahead past any following comment tokens, to see what the indentation is really going to be.
Ideas for how best to fix this in general (ie. parsing comments at any indentation level, without them affecting indentation) would be appreciated.
from coffeescript.
I'd like to help, but I don't actually fully understand how the whitespace delimiting works.
Perhaps you could do a little write up with an example in the docs ?
from coffeescript.
The best reference for the way that whitespace works is Kamatsu's original demonstration:
http://github.com/jashkenas/coffee-script/issues/closed#issue/5
To see it occur in lexer.rb, look at Lexer#indent_token
and Lexer#outdent_token
, as well as Rewriter#rewrite_closing_parens
. In the grammar, the most important rule is Block
.
from coffeescript.
jots contributes this, which errors in the current parser:
while c
switch c
# whitespace
when c <= ' '
do_something()
from coffeescript.
Let's have some votes on this. Fixing it really nicely would mean finding a way to parse comments at any indentation level, and have them printed on the correct line in the JavaScript. I can't think of a way to do it properly -- if the lexer pulls them out in advance, it's difficult to find the lines where they should be inserted in the AST on the other end. If the lexer tokenizes them as it does now, it has the potential to confuse indentation, and to confuse the grammar, depending on where the comment occurs in the token stream. At the moment, you can most easily put them anywhere you can put an expression, but as you can see in the above example, some places where expressions don't belong are totally reasonable for comments.
The alternative, which is what we did originally, would just be to strip out comments in the lexer.
Ideas? Votes?
from coffeescript.
Obviously, from a debugging point of view, the nicest is to maintain the comments. It must be possible, but I understand the algorithm is a little tricky - we need kamatsu on the case!
from coffeescript.
some ides
a) is it possible for the indenter to ignore any line with only a comment on it?
B) is it possible, to 'fix' the indentation levels of the comments, if they occur in the wrong place? I.e. is there always a 'correct' level of indentation for a comment, or are there lines where a comment is just innappropriate (jots example above?)
from coffeescript.
Ok, jots' case above now compiles on master, but only because I added a special case in the parser for comments within switch statements. Closing the ticket for now. If anyone else finds gaping holes in the comment parsing, or has a solution for how to parse them in the general case, feel free to reopen this.
from coffeescript.
I'm bringing this ticket back from the dead because this situation hasn't been resolved satisfactorily in many months. So, I've pushed a patch to CoffeeScript that takes the usual lexing approach, discarding comments instead of passing them through to the JS.
Now, we should be able to support comments in any location you like, with the downside that you'll no longer be able to have them in the JavaScript. As I've been working with CoffeeScript apps, I've found that I've used the JavaScript for debugging only, and have never really looked at the comments in a JavaScript file -- so this is actually an improvement in readability for my case.
Here's the commit:
http://github.com/jashkenas/coffee-script/commit/ec570c46bfaf91a30d86ee140cb78f6bdef04c6d
from coffeescript.
It's necessary for some comments to go through to the JavaScript. For instance, if I use open-sourced CoffeeScript code, I'd be legally required to include the license from the code's comments in the comments of the JavaScript I serve. It would be annoying to have to add the license in by hand every time I recompiled the CoffeeScript.
However, I've also bumped into the problem of comments causing compilation errors many, many times, and I understand why preserving comments anywhere is such a difficult technical challenge.
So here's what I think is an acceptable compromise: Preserve block comments. Discard the rest. Don't allow ###
-style comments to go anywhere they might gum up the works. Allow #
-style comments at the end of any line, and just ignore them during compilation. Does that sound workable?
from coffeescript.
Both workable, and a grand idea. I've gone back and reverted large portions of the comma-killing commit to make block comments passable again. Here's the test case of places where block comments may be used:
###
This is a here-comment.
Kind of like a heredoc.
###
func: ->
###
Another block comment.
###
code
obj: {
a: b
###
comment
###
c: d
}
arr: [
1, 2, 3,
###
four
###
5, 6, 7
]
Thanks much. It's now on master. If you have any further tweaks to block comments, let me know.
from coffeescript.
Related Issues (20)
- Unnecessary `splice` ref added for Array destructuring with rest element not in last position HOT 4
- Bug: Re-ordered nested non-end BindingRestElement doesn't get transpiled HOT 1
- CoffeeScript is fantastic, please donβt give it up HOT 1
- How to imitate `let` behavior in loops? HOT 5
- Proposal: Alternative file extension HOT 1
- Bug: Excessive variable and shallow copy for leading or middle rest parameter
- Proposal: Introduce `let` statement. HOT 6
- Proposal: Document Existential Operator Assignment
- Site issue: code blocks twitch on hover HOT 2
- Proposal: cake command should support ES6 modules HOT 2
- Need help understanding class member meanings HOT 1
- CLI `npm` `scripts` and input `.coffee` file/s as last argument conflicting with `--watch` HOT 2
- Bug: wrong code is transpiled for function call without parentheses HOT 2
- Proposal: Add end word to close method or class HOT 2
- Bug: Invalid indentation allowed after `do`
- Bug: Remove checkShebangLine multi arguments check HOT 1
- feature_request(html): backend CoffeeScript compilation inside HTML files HOT 3
- Bug: yield cannot be used in do -> expressions reliably? HOT 5
- [not an issue] An embeddable playground for CoffeeScript HOT 1
- Please help HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coffeescript.