coffeelint icon indicating copy to clipboard operation
coffeelint copied to clipboard

Don't allow empty lines at the end of the file

Open mitar opened this issue 9 years ago • 14 comments

eol_last rule requires a newline character at the end of the file. But on the other hand one might not want empty lines on itself at the end of the file. We should have a rule for that as well. What would be its name?

mitar avatar May 18 '15 00:05 mitar

I don't recommend adding any rules that you personally don't plan on using. It just adds to what needs to be maintained and we don't know if anyone will ever turn it on.

They can either just not turn on eol_last or come implement the rule.

AsaAyers avatar May 18 '15 01:05 AsaAyers

I am plan to use it. :-)

mitar avatar May 18 '15 03:05 mitar

I haven't published a new version yet, so eol_last isn't in the wild. What do you think about adding a mode that could either be require or forbid? And my preference would be to default to require.

AsaAyers avatar May 18 '15 04:05 AsaAyers

I am not sure what you are talking now here. Require or forbid what? An extra empty line at the end? This sounds strange that one would require extra empty line.

mitar avatar May 18 '15 04:05 mitar

Now I'm not sure what you are talking about here :). doesn't eol_last (and eol-last) require that the last line of the file is empty?

foobar\n mode: 'require' says this file is good. foobar\n mode: 'forbid' says this file is bad. foobar\n is a 2 line file

AsaAyers avatar May 18 '15 04:05 AsaAyers

This rule here is to forbid: foobar\n\n (and foobar\n\n\n, and so on).

mitar avatar May 18 '15 04:05 mitar

foobar\n is a 1 line file, BTW. If you think it the terms of eol_last.

mitar avatar May 18 '15 04:05 mitar

oh, that makes more sense. Instead of making it specific to eof I think no-multiple-empty-lines would be ideal for this. I do think it should have a configurable max value, basically exactly the same as eslint's rule :).

I was thinking in terms of what the editor will show and:

> "foobar\n".split("\n").length
2

AsaAyers avatar May 18 '15 04:05 AsaAyers

Yea, so I am planing to do no-multiple-empty-lines as #430. But this here is different. Sorry to be nitpicky, but that is probably normal for people who are using lints. ;-)

So, for me, eol_last is about:

Bad: foobar Good: foobar\n

no-multiple-empty-lines (with max number 1) is about:

Bad: foo\n\n\nbar Good: foo, foo\nbar, foo\n\nbar

And this ticket here is about:

Bad: foo\n\n Good: foo\n

So this ticket is not no-multiple-empty-lines. So at least for me, I am planing to use it so that there is always \n required exactly at the end of the file, but no multiple of them. In the code itself, I want to allow zero or one visual empty lines between lines, but not more than that. So no-multiple-empty-lines with 1 does not catch the foo\n\n because it allows it. It would catch foo\n\n\n though.

One option is to simply change eol_last to catch foo\n\n. And then we do not need this rule here. And then #430 is about empty lines in the code.

mitar avatar May 18 '15 04:05 mitar

I think it's best to just have one rule that handles limiting the number of empty lines. I can't see any reason people would want to keep more than 1 line at the end of the file, so I think it's worth just making it a special case. The last line can be limited to 1 even if you set a higher maximum for that rule.

AsaAyers avatar May 18 '15 13:05 AsaAyers

The multiple empty lines should be covered in the no trailing whitespace. One empty line at the end is acceptable. Two is basically trailing space. Something like this should be caught: \s\s+$

Multiple empty lines between functions seems perfectly reasonable to me and could help in readability:

###
# A function
###
doSomething = ->


###
# Classes
###
class A

It could be used as an additional means of organizing code.

eddiemonge avatar May 18 '15 19:05 eddiemonge

And that's why it's configurable. You probably like having 0-2 blank lines, but you probably don't want 8.

I see your point about it being trailing space. I'll give that some thought.

AsaAyers avatar May 18 '15 19:05 AsaAyers

I think we should just make three rules. eol at the end, number of empty lines at the end, and number of empty lines in between the code.

I do not think we are anywhere looking at newlines as trailing space? Trailing space rule does not catch foo\n\n\n\nbar as trailing space?

mitar avatar May 18 '15 19:05 mitar

:+1: \n{2,}$ (regex syntax, $ being end of file, not end of line) is bad (--> I mean triggers the rule ;-)) and that's it. For me a perfectly valid rule.

doits avatar Jan 26 '16 16:01 doits