class RubbyCop::Cop::Style::EmptyMethod
This cop checks for the formatting of empty method definitions. By default it enforces empty method definitions to go on a single line (compact style), but it can be configured to enforce the `end` to go on its own line (expanded style.)
Note: A method definition is not considered empty if it contains
comments.
@example
EnforcedStyle: compact (default) @bad def foo(bar) end def self.foo(bar) end @good def foo(bar); end def foo(bar) # baz end def self.foo(bar); end EnforcedStyle: expanded @bad def foo(bar); end def self.foo(bar); end @good def foo(bar) end def self.foo(bar) end
Constants
- MSG_COMPACT
- MSG_EXPANDED
Public Instance Methods
on_method_def(node, _method_name, _args, body)
click to toggle source
# File lib/rubbycop/cop/style/empty_method.rb, line 50 def on_method_def(node, _method_name, _args, body) return if body || comment_lines?(node) return if compact_style? && compact?(node) return if expanded_style? && expanded?(node) add_offense(node, node.source_range, message) end
Private Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubbycop/cop/style/empty_method.rb, line 60 def autocorrect(node) lambda do |corrector| corrector.replace(node.source_range, corrected(node)) end end
comment_lines?(node)
click to toggle source
# File lib/rubbycop/cop/style/empty_method.rb, line 80 def comment_lines?(node) processed_source[line_range(node)].any? { |line| comment_line?(line) } end
compact?(node)
click to toggle source
# File lib/rubbycop/cop/style/empty_method.rb, line 84 def compact?(node) node.single_line? end
compact_style?()
click to toggle source
# File lib/rubbycop/cop/style/empty_method.rb, line 92 def compact_style? style == :compact end
corrected(node)
click to toggle source
# File lib/rubbycop/cop/style/empty_method.rb, line 70 def corrected(node) method_name, args, _body, scope = method_def_node_parts(node) arguments = args.source unless args.children.empty? joint = compact_style? ? '; ' : "\n" scope = scope ? 'self.' : '' ["def #{scope}#{method_name}#{arguments}", 'end'].join(joint) end
expanded?(node)
click to toggle source
# File lib/rubbycop/cop/style/empty_method.rb, line 88 def expanded?(node) node.multiline? end
expanded_style?()
click to toggle source
# File lib/rubbycop/cop/style/empty_method.rb, line 96 def expanded_style? style == :expanded end
message()
click to toggle source
# File lib/rubbycop/cop/style/empty_method.rb, line 66 def message compact_style? ? MSG_COMPACT : MSG_EXPANDED end