Skip to content
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

chainable methods with function multiinline as arguments #34

Closed
francescoagati opened this issue Jan 5, 2010 · 5 comments
Closed

chainable methods with function multiinline as arguments #34

francescoagati opened this issue Jan 5, 2010 · 5 comments
Labels

Comments

@francescoagati
Copy link

i have see that isn't possible having chainable methods with function multiinline as arguments.

$("a").each(
=> $(this).css()
$(this).css()
$(this).css()
).
map(=> $(this).attr("href") ).
end()

this is compiled in this

(function(){
$("a").each(function() {
return $(this).css();
}, $(this).css(), $(this).css()).map(function() {
return $(this).attr("href");
}).end();
})();

and if i use the point for close the function in this mode:

$("a").each(
=> $(this).css()
$(this).css()
$(this).css()
.
).
map(=> $(this).attr("href") ).
end()

i have this error:

ine 5: syntax error for '.'

@jashkenas
Copy link
Owner

Using the latest CoffeeScript, this compiles just fine (despite looking like a mess) -- of course, you have to indent it correctly:

$('a').each(
  =>
    $(this).css()
    $(this).css()
    $(this).css()
).map(
  => $(this).attr('href')
).end()

Compiles into:

$('a').each(function() {
  $(this).css();
  $(this).css();
  return $(this).css();
}).map(function() {
  return $(this).attr('href');
}).end();

Closing the ticket...

@weepy
Copy link

weepy commented Jan 5, 2010

Ah so you can't use the () in this case

$('a').each()
  =>
    $(this).css()
    $(this).css()
    $(this).css()
.map() => $(this).attr('href')

=> syntax error fail

@jashkenas
Copy link
Owner

Yeah, absolutely -- if you want to chain off a function, you'll have to enclose the call with parenthesis. I don't really think that your example should be able to compile. Chaining, block syntax, and significant whitespace don't mix too well. If it could, we could end up with ambiguous looking things like this:

els.each() el =>
  funcs.each() func =>
    func.properties.select() prop =>
      prop.isActive
  .reverse()

Notice how that reverse is two blocks back.

@weepy
Copy link

weepy commented Jan 5, 2010

right, so is it correct that because of the way whitespace works differently to python, you can only close one block per line?

@jashkenas
Copy link
Owner

No, you can close as many as you'd like.

I just think that allowing the above example to compile is asking for trouble. If you're going to call a method on something, just visually speaking, you need to attach that period to the thing that you're calling, not just a level of indentation. Looking at the example, I would have a hard time figuring out what's being reversed.

alangpierce added a commit to alangpierce/coffeescript that referenced this issue Mar 11, 2018
* The start of an artificial CSX call was the start of the tag name instead of
  the start of the `<`, so the range of the decaffeinate node was wrong.
* The artificial `)` after a nested CSX tag was placed so that the end of the
  `)` is one after the end of the `>`. This made nested CSX tags one character
  too long, which broke some interpolation code in decaffeinate-parser.

This is sort of a quick fix and I'll use tests in decaffeinate-parser to verify
correctness. The first change also makes one of the CS error messages worse.
There may be a more robust fix to these issues, but this seems to work for now.
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants