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
I'd expect rubocop not to touch the indentation here
The module body gets aligned with the extend keyword
module Asd extend self
def some_method
p 'something'
end
end
see the code template above
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)
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.
Style/TrailingBodyOnClassDefinition copStyle/TrailingBodyOnModuleDefinition copStyle/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.
Most helpful comment
Guess we need autocorrect for cop that detects module's
extend selfwithmodule_functionhttps://github.com/bbatsov/ruby-style-guide#module-functionbtw if
extend selfgoes to the next line, the cop works as expected