forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redundant_begin.rb
157 lines (132 loc) · 4.03 KB
/
redundant_begin.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks for redundant `begin` blocks.
#
# Currently it checks for code like this:
#
# @example
#
# # bad
# def redundant
# begin
# ala
# bala
# rescue StandardError => e
# something
# end
# end
#
# # good
# def preferred
# ala
# bala
# rescue StandardError => e
# something
# end
#
# # bad
# begin
# do_something
# end
#
# # good
# do_something
#
# # bad
# do_something do
# begin
# something
# rescue => ex
# anything
# end
# end
#
# # good
# # In Ruby 2.5 or later, you can omit `begin` in `do-end` block.
# do_something do
# something
# rescue => ex
# anything
# end
#
# # good
# # Stabby lambdas don't support implicit `begin` in `do-end` blocks.
# -> do
# begin
# foo
# rescue Bar
# baz
# end
# end
class RedundantBegin < Base
include RangeHelp
extend AutoCorrector
MSG = 'Redundant `begin` block detected.'
def on_def(node)
return unless node.body&.kwbegin_type?
register_offense(node.body)
end
alias on_defs on_def
def on_block(node)
return if node.send_node.lambda_literal?
return if node.braces?
return unless node.body&.kwbegin_type?
register_offense(node.body)
end
def on_kwbegin(node)
return if empty_begin?(node) ||
contain_rescue_or_ensure?(node) ||
valid_context_using_only_begin?(node)
register_offense(node)
end
private
def register_offense(node)
offense_range = node.loc.begin
add_offense(offense_range) do |corrector|
if any_ancestor_assignment_node?(node)
replace_begin_with_statement(corrector, offense_range, node)
else
corrector.remove(offense_range)
end
corrector.remove(node.loc.end)
end
end
def replace_begin_with_statement(corrector, offense_range, node)
first_child = node.children.first
source = first_child.source
source = "(#{source})" if first_child.if_type? && first_child.modifier_form?
corrector.replace(offense_range, source)
corrector.remove(range_between(offense_range.end_pos, first_child.source_range.end_pos))
restore_removed_comments(corrector, offense_range, node, first_child)
end
# Restore comments that occur between "begin" and "first_child".
# These comments will be moved to above the assignment line.
def restore_removed_comments(corrector, offense_range, node, first_child)
comments_range = range_between(offense_range.end_pos, first_child.source_range.begin_pos)
comments = comments_range.source
corrector.insert_before(node.parent, comments) unless comments.blank?
end
def empty_begin?(node)
node.children.empty?
end
def contain_rescue_or_ensure?(node)
first_child = node.children.first
first_child.rescue_type? || first_child.ensure_type?
end
def valid_context_using_only_begin?(node)
parent = node.parent
valid_begin_assignment?(node) || parent&.post_condition_loop? ||
parent&.send_type? || parent&.operator_keyword?
end
def valid_begin_assignment?(node)
any_ancestor_assignment_node?(node) && !node.children.one?
end
def any_ancestor_assignment_node?(node)
node.each_ancestor.any?(&:assignment?)
end
end
end
end
end