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

EmptyLines removes empty lines from heredoc strings #815

Closed
ngan opened this issue Feb 13, 2014 · 6 comments
Closed

EmptyLines removes empty lines from heredoc strings #815

ngan opened this issue Feb 13, 2014 · 6 comments

Comments

@ngan
Copy link

ngan commented Feb 13, 2014

Not sure if this is a bug or feature, but the EmptyLines cop seems to not like blank lines in heredoc strings.

str = <<-TEXT
line 1


line 4
TEXT
@bbatsov
Copy link
Collaborator

bbatsov commented Feb 13, 2014

I cannot reproduce this. What version of RuboCop are you using?

@ngan
Copy link
Author

ngan commented Feb 13, 2014

rubocop (0.18.1)

@ngan
Copy link
Author

ngan commented Feb 13, 2014

➜  cat test.rb 
str = <<-TEXT
line 1


line 2
TEXT
puts str
➜  rubocop test.rb 
Inspecting 1 file
C

Offences:

test.rb:4:1: C: Extra blank line detected.

1 file inspected, 1 offence detected

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 13, 2014

Ah, yes. Adding the puts after the heredoc triggers a false positive indeed. I'll have that fixed.

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 13, 2014

Seems that the tokens in the source are mixed up, which is causing the problem. The newline on the first line appear in the token stream just before the tokens of puts str. I'm not sure if this is a RuboCop bug or a Parser bug. @yujinakayama @jonas054 What do you think? Should we just sort the tokens by line number (which would solve this particular problem)?

@yujinakayama
Copy link
Collaborator

I guess this is not a Parser bug.

The @ngan's source:

str = <<-TEXT
line 1


line 2
TEXT
puts str

is tokenized as:

"str"      line:1 type:tIDENTIFIER
"="        line:1 type:tEQL
"<<\""     line:1 type:tSTRING_BEG
"line 1\n" line:2 type:tSTRING_CONTENT
"\n"       line:3 type:tSTRING_CONTENT
"\n"       line:4 type:tSTRING_CONTENT
"line 2\n" line:5 type:tSTRING_CONTENT
"TEXT"     line:6 type:tSTRING_END
nil        line:1 type:tNL             <- This causes the bug.
"puts"     line:7 type:tIDENTIFIER
"str"      line:7 type:tIDENTIFIER
nil        line:7 type:tNL

However, we can parenthesize heredocs like this:

puts(<<-TEXT)
line 1


line 2
TEXT
puts 'foo'

and its tokens are:

"puts"     line:1 type:tIDENTIFIER
"("        line:1 type:tLPAREN2
"<<\""     line:1 type:tSTRING_BEG
"line 1\n" line:2 type:tSTRING_CONTENT
"\n"       line:3 type:tSTRING_CONTENT
"\n"       line:4 type:tSTRING_CONTENT
"line 2\n" line:5 type:tSTRING_CONTENT
"TEXT"     line:6 type:tSTRING_END
")"        line:1 type:tRPAREN
nil        line:1 type:tNL
"puts"     line:7 type:tIDENTIFIER
"foo"      line:7 type:tSTRING
nil        line:7 type:tNL

I think this is natural order.

Should we just sort the tokens by line number (which would solve this particular problem)?

I think so. Or ignore line numbers that are less than the max line number seen so far.

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

No branches or pull requests

3 participants