coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Significant last semicolon in a one liner function

Open OnesimusUnbound opened this issue 15 years ago • 22 comments

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.

OnesimusUnbound avatar Mar 11 '11 04:03 OnesimusUnbound

Wow. Looks like a bug to me. Nice find.

michaelficarra avatar Mar 11 '11 05:03 michaelficarra

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?

geraldalewis avatar Aug 30 '11 16:08 geraldalewis

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?

jashkenas avatar Aug 30 '11 16:08 jashkenas

token[1] isnt ';'

AFAIU, that's for -> a; b to parse as (-> a; b). Removing it won't be backward compatible.

satyr avatar Aug 30 '11 19:08 satyr

Yep, that sounds correct.

We could check if the ; is followed by a real TERMINATOR

jashkenas avatar Aug 30 '11 19:08 jashkenas

Look here: https://github.com/jashkenas/coffee-script/blob/1.1.2/src/lexer.coffee#L296

satyr avatar Aug 30 '11 19:08 satyr

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.

geraldalewis avatar Aug 30 '11 20:08 geraldalewis

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.

jashkenas avatar Aug 30 '11 20:08 jashkenas

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.

geraldalewis avatar Aug 30 '11 20:08 geraldalewis

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.

jashkenas avatar Aug 30 '11 20:08 jashkenas

So the patch should:

  • consider ; as a terminator if it's the final token (addresses this issue)
  • consider ;\n as a terminator dedent (just to clarify: this is not the current behavior)

Is that correct?

geraldalewis avatar Aug 30 '11 21:08 geraldalewis

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.

jashkenas avatar Aug 30 '11 21:08 jashkenas

Nice -- thanks @jashkenas!

geraldalewis avatar Aug 30 '11 21:08 geraldalewis

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 like a(), void 0, which simplifies to a() anyway when its value is unused
  • fn (a(); b): fn((a(), b))
  • fn (a();): fn(a()), but I like fn((a(), void 0))
  • -> a(); b: function(){ return a(), b; }

I'd like to see an example where semicolons are not commas.

michaelficarra avatar Aug 30 '11 23:08 michaelficarra

@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 avatar Aug 31 '11 14:08 geraldalewis

@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.

michaelficarra avatar Aug 31 '11 15:08 michaelficarra

related: #1650

michaelficarra avatar Sep 04 '11 04:09 michaelficarra

@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 avatar Sep 04 '11 16:09 geraldalewis

@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.

michaelficarra avatar Sep 04 '11 16:09 michaelficarra

... and I say that as you open #1662

michaelficarra avatar Sep 04 '11 16:09 michaelficarra