Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I indent list comprehension in CoffeeScript?

I have the following line of CoffeeScript:

names = (mail.folder for mail in @data when mail.service_name is service.name).unique()

This line is too long, so it won't pass linting by CoffeeLint.

I'm trying to break it, but I always get indentation errors by CoffeeLint.

What is the proper way to indent this?

like image 513
Salvatore Iovene Avatar asked Jun 28 '12 09:06

Salvatore Iovene


1 Answers

This is the most readable version of that without getting overly lengthy:

names =
  (for mail in @data when mail.service_name is service.name
    mail.folder).unique()

You can't split list comprehensions over multiple lines, but a normal for loop can also return a value, so using one of those solves the problem. If you're willing to grant an extra line, there's no need for the awkward parentheses around the loop:

names =
  for mail in @data when mail.service_name is service.name
    mail.folder
names = names.unique()

Finally, the indentation of the for line is up to you; I find my first version more readable but this is also valid:

names =
(for mail in @data when mail.service_name is service.name
  mail.folder).unique()
like image 124
Aaron Dufour Avatar answered Sep 27 '22 22:09

Aaron Dufour