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

Style/ClassAndModuleChildren cop incorrectly corrects parent class to module #9128

Closed
lopopolo opened this issue Nov 30, 2020 · 0 comments · Fixed by #9131
Closed

Style/ClassAndModuleChildren cop incorrectly corrects parent class to module #9128

lopopolo opened this issue Nov 30, 2020 · 0 comments · Fixed by #9131
Labels

Comments

@lopopolo
Copy link

lopopolo commented Nov 30, 2020

RuboCop incorrectly expands compact style class reopenings to use module for the parent if the parent is a Class.


Expected behavior

Describe here how you expected RuboCop to behave in this particular situation.

Input

# frozen_string_literal: true

# Foo class
class Foo
  # Foo::Bar class
  class Bar
    # Foo::Bar#baz method
    def baz
      'baz'
    end
  end
end

class Foo::Bar
  # Foo::Bar#quux method
  def quux
    'quux'
  end
end

Expected output

# frozen_string_literal: true

# Foo class
class Foo
  # Foo::Bar class
  class Bar
    # Foo::Bar#baz method
    def baz
      'baz'
    end
  end
end

class Foo
  class Bar
    # Foo::Bar#quux method
    def quux
      'quux'
    end
  end
end

Actual behavior

Describe here what actually happened.
Please use rubocop --debug when pasting rubocop output as it contains additional information.

Input

# frozen_string_literal: true

# Foo class
class Foo
  # Foo::Bar class
  class Bar
    # Foo::Bar#baz method
    def baz
      'baz'
    end
  end
end

class Foo::Bar
  # Foo::Bar#quux method
  def quux
    'quux'
  end
end

Expected output

# frozen_string_literal: true

# Foo class
class Foo
  # Foo::Bar class
  class Bar
    # Foo::Bar#baz method
    def baz
      'baz'
    end
  end
end

module Foo
  class Bar
    # Foo::Bar#quux method
    def quux
      'quux'
    end
  end
end

Executing the bad correction code

$ cat rubocop_compact_autocorrect_bad.rb
# frozen_string_literal: true

# Foo class
class Foo
  # Foo::Bar class
  class Bar
    # Foo::Bar#baz method
    def baz
      'baz'
    end
  end
end

module Foo
  class Bar
    # Foo::Bar#quux method
    def quux
      'quux'
    end
  end
end
$ ruby rubocop_compact_autocorrect_bad.rb
Traceback (most recent call last):
rubocop_compact_autocorrect_bad.rb:14:in `<main>': Foo is not a module (TypeError)

Steps to reproduce the problem

This is extremely important! Providing us with a reliable way to reproduce
a problem will expedite its solution.

bundle exec rubocop -A [source]

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rspec, and others)
output by rubocop -V, include them as well. Here's an example:

$ bundle exec rubocop -V
1.3.1 (using Parser 2.7.2.0, rubocop-ast 1.1.1, running on ruby 2.6.6 x86_64-darwin20)
@koic koic added the bug label Dec 1, 2020
koic added a commit to koic/rubocop that referenced this issue Dec 1, 2020
…oduleChildren`

Fixes rubocop#9128.

This PR fixes an incorrect auto-correct for `Style/ClassAndModuleChildren`
when namespace is defined as a class in the same file.

It is still undetectable when class definition exists in the different file,
but it can be detected in the same file. This PR solves the latter.
bbatsov pushed a commit that referenced this issue Dec 1, 2020
…ildren`

Fixes #9128.

This PR fixes an incorrect auto-correct for `Style/ClassAndModuleChildren`
when namespace is defined as a class in the same file.

It is still undetectable when class definition exists in the different file,
but it can be detected in the same file. This PR solves the latter.
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