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

Inline comment after block is removed on safe autocorrect #9902

Closed
themilkman opened this issue Jul 2, 2021 · 0 comments · Fixed by #9913
Closed

Inline comment after block is removed on safe autocorrect #9902

themilkman opened this issue Jul 2, 2021 · 0 comments · Fixed by #9913
Labels

Comments

@themilkman
Copy link
Contributor

When using rubocop --auto-correct --safe test.rb on this file, the whole comment after the block is dropped.

FactoryBot.define do
  factory :foo do
    # This line:
    # raw_article_id { instance.article_id } # ATTENTION: foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar

    # becomes this (not the missing comment!):
    # raw_article_id do
    #   instance.article_id
    # end

    raw_article_id { instance.article_id } # ATTENTION: foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
  end
end

Expected behavior

The comment should be kept - no matter where

Actual behavior

see the ruby code above (shouldn't matter that it is from factory bot I guess)

$ rubocop --auto-correct --safe --debug test.rb 
For /media/sicrepos/sic_dev/github: Default configuration from /home/sweethome/.rvm/gems/ruby-3.0.1/gems/rubocop-1.18.1/config/default.yml
Inspecting 1 file
Scanning /media/sicrepos/sic_dev/github/test.rb
Loading cache from /home/sweethome/.cache/rubocop_cache/a406249fe5b009545c87f1399ff9a9d947889e6d/ecccc5a26c57637834a87a0f8112dae74e190d88/30ea5a4ea576d75ab9538a6292664d1c8e9d4771
C

Offenses:

test.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
FactoryBot.define do
^
test.rb:4:121: C: Layout/LineLength: Line is too long. [136/120]
    # raw_article_id { instance.article_id } # ATTENTION: foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
                                                                                                                        ^^^^^^^^^^^^^^^^
test.rb:11:20: C: [Corrected] Style/BlockDelimiters: Avoid using {...} for multi-line blocks.
    raw_article_id {
                   ^
test.rb:11:121: C: [Corrected] Layout/LineLength: Line is too long. [134/120]
    raw_article_id { instance.article_id } # ATTENTION: foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
                                                                                                                        ^^^^^^^^^^^^^^
test.rb:12:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 1) spaces for indentation.
 instance.article_id
^
test.rb:12:3: C: [Corrected] Layout/IndentationWidth: Use 2 (not -2) spaces for indentation.
  instance.article_id
  ^^
test.rb:12:22: C: [Corrected] Layout/BlockEndNewline: Expression at 12, 22 should be on its own line.
 instance.article_id } # ATTENTION: foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
                     ^
test.rb:13:1: C: [Corrected] Layout/BlockAlignment: } at 13, 0 is not aligned with raw_article_id { at 11, 4.
}     # ATTENTION: foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
^
test.rb:13:13: C: [Corrected] Style/CommentedKeyword: Do not place comments on the same line as the end keyword.
    end     # ATTENTION: foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 9 offenses detected, 7 offenses corrected, 1 more offense can be corrected with `rubocop -A`
Finished in 0.17514862899997752 seconds

Steps to reproduce the problem

Take the ruby code above in a test.rb and use the command line as mentioned.

RuboCop version

$ rubocop -V
1.18.1 (using Parser 3.0.1.1, rubocop-ast 1.7.0, running on ruby 3.0.1 x86_64-linux)

Cheers and thanks for all your work!

@koic koic added the bug label Jul 5, 2021
koic added a commit to koic/rubocop that referenced this issue Jul 6, 2021
…miters`

Fixes rubocop#9902

This PR fixes an incorrect auto-correct for `Style/BlockDelimiters`
when there is a comment after the closing brace.
It prevents `Style/CommentedKeyword` from removing source code
comment after `end` keyword.
bbatsov pushed a commit that referenced this issue Jul 6, 2021
Fixes #9902

This PR fixes an incorrect auto-correct for `Style/BlockDelimiters`
when there is a comment after the closing brace.
It prevents `Style/CommentedKeyword` from removing source code
comment after `end` keyword.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants