Class: RuboCop::Cop::Sane::NoMethodCallAfterEnd

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sane/no_method_call_after_end.rb

Overview

Prohibits calling methods directly after ‘end`.

This cop detects method calls chained directly on blocks, conditionals, and other structures that end with ‘end`. Such code is harder to read and should be refactored to assign the result to a variable first.

Examples:

# bad
if condition
  value
end.foo

# bad
if condition
  value
end&.foo

# bad
array.map do |item|
  transform(item)
end.compact

# good
result = if condition
  value
end
result.foo

# good
result = array.map do |item|
  transform(item)
end
result.compact

Constant Summary collapse

MSG =
"Do not call methods directly after `end`."
END_KEYWORD_NODES =
[
  :if, :case, :case_match, :while, :until, :for,
  :kwbegin, :block, :numblock,
  :def, :defs, :class, :module, :sclass,
].freeze
BLOCK_NODES =
[:block, :numblock].freeze

Instance Method Summary collapse

Instance Method Details

#on_csend(node) ⇒ Object



55
56
57
# File 'lib/rubocop/cop/sane/no_method_call_after_end.rb', line 55

def on_csend(node)
  check_method_call_after_end(node)
end

#on_send(node) ⇒ Object



51
52
53
# File 'lib/rubocop/cop/sane/no_method_call_after_end.rb', line 51

def on_send(node)
  check_method_call_after_end(node)
end