Significant last semicolon in a one liner function
In version 1.0.1, the last semicolon in a one liner function causes a parser error unclosed INDENT on line 1.
greet = (name) -> "hello #{name}";
Is this intentional (feature) or a bug? If this is intentional, then message is really not helpful in pointing out the cause of error.
Wow. Looks like a bug to me. Nice find.
Looking into this. The issue is in Rewriter#addImplicitIndentation:
condition = (token, i) ->
token[1] isnt ';' and token[0] in SINGLE_CLOSERS and
not (token[0] is 'ELSE' and starter not in ['IF', 'THEN'])
Removing token[1] isnt ';' allows the example function to compile correctly, but then breaks when building Coffee.
I'm not sure why the test for ; was introduced -- I tracked the introduction of the test to 561a02c, which references "jots multiline function case". I tried digging it up by searching through the Issues, but it almost looks as if a bunch of posts vanished/were deleted around Jan/Feb 2010. @jashkenas, are you able to shed some light?
I'm afraid that I'm not able to shed any light on missing issues -- as far as I know, there's no way to delete them. Perhaps worth raising with support.github.com?
token[1] isnt ';'
AFAIU, that's for -> a; b to parse as (-> a; b). Removing it won't be backward compatible.
Yep, that sounds correct.
We could check if the ; is followed by a real TERMINATOR
Look here: https://github.com/jashkenas/coffee-script/blob/1.1.2/src/lexer.coffee#L296
Is this the expected output:
-> a;
b
(function() {
a; return b;
});
Seems like a possible interpretation of that (which may be less confusing to newcomers) would be:
(function() {
return a;
});
b;
or error on ;\n ?
... This feels like something that'd have some issues attached to it already... going to go look.
I'd argue that if there's no indentation for b, it should be outside the function ... and if there is indentation, it should be inside.
I like how @michaelficarra put it in #1417 "Semicolons are the coffeescript version of commas."
However, the current behavior is that they can act as both the comma operator, and as a terminator depending on the context.
Also, I agree with @jashkenas that:
-> a;
b
b should not be part of the function body unless it's indented. In that case -> a; should be considered an error, equivalent to: function(){ a, }
Edit: In that case, this issue wouldn't be considered a bug, and it'd entail a little cleanup work for cases like
->
-> a;
that don't currently produce an error.
No -- I don't think it should be an error if we can help it. Semicolons aren't commas -- they're an optional way to end a line early. If you put them right before the true end of the line, it's always redundant ... but shouldn't be a syntax error.
So the patch should:
- consider
;as aterminatorif it's the final token (addresses this issue) - consider
;\nas aterminatordedent(just to clarify: this is not the current behavior)
Is that correct?
consider ; as a terminator if it's the final token (addresses this issue)
... either that or ignore it, as it's followed immediately by a terminator, indent, or dedent.
consider ;\n as a terminator dedent (just to clarify: this is not the current behavior)
I think that, as above, it would just be the same as whatever the whitespace tells you ... ignore the terminator, and let the whitespace indent, dedent, or terminate as usual.
Nice -- thanks @jashkenas!
Semicolons aren't commas -- they're an optional way to end a line early
I'm not so sure about that. For now, I think they're commas, but empty RHS expressions cause the operator to return the value of the LHS (even though I think void 0 -- the RHS -- would be more appropriate sometimes). Some examples of semicolons behaving as commas:
a(); b():a(), b()a();:a(), but I likea(), void 0, which simplifies toa()anyway when its value is unusedfn (a(); b):fn((a(), b))fn (a();):fn(a()), but I likefn((a(), void 0))-> a(); b:function(){ return a(), b; }
I'd like to see an example where semicolons are not commas.
@michaelficarra, I might be missing something, but some of your examples are producing different results in Coffee, e.g.:
-> a(); b # function(){ return a(), b; }
actually produces:
function() { a(); return b; }
and:
a(); b() # a(), b()
produces:
a(); b();
Interestingly, when these examples are wrapped in parens on the RHS of an assignment, they do indeed produce with ,.
@geraldalewis: those are both equivalent compilations. I was just showing what the compilations would look like if they were more directly compiled using the comma operator. Everything's an expression (almost...), so when the value of a sequence of expressions is used, the comma operator is a straightforward and appropriate compilation.
But I don't want to derail this issue any more. I'm willing to talk about it on IRC any time anyone wants, just PM me and I'll get on.
related: #1650
@jashkenas suggested lexing the semicolons out before they even hit the rewriter, so I created a new branch/new pull request using that solution (#1662). Pull #1650 should remain closed :)
@geraldalewis: I don't see a new pull request from you. Are you sure you opened one? Either way, you should add a link in here.
... and I say that as you open #1662