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/ClassMethodsDefinitions is wrongly creating an instance method #8663

Closed
tolbard opened this issue Sep 8, 2020 · 0 comments · Fixed by #8687
Closed

Style/ClassMethodsDefinitions is wrongly creating an instance method #8663

tolbard opened this issue Sep 8, 2020 · 0 comments · Fixed by #8687
Labels

Comments

@tolbard
Copy link

tolbard commented Sep 8, 2020

I use autocorrect on a Style/ClassMethodsDefinitions issue and it wrongly restructures the code

Version rubocop (0.90.0)


Expected behavior

class Repository
  attr_reader :causation_id, :correlation_id

  def self.with_causation_id(causation_id)
    new(causation_id: causation_id, correlation_id: causation_id)
  end

  def self.with_event(event)
    new(causation_id: event.id, correlation_id: event.correlation_id)
  end

  def initialize(causation_id:, correlation_id:)

Actual behavior

Major issues:

  • def with_causation_id(causation_id) is not remove
  • class << self end empty block stay in the code

Minor Issues:

  • an extra empty line is created
  • indentation is wrong
class Repository
  attr_reader :causation_id, :correlation_id

  
    def self.with_causation_id(causation_id)
      new(causation_id: causation_id, correlation_id: causation_id)
    end

  
    def self.with_event(event)
      new(causation_id: event.id, correlation_id: event.correlation_id)
    end

    def with_causation_id(causation_id)
      new(causation_id: causation_id, correlation_id: causation_id)
    end

  class << self  end

  def initialize(causation_id:, correlation_id:)
  ....

Steps to reproduce the problem

Starting code:

class Repository
  attr_reader :causation_id, :correlation_id

  class << self
    def with_event(event)
      new(causation_id: event.id, correlation_id: event.correlation_id)
    end

    def with_causation_id(causation_id)
      new(causation_id: causation_id, correlation_id: causation_id)
    end
  end

  def initialize ....

RuboCop version

$ rubocop -V
0.90.0 (using Parser 2.7.1.4, rubocop-ast 0.3.0, running on ruby 2.6.6 x86_64-darwin18)
@koic koic added the bug label Sep 8, 2020
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Sep 11, 2020
Autocorrection was rewritten, which notably moves the offense for def_self style to the sclass itself, in order to properly handle each method within it without getting clobbering errors.

Autocorrection fixes:
* Handle multiple methods within class << self
* Remove extra whitespace
* Fix indentation
* Remove self << class if empty
bbatsov added a commit that referenced this issue Sep 15, 2020
Autocorrection was rewritten, which notably moves the offense for def_self style to the sclass itself, in order to properly handle each method within it without getting clobbering errors.

Autocorrection fixes:
* Handle multiple methods within class << self
* Remove extra whitespace
* Fix indentation
* Remove self << class if empty

Co-authored-by: Bozhidar Batsov <bozhidar@batsov.com>
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