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

Layout/IndentHeredoc with wrong output #4662

Closed
daixiang0 opened this issue Aug 14, 2017 · 6 comments
Closed

Layout/IndentHeredoc with wrong output #4662

daixiang0 opened this issue Aug 14, 2017 · 6 comments
Labels
enhancement good first issue Easy task, suitable for newcomers to the project

Comments

@daixiang0
Copy link

daixiang0 commented Aug 14, 2017

My ruby code is as below:

    stdout = <<~END  # i am sure this line has aligned, 4 spaces at the beginning
2017-03-22 09:28:24 sudo python width_and_depth_tests.py
Width test: PASSED
Depth test: FAILED
END

Rubpcop output is:

C: Layout/IndentHeredoc: Use 2 spaces for indentation in a heredoc by using <<~ instead of <<~.

So I add 2 spaces for 3 lines like below:

    stdout = <<~END  # i am sure this line has aligned
  2017-03-22 09:28:24 sudo python width_and_depth_tests.py
  Width test: PASSED
  Depth test: FAILED
END

Still same output, then i read the doc and it align with <<~, then i try and also fail.
I think the output is not clear or even wrong.

@pocke
Copy link
Collaborator

pocke commented Aug 14, 2017

This cop says this code should be aligned like the following.

    stdout = <<~END
      2017-03-22 09:28:24 sudo python width_and_depth_tests.py
      Width test: PASSED
      Depth test: FAILED
END

The 2 space means 2 space * indentation level.
For example:

    stdout = <<~END # This line is indented by 2 * 2 space
      2017-03-22 09:28:24 sudo python width_and_depth_tests.py # This line is indented by 2 * 3 space (+1 level)
      Width test: PASSED
      Depth test: FAILED
END

@daixiang0
Copy link
Author

Thanks, i misunderstand it.

@daixiang0
Copy link
Author

But If i do need a blank in the beginning of EOF iike:

<<~EOF

  Hi!
EOF

How to fix this cop?

@pocke
Copy link
Collaborator

pocke commented Aug 14, 2017

Hmm, It is a bug of this cop.
Currently this cop adds an offense for the code, but I think this cop should not add offense.

@mikegee
Copy link
Contributor

mikegee commented Aug 14, 2017

Let's reconsider the message in this case too: … by using <<~ instead of <<~.

@daixiang0
Copy link
Author

I think so, this message may make people confused.

@Drenmi Drenmi added enhancement good first issue Easy task, suitable for newcomers to the project labels Sep 17, 2017
sambostock added a commit to sambostock/rubocop that referenced this issue Nov 16, 2017
Checking indentation of heredocs would fail if the first line was empty,
with a nonsense message:

    <<~HEREDOC

      foo
    HEREDOC

    # Use 2 spaces for indentation in a heredoc by using `<<~` instead of `<<~`.

Indentations were detected as leading contiguous whitespace. In the
example above, this would be "\n  ", which fails the check due to being
3 characters long.

By first splitting lines, we are can safely rely on the leading
contiguous whitespace being the indent.

Resolves: rubocop#4662
See also: rubocop#4465
bbatsov pushed a commit that referenced this issue Nov 17, 2017
Checking indentation of heredocs would fail if the first line was empty,
with a nonsense message:

    <<~HEREDOC

      foo
    HEREDOC

    # Use 2 spaces for indentation in a heredoc by using `<<~` instead of `<<~`.

Indentations were detected as leading contiguous whitespace. In the
example above, this would be "\n  ", which fails the check due to being
3 characters long.

By first splitting lines, we are can safely rely on the leading
contiguous whitespace being the indent.

Resolves: #4662
See also: #4465
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue Easy task, suitable for newcomers to the project
Projects
None yet
Development

No branches or pull requests

4 participants