Class: RuboCop::Cop::Obsession::NoParagraphs
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Obsession::NoParagraphs
- Defined in:
- lib/rubocop/cop/obsession/no_paragraphs.rb
Overview
This cop checks for methods with many instructions but no paragraphs.
If your method code has many instructions that are not organized into paragraphs, you should break it up into multiple paragraphs to make the code more breathable and readable. The 3 possible paragraphs themes could be: initialization, action, and result.
Constant Summary collapse
- MSG =
'Method has many instructions and should be broken up into paragraphs.'
- MAX_CONSECUTIVE_INSTRUCTIONS_ALLOWED =
5
Instance Method Summary collapse
- #on_def(node) ⇒ Object (also: #on_defs)
Instance Method Details
#on_def(node) ⇒ Object Also known as: on_defs
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rubocop/cop/obsession/no_paragraphs.rb', line 40 def on_def(node) lines = processed_source.lines[node.first_line..(node.last_line - 2)] return if lines.any?(&:blank?) node_body_type = node&.body&.type return if %i[send if or case array hash].include?(node_body_type) return if %w[asgn str].any? { |string| node_body_type.to_s.include?(string) } too_big = case node_body_type when :begin, :block, :rescue node.body.children.count > MAX_CONSECUTIVE_INSTRUCTIONS_ALLOWED && !node.body.children.all? { |child| child.type.to_s.include?('asgn') } else lines.count > MAX_CONSECUTIVE_INSTRUCTIONS_ALLOWED end add_offense(node) if too_big end |