Skip to content

Whitespace not handled consistently on + operator #2703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
burlesona opened this issue Feb 16, 2013 · 2 comments
Closed

Whitespace not handled consistently on + operator #2703

burlesona opened this issue Feb 16, 2013 · 2 comments

Comments

@burlesona
Copy link

If you leave a space before a + operator but not after it, the compiler will interpret the + and the string behind it as being passed to as arguments to a function call. Here's an example:

test.coffee

test = "abc"
result = test+"def"
result = test + "def"
result = test+ "def"
result = test +"def"

test.js

// Generated by CoffeeScript 1.4.0
(function() {
  var result, test;

  test = "abc";

  result = test + "def";

  result = test + "def";

  result = test + "def";

  result = test(+"def");

}).call(this);

My first thought was that this must be just a consequence of CS optional parenthesis, but even when adding some parenthesis to try and separate the arguments, if the plus is adjacent to the string the result is the same:

test = "abc"
result = (test) +"def"
result = test +("def")
// Generated by CoffeeScript 1.4.0
(function() {
  var result, test;

  test = "abc";

  result = test(+"def");

  result = test(+"def");

}).call(this);

I think the fact that test +("def") is parsed into test(+"def"); seems especially odd.

Anyway, I wanted to share this since it really surprised me. Lastly, I realize this is not "typical" coding style so it's probably not encountered very often, however I discovered it because I accidentally left out the space after a + in some code today, and the result was TypeError: string is not a function, which was surprising.

Interested to hear whether this is considered a bug, and if not, why not. Thanks!

@vendethiel
Copy link
Collaborator

Why is that odd? CoffeeScript is a white-space significant language, and a b is a(b) (implicit call), nothing inconsistent.
#2595 #2469 #2210 and others

@jashkenas
Copy link
Owner

Interested to hear whether this is considered a bug, and if not, why not. Thanks!

Not. If you expect:

return -x

To return -x, then you should expect the following to pass -x to the callback in CoffeeScript.

callback -x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants