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

safe-auto-correct breaks the code #10563

Closed
altherlex opened this issue Apr 21, 2022 · 3 comments · Fixed by #10564
Closed

safe-auto-correct breaks the code #10563

altherlex opened this issue Apr 21, 2022 · 3 comments · Fixed by #10564
Labels

Comments

@altherlex
Copy link

Applied rubocop -a and it broken code:

Before:

      consume_diets = positive.select{|p| p.change == "consume" and !all_ignore.include? p.diet.name}.map{|a| [a.disease_measure_name, a.diet]}.uniq.map {|d| d[1]}.compact
      consume_diets2 = positive.select{|p| p.change == "consume" and !all_ignore.include? p.diet.name}.map{|a| [a.disease_measure_name, a.diet]}.group_by{|c| c[1].name} #.map {|d| d[1]}.compact
      consume_diets_by_diet_id = positive.select{|p| p.change == "consume" and !all_ignore.include? p.diet.name}.map{|a| [a.disease_measure_identifier, a.diet]}.group_by{|c| c[1].id} #.map {|d| d[1]}.compact
      consume_diet_ids = consume_diets.map {|d| d.id}

After:

      consume_diets = positive.select do |p|
                        p.change == 'consume' and !all_ignore.include? p.diet.name
                      end.map do |a|
                        [a.disease_measure_name,
                         a.diet]
                      end.uniq.map { |d| d[1] }.compact
      consume_diets2 = # .map {|d| d[1]}.compact
        positive.select do |p|
          p.change == 'consume' and !all_ignore.include? p.diet.name
        end.map
      consume_diets_by_diet_id = # .map {|d| d[1]}.compact
        positive.select do |p|
          p.change == 'consume' and !all_ignore.include? p.diet.name
        end.map
      consume_diet_ids = consume_diets.map { |d| d.id }
@dvandersluis
Copy link
Member

Can you specify which cop caused a problem and what the problem is?

@altherlex
Copy link
Author

problem is that var consume_diets2 and consume_diets_by_diet_id have part of the code deleted

code deleted by rubocop -a (no extra config):

{|a| [a.disease_measure_name, a.diet]}.group_by{|c| c[1].name} #.map {|d| d[1]}.compact

I think the cop is something related to indentation.

Thought to publish it here. It might be a good case scenario to improve even more this awesome gem.

@nobuyo
Copy link
Contributor

nobuyo commented Apr 22, 2022

It seems to be caused by Style/BlockDelimiters if method chain with block and comment like following case:

it 'registers an offense when there is a comment after the closing brace and block body is not empty' do
  expect_offense(<<~RUBY)
    baz.map { |x|
            ^ Avoid using `{...}` for multi-line blocks.
    foo(x) }.map { |x| x.quux } # comment
  RUBY

  expect_correction(<<~RUBY)
    # comment
    baz.map do |x|
    foo(x) end.map { |x| x.quux }
  RUBY
end

The case returns result:

Diff:
@@ -1,6 +1,4 @@
# comment
baz.map do |x|
-foo(x) end.map { |x| x.quux }
+foo(x) end.map

nobuyo added a commit to nobuyo/rubocop that referenced this issue Apr 22, 2022
…block on moving comment if methods with block are chained
@koic koic added the bug label Apr 23, 2022
koic added a commit that referenced this issue Apr 23, 2022
…hen-style-block-delimiters-treats-comment

[Fix #10563] Fix `Style/BlockDelimiters` unexpectedly deletes block
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.

4 participants