Rubocop: Style/IndentationConsistency and extend self

Created on 13 Nov 2017  路  5Comments  路  Source: rubocop-hq/rubocop

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)

Most helpful comment

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

All 5 comments

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

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.

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

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

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

david942j picture david942j  路  3Comments

NobodysNightmare picture NobodysNightmare  路  3Comments

bbugh picture bbugh  路  3Comments

ecbrodie picture ecbrodie  路  3Comments

Aqualon picture Aqualon  路  3Comments