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/IndentationConsistency and extend self #5050

Closed
dreyks opened this issue Nov 13, 2017 · 5 comments
Closed

Style/IndentationConsistency and extend self #5050

dreyks opened this issue Nov 13, 2017 · 5 comments

Comments

@dreyks
Copy link
Contributor

dreyks commented Nov 13, 2017

In my current project we have a weird convention to write extend self on the same line as the module definition, which turns out to be valid ruby

module Asd extend self
  def some_method
    p 'something'
  end
end

Expected behavior

I'd expect rubocop not to touch the indentation here

Actual behavior

The module body gets aligned with the extend keyword

module Asd extend self
           def some_method
             p 'something'
           end
end

Steps to reproduce the problem

see the code template above

RuboCop version

Include the output of rubocop -V. Here's an example:

$ rubocop -V
0.51.0 (using Parser 2.4.0.0, running on ruby 2.4.2 x86_64-darwin17)
@michniewicz
Copy link
Contributor

michniewicz commented Nov 13, 2017

Guess we need autocorrect for cop that detects module's extend self with module_function https://github.com/bbatsov/ruby-style-guide#module-function

btw if extend self goes to the next line, the cop works as expected

module Asd
  extend self
  def some_method; end
end

@Drenmi
Copy link
Collaborator

Drenmi commented Nov 23, 2017

I am inclined to say we should add a special case for this. The code provided intentionally obscures what is actually going on. Adding complexity to support that is quite contrary to what RuboCop does. 🙂

We recently added a cop that checks that a method doesn't have trailing implementation on the def line:

def foo; bar
  baz
end

We probably want a similar cop for class and module, as well as the cop proposed by @michniewicz.

@garettarrowood
Copy link
Contributor

These look like fun! Will attempt to get the work up over the next few days. 3 PRs.

  1. Add Style/TrailingBodyOnClassDefinition cop
  2. Add Style/TrailingBodyOnModuleDefinition cop
  3. Add autocorrection to Style/ModuleFunction cop

@dreyks
Copy link
Contributor Author

dreyks commented Jan 20, 2018

@bbatsov how does Add auto-correction to Style/ModuleFunction fix the initial issue?

@garettarrowood
Copy link
Contributor

@dreyks - Sorry! Based on the follow up commentary above, this is how I interpreted the issue. I'm fine with opening this back up if that's what we decide.

Quotes:
"... if extend self goes to the next line, the cop works as expected"
"... We recently added a cop that checks that a method doesn't have trailing implementation on the def line: ... We probably want a similar cop for class and module "
"... The code provided intentionally obscures what is actually going on. Adding complexity to support that is quite contrary to what RuboCop does. "

I put this together to mean that we didn't want to add complexity that supports an obscure practice. And instead, let's provide linting and autocorrection that re-aligns the provided code to follow standard practices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants