Giter Club home page Giter Club logo

Comments (11)

jashkenas avatar jashkenas commented on July 23, 2024

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.

weepy avatar weepy commented on July 23, 2024

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.

jashkenas avatar jashkenas commented on July 23, 2024

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.

jashkenas avatar jashkenas commented on July 23, 2024

jots contributes this, which errors in the current parser:

while c
  switch c
    # whitespace
    when c <= ' '
      do_something()

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

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.

weepy avatar weepy commented on July 23, 2024

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.

weepy avatar weepy commented on July 23, 2024

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.

jashkenas avatar jashkenas commented on July 23, 2024

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.

jashkenas avatar jashkenas commented on July 23, 2024

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.

TrevorBurnham avatar TrevorBurnham commented on July 23, 2024

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.

jashkenas avatar jashkenas commented on July 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.