-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoo_many_paragraphs.rb
56 lines (53 loc) · 1.86 KB
/
too_many_paragraphs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true
module RuboCop
module Cop
module Obsession
# This cop checks for methods with too many paragraphs.
#
# You should organize your method code into 2 to 3 paragraphs maximum.
# The 3 possible paragraphs themes could be: initialization, action,
# and result. Shape your paragraph according to those themes. Spec code
# should also be organized into 2 to 3 paragraphs
# (initialization/action/result or given/when/then paragraphs).
#
# After organizing your paragraphs, if they are too long or dense, it
# could be a sign that they should be broken into smaller methods.
#
# @example
#
# # bad
# def set_seo_content
# return if seo_content.present?
#
# template = SeoTemplate.find_by(template_type: 'BlogPost')
#
# return if template.blank?
#
# self.seo_content = build_seo_content(seo_template: template, slug: slug)
#
# Rails.logger.info('Content has been set')
# end
#
# # good
# def set_seo_content
# return if seo_content.present?
# template = SeoTemplate.find_by(template_type: 'BlogPost')
# return if template.blank?
#
# self.seo_content = build_seo_content(seo_template: template, slug: slug)
#
# Rails.logger.info('Content has been set')
# end
class TooManyParagraphs < Base
MSG = 'Organize method into 2 to 3 paragraphs (init, action, result).'
MAX_PARAGRAPHS = 3
def on_def(node)
lines = processed_source.lines[node.first_line..(node.last_line - 2)]
blank_lines_count = lines.count(&:blank?)
add_offense(node) if blank_lines_count >= MAX_PARAGRAPHS
end
alias_method :on_defs, :on_def
end
end
end
end