CoffeeScriptRedux
CoffeeScriptRedux copied to clipboard
Semicolon at the end of function def causes next instruction to be inside func scope
Hi! I'm not sure if this behaviour is considered buggy or if it's just a quirk from CS 1.x.
Consider this code:
func = () -> "foo";
class Bar
baz: () -> "foobar"
Notice the semicolon at the end of func. CS 1.x generates this code (which from my point of view, is what should be generated):
// Generated by CoffeeScript 1.6.3
(function() {
var Bar, func;
func = function() {
return "foo";
};
Bar = (function() {
function Bar() {}
Bar.prototype.baz = function() {
return "foobar";
};
return Bar;
})();
}).call(this);
CS Redux 2.0.0-beta6 produces this:
var func;
func = function () {
var Bar;
return Bar = function () {
function Bar() {
}
Bar.prototype.baz = function () {
return 'foobar';
};
return Bar;
}();
};
Notice that CS Redux generates the Bar class inside the func scope.
In my opionion it is inconsistent behaviour that the CS Redux result, that is generated by
func = () ->
"foo"
class Bar
baz: () -> "foobar"
is also generated after a de-indent if there's a stray semicolon.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
From the wiki:
- semicolon operator: Semicolons are semantic. See the discussion in #208. They will still work at the end of most lines, but you should only use them as you would JavaScript's sequence (comma) operator. Using semicolons as line terminators has always been highly discouraged.
I believe the parser is interpreting this as an incomplete sequence and continuing it on the next line.
Yes, when converting my code to work with CS redux I had a lot of errors related to the intentional deviation cited by you, but IMO the behaviour is still inconsistent and error-prone.
@erisdiscord: thanks for answering this.
Closing as wontfix.
@michaelficarra
Thanks for looking into this, but to be honest I think even if it is discouraged to use semicolons at the end of lines, CS Redux shouldn't yield unexpectable results if there's a single character that is not considered an error.
I have to admit I'm seeing a tendency in CS redux to not fix issues like this and leaving hurdles for new Coffeescript users that are hard to debug and even harder to explain. If you think this behaviour is consistent, please explain to me - at the moment I can't understand.
In the long term I'd like to see CS redux as a replacement for the original CoffeeScript. IMO the current redux compiler produces slightly better code even if it's quite slow. Having worked with both compilers for some time, I don't like the tendency of redux dropping a lot of features and therefore requiring people to change their code in order to use redux. But hey, at least the language gets a little cleaner from that (hopefully). But what I really can't understand at all is why inconsistencies like this (IMO it's only a matter of time until someone stumbles over this bug and wastes hours debugging it) won't be fixed.
I think to resolve this (and some related issues), CS redux should either
- Completely disallow semicolons (at least at the end of indented blocks, like functions) or
- Ignore semicolons at the end of indented blocks (keeping maximum compatibility with CS 1.x)
This would also fix a common case of incompatibility with CS 1.x:
class Foo
baz: () -> "foo";
I think it's obvious what the programmer meant to do. CS 1.x compiles that code. CS redux does not. baz is an indented code block (not sure what's the actual term for that is, I consider it an indented code block even if it's inlike like here). If there would be either an error message or the semicolon would be ignored silently, the behaviour would be fine.
I'm not familiar with the internal structure of CS redux and I don't know how difficult this would be to fix - I'm not saying this is in any way urgent or must be fixed immediately - still, I think you should just leave it open. But please, don't introduce additional hurdles for new CS users in any case, that would (at least in my opionion) defeat one of the main purposes of CoffeeScript!
@michaelficarra : Some time ago someone asked if this is a drop-in-replacement (see #162 ). Your answer was (basically) yes. I think it is not a drop-in replacement at all if this won't be fixed.
Thanks and sorry for the way-oversized post.
@ulikoehler +1 long post but I agree all the way.
Re-opening. I'd like to add a parser rule at the end of blocks to look for a semicolon first. If it sees one, it should immediately throw an error. Thanks for that recommendation, @ulikoehler, and sorry for taking so long to get back to you.
Thanks @michaelficarra and @martinheidegger !
@michaelficarra No problem, a month isn't really long, considering some people from other repositories haven't answered after more than a year ;-)