forked from rubocop/rubocop
/
empty_block.rb
98 lines (89 loc) · 2.7 KB
/
empty_block.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for blocks without a body.
# Such empty blocks are typically an oversight or we should provide a comment
# be clearer what we're aiming for.
#
# Empty lambdas and procs are ignored by default.
#
# NOTE: For backwards compatibility, the configuration that allows/disallows
# empty lambdas and procs is called `AllowEmptyLambdas`, even though it also
# applies to procs.
#
# @example
# # bad
# items.each { |item| }
#
# # good
# items.each { |item| puts item }
#
# @example AllowComments: true (default)
# # good
# items.each do |item|
# # TODO: implement later (inner comment)
# end
#
# items.each { |item| } # TODO: implement later (inline comment)
#
# @example AllowComments: false
# # bad
# items.each do |item|
# # TODO: implement later (inner comment)
# end
#
# items.each { |item| } # TODO: implement later (inline comment)
#
# @example AllowEmptyLambdas: true (default)
# # good
# allow(subject).to receive(:callable).and_return(-> {})
#
# placeholder = lambda do
# end
# (callable || placeholder).call
#
# proc { }
#
# Proc.new { }
#
# @example AllowEmptyLambdas: false
# # bad
# allow(subject).to receive(:callable).and_return(-> {})
#
# placeholder = lambda do
# end
# (callable || placeholder).call
#
# proc { }
#
# Proc.new { }
#
class EmptyBlock < Base
MSG = 'Empty block detected.'
def on_block(node)
return if node.body
return if allow_empty_lambdas? && lambda_or_proc?(node)
return if cop_config['AllowComments'] && allow_comment?(node)
add_offense(node)
end
private
def allow_comment?(node)
return false unless processed_source.contains_comment?(node.source_range)
line_comment = processed_source.comment_at_line(node.source_range.line)
!line_comment || !comment_disables_cop?(line_comment.loc.expression.source)
end
def allow_empty_lambdas?
cop_config['AllowEmptyLambdas']
end
def comment_disables_cop?(comment)
regexp_pattern = "# rubocop : (disable|todo) ([^,],)* (all|#{cop_name})"
Regexp.new(regexp_pattern.gsub(' ', '\s*')).match?(comment)
end
def lambda_or_proc?(node)
node.lambda? || node.proc?
end
end
end
end
end