class RuboCop::Cop::Style::EmptyMethod
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.
NOTE: Autocorrection will not be applied for the ‘compact` style if the resulting code is longer than the `Max` configuration for `Layout/LineLength`, but an offense will still be registered.
@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
@example 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_def(node)
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 54 def on_def(node) return if node.body || processed_source.contains_comment?(node.source_range) return if correct_style?(node) add_offense(node) do |corrector| correction = corrected(node) next if compact_style? && max_line_length && correction.size > max_line_length corrector.replace(node, correction) end end
Also aliased as: on_defs
Private Instance Methods
compact?(node)
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 95 def compact?(node) node.single_line? end
compact_style?()
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 103 def compact_style? style == :compact end
correct_style?(node)
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 73 def correct_style?(node) (compact_style? && compact?(node)) || (expanded_style? && expanded?(node)) end
corrected(node)
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 77 def corrected(node) scope = node.receiver ? "#{node.receiver.source}." : '' arguments = if node.arguments? args = node.arguments.map(&:source).join(', ') parentheses?(node.arguments) ? "(#{args})" : " #{args}" end signature = [scope, node.method_name, arguments].join ["def #{signature}", 'end'].join(joint(node)) end
expanded?(node)
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 99 def expanded?(node) node.multiline? end
expanded_style?()
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 107 def expanded_style? style == :expanded end
joint(node)
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 89 def joint(node) indent = ' ' * node.loc.column compact_style? ? '; ' : "\n#{indent}" end
max_line_length()
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 111 def max_line_length return unless config.for_cop('Layout/LineLength')['Enabled'] config.for_cop('Layout/LineLength')['Max'] end
message(_range)
click to toggle source
# File lib/rubocop/cop/style/empty_method.rb, line 69 def message(_range) compact_style? ? MSG_COMPACT : MSG_EXPANDED end