rubocop 0.67.2 → 0.68.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +86 -233
- data/exe/rubocop +0 -12
- data/lib/rubocop.rb +13 -30
- data/lib/rubocop/ast/builder.rb +4 -0
- data/lib/rubocop/ast/node/alias_node.rb +24 -0
- data/lib/rubocop/ast/node/class_node.rb +31 -0
- data/lib/rubocop/ast/node/module_node.rb +24 -0
- data/lib/rubocop/ast/node/range_node.rb +7 -0
- data/lib/rubocop/ast/node/resbody_node.rb +12 -0
- data/lib/rubocop/ast/node/self_class_node.rb +24 -0
- data/lib/rubocop/cli.rb +40 -4
- data/lib/rubocop/config.rb +9 -7
- data/lib/rubocop/config_loader.rb +48 -7
- data/lib/rubocop/config_loader_resolver.rb +5 -4
- data/lib/rubocop/cop/commissioner.rb +24 -0
- data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +18 -6
- data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +12 -14
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +9 -20
- data/lib/rubocop/cop/layout/align_arguments.rb +93 -0
- data/lib/rubocop/cop/layout/align_parameters.rb +57 -33
- data/lib/rubocop/cop/layout/class_structure.rb +5 -5
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +6 -8
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +3 -6
- data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +1 -2
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -0
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +292 -0
- data/lib/rubocop/cop/layout/{first_parameter_indentation.rb → indent_first_argument.rb} +11 -12
- data/lib/rubocop/cop/layout/{indent_array.rb → indent_first_array_element.rb} +2 -2
- data/lib/rubocop/cop/layout/{indent_hash.rb → indent_first_hash_element.rb} +2 -2
- data/lib/rubocop/cop/layout/indent_first_parameter.rb +96 -0
- data/lib/rubocop/cop/layout/indentation_width.rb +4 -16
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -4
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -16
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -2
- data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -8
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +4 -8
- data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +157 -0
- data/lib/rubocop/cop/lint/inherit_exception.rb +3 -4
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +18 -1
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -5
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +25 -5
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -6
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -2
- data/lib/rubocop/cop/message_annotator.rb +1 -0
- data/lib/rubocop/cop/metrics/line_length.rb +139 -28
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +3 -4
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +190 -0
- data/lib/rubocop/cop/mixin/{array_hash_indentation.rb → multiline_element_indentation.rb} +3 -2
- data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -7
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +33 -4
- data/lib/rubocop/cop/rails/active_record_override.rb +23 -8
- data/lib/rubocop/cop/rails/delegate.rb +5 -8
- data/lib/rubocop/cop/rails/environment_comparison.rb +5 -3
- data/lib/rubocop/cop/rails/find_each.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_allow_nil.rb +3 -3
- data/lib/rubocop/cop/rails/reflection_class_name.rb +1 -1
- data/lib/rubocop/cop/rails/skips_model_validations.rb +6 -7
- data/lib/rubocop/cop/rails/time_zone.rb +3 -10
- data/lib/rubocop/cop/rails/validation.rb +3 -0
- data/lib/rubocop/cop/registry.rb +3 -3
- data/lib/rubocop/cop/style/alias.rb +13 -7
- data/lib/rubocop/cop/style/block_delimiters.rb +20 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +19 -21
- data/lib/rubocop/cop/style/class_methods.rb +16 -24
- data/lib/rubocop/cop/style/conditional_assignment.rb +20 -49
- data/lib/rubocop/cop/style/documentation.rb +3 -7
- data/lib/rubocop/cop/style/format_string.rb +18 -21
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +4 -0
- data/lib/rubocop/cop/style/lambda.rb +12 -8
- data/lib/rubocop/cop/style/mixin_grouping.rb +8 -10
- data/lib/rubocop/cop/style/module_function.rb +2 -3
- data/lib/rubocop/cop/style/next.rb +10 -14
- data/lib/rubocop/cop/style/one_line_conditional.rb +5 -3
- data/lib/rubocop/cop/style/optional_arguments.rb +1 -4
- data/lib/rubocop/cop/style/random_with_offset.rb +44 -47
- data/lib/rubocop/cop/style/redundant_return.rb +6 -14
- data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +3 -0
- data/lib/rubocop/cop/style/struct_inheritance.rb +2 -3
- data/lib/rubocop/cop/style/symbol_proc.rb +20 -40
- data/lib/rubocop/cop/style/unless_else.rb +1 -2
- data/lib/rubocop/cop/style/yoda_condition.rb +8 -7
- data/lib/rubocop/cop/util.rb +2 -4
- data/lib/rubocop/file_finder.rb +5 -10
- data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -0
- data/lib/rubocop/node_pattern.rb +304 -170
- data/lib/rubocop/options.rb +4 -1
- data/lib/rubocop/rspec/shared_contexts.rb +3 -0
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop/yaml_duplication_checker.rb +1 -1
- metadata +26 -50
- data/lib/rubocop/cop/performance/caller.rb +0 -69
- data/lib/rubocop/cop/performance/case_when_splat.rb +0 -177
- data/lib/rubocop/cop/performance/casecmp.rb +0 -108
- data/lib/rubocop/cop/performance/chain_array_allocation.rb +0 -78
- data/lib/rubocop/cop/performance/compare_with_block.rb +0 -122
- data/lib/rubocop/cop/performance/count.rb +0 -102
- data/lib/rubocop/cop/performance/detect.rb +0 -110
- data/lib/rubocop/cop/performance/double_start_end_with.rb +0 -94
- data/lib/rubocop/cop/performance/end_with.rb +0 -56
- data/lib/rubocop/cop/performance/fixed_size.rb +0 -97
- data/lib/rubocop/cop/performance/flat_map.rb +0 -78
- data/lib/rubocop/cop/performance/inefficient_hash_search.rb +0 -99
- data/lib/rubocop/cop/performance/open_struct.rb +0 -46
- data/lib/rubocop/cop/performance/range_include.rb +0 -50
- data/lib/rubocop/cop/performance/redundant_block_call.rb +0 -93
- data/lib/rubocop/cop/performance/redundant_match.rb +0 -56
- data/lib/rubocop/cop/performance/redundant_merge.rb +0 -183
- data/lib/rubocop/cop/performance/regexp_match.rb +0 -265
- data/lib/rubocop/cop/performance/reverse_each.rb +0 -42
- data/lib/rubocop/cop/performance/size.rb +0 -77
- data/lib/rubocop/cop/performance/start_with.rb +0 -59
- data/lib/rubocop/cop/performance/string_replacement.rb +0 -173
- data/lib/rubocop/cop/performance/times_map.rb +0 -71
- data/lib/rubocop/cop/performance/unfreeze_string.rb +0 -50
- data/lib/rubocop/cop/performance/uri_default_parser.rb +0 -47
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
|
-
# Common code for indenting
|
6
|
-
|
5
|
+
# Common code for indenting the first elements in multiline
|
6
|
+
# array literals, hash literals, and method definitions.
|
7
|
+
module MultilineElementIndentation
|
7
8
|
private
|
8
9
|
|
9
10
|
def each_argument_node(node, type)
|
@@ -24,15 +24,11 @@ module RuboCop
|
|
24
24
|
|
25
25
|
def extract_body(node)
|
26
26
|
case node.type
|
27
|
-
when :block, :def
|
28
|
-
|
29
|
-
when :defs
|
30
|
-
_self, _method, _args, body = *node
|
27
|
+
when :block, :def, :defs
|
28
|
+
node.body
|
31
29
|
else
|
32
|
-
|
30
|
+
node
|
33
31
|
end
|
34
|
-
|
35
|
-
body
|
36
32
|
end
|
37
33
|
end
|
38
34
|
end
|
@@ -24,6 +24,13 @@ module RuboCop
|
|
24
24
|
# # do something
|
25
25
|
# end
|
26
26
|
#
|
27
|
+
# # good
|
28
|
+
# begin
|
29
|
+
# # do something
|
30
|
+
# rescue MyException => _e
|
31
|
+
# # do something
|
32
|
+
# end
|
33
|
+
#
|
27
34
|
# @example PreferredName: exception
|
28
35
|
# # bad
|
29
36
|
# begin
|
@@ -39,6 +46,13 @@ module RuboCop
|
|
39
46
|
# # do something
|
40
47
|
# end
|
41
48
|
#
|
49
|
+
# # good
|
50
|
+
# begin
|
51
|
+
# # do something
|
52
|
+
# rescue MyException => _exception
|
53
|
+
# # do something
|
54
|
+
# end
|
55
|
+
#
|
42
56
|
class RescuedExceptionsVariableName < Cop
|
43
57
|
MSG = 'Use `%<preferred>s` instead of `%<bad>s`.'.freeze
|
44
58
|
|
@@ -50,21 +64,36 @@ module RuboCop
|
|
50
64
|
return if @exception_name.const_type? ||
|
51
65
|
variable_name == preferred_name
|
52
66
|
|
53
|
-
add_offense(node, location:
|
67
|
+
add_offense(node, location: offense_range(node))
|
68
|
+
end
|
69
|
+
|
70
|
+
def autocorrect(node)
|
71
|
+
lambda do |corrector|
|
72
|
+
corrector.replace(offense_range(node), preferred_name)
|
73
|
+
end
|
54
74
|
end
|
55
75
|
|
56
76
|
private
|
57
77
|
|
78
|
+
def offense_range(resbody)
|
79
|
+
variable = resbody.exception_variable
|
80
|
+
variable.loc.expression
|
81
|
+
end
|
82
|
+
|
58
83
|
def preferred_name
|
59
|
-
@preferred_name ||=
|
84
|
+
@preferred_name ||= begin
|
85
|
+
name = cop_config.fetch('PreferredName', 'e')
|
86
|
+
name = "_#{name}" if variable_name.to_s.start_with?('_')
|
87
|
+
name
|
88
|
+
end
|
60
89
|
end
|
61
90
|
|
62
91
|
def variable_name
|
63
|
-
|
92
|
+
location.source
|
64
93
|
end
|
65
94
|
|
66
95
|
def location
|
67
|
-
@
|
96
|
+
@exception_name.loc.expression
|
68
97
|
end
|
69
98
|
|
70
99
|
def message(_node = nil)
|
@@ -29,25 +29,26 @@ module RuboCop
|
|
29
29
|
'Use %<prefer>s callbacks instead of overriding the Active Record ' \
|
30
30
|
'method `%<bad>s`.'.freeze
|
31
31
|
BAD_METHODS = %i[create destroy save update].freeze
|
32
|
+
ACTIVE_RECORD_CLASSES = %w[ApplicationRecord ActiveModel::Base
|
33
|
+
ActiveRecord::Base].freeze
|
32
34
|
|
33
35
|
def on_def(node)
|
34
|
-
method_name = node.method_name
|
35
36
|
return unless BAD_METHODS.include?(node.method_name)
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
!part.nil? && part.const_type?
|
40
|
-
end.last
|
41
|
-
return unless %w[ApplicationRecord ActiveModel::Base]
|
42
|
-
.include?(parent_class.const_name)
|
38
|
+
parent_class_name = find_parent_class_name(node)
|
39
|
+
return unless active_model?(parent_class_name)
|
43
40
|
|
44
41
|
return unless node.descendants.any?(&:zsuper_type?)
|
45
42
|
|
46
|
-
add_offense(node, message: message(method_name))
|
43
|
+
add_offense(node, message: message(node.method_name))
|
47
44
|
end
|
48
45
|
|
49
46
|
private
|
50
47
|
|
48
|
+
def active_model?(parent_class_name)
|
49
|
+
ACTIVE_RECORD_CLASSES.include?(parent_class_name)
|
50
|
+
end
|
51
|
+
|
51
52
|
def callback_names(method_name)
|
52
53
|
names = %w[before_ around_ after_].map do |prefix|
|
53
54
|
"`#{prefix}#{method_name}`"
|
@@ -61,6 +62,20 @@ module RuboCop
|
|
61
62
|
def message(method_name)
|
62
63
|
format(MSG, prefer: callback_names(method_name), bad: method_name)
|
63
64
|
end
|
65
|
+
|
66
|
+
def find_parent_class_name(node)
|
67
|
+
return nil unless node
|
68
|
+
|
69
|
+
if node.class_type?
|
70
|
+
parent_class_name = node.node_parts[1]
|
71
|
+
|
72
|
+
return nil if parent_class_name.nil?
|
73
|
+
|
74
|
+
return parent_class_name.source
|
75
|
+
end
|
76
|
+
|
77
|
+
find_parent_class_name(node.parent)
|
78
|
+
end
|
64
79
|
end
|
65
80
|
end
|
66
81
|
end
|
@@ -68,11 +68,10 @@ module RuboCop
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def autocorrect(node)
|
71
|
-
|
72
|
-
|
73
|
-
"to: :#{body.receiver.method_name}"]
|
71
|
+
delegation = ["delegate :#{node.body.method_name}",
|
72
|
+
"to: :#{node.body.receiver.method_name}"]
|
74
73
|
|
75
|
-
if method_name == prefixed_method_name(body)
|
74
|
+
if node.method_name == prefixed_method_name(node.body)
|
76
75
|
delegation << ['prefix: true']
|
77
76
|
end
|
78
77
|
|
@@ -84,11 +83,9 @@ module RuboCop
|
|
84
83
|
private
|
85
84
|
|
86
85
|
def trivial_delegate?(def_node)
|
87
|
-
method_name, args, body = *def_node
|
88
|
-
|
89
86
|
delegate?(def_node) &&
|
90
|
-
method_name_matches?(method_name, body) &&
|
91
|
-
arguments_match?(
|
87
|
+
method_name_matches?(def_node.method_name, def_node.body) &&
|
88
|
+
arguments_match?(def_node.arguments, def_node.body)
|
92
89
|
end
|
93
90
|
|
94
91
|
def arguments_match?(arg_array, body)
|
@@ -56,10 +56,12 @@ module RuboCop
|
|
56
56
|
private
|
57
57
|
|
58
58
|
def replacement(node)
|
59
|
-
receiver
|
60
|
-
environment, = *compared
|
61
|
-
"#{receiver.source}.#{environment}?"
|
59
|
+
"#{node.receiver.source}.#{content(node.first_argument)}?"
|
62
60
|
end
|
61
|
+
|
62
|
+
def_node_matcher :content, <<-PATTERN
|
63
|
+
({str sym} $_)
|
64
|
+
PATTERN
|
63
65
|
end
|
64
66
|
end
|
65
67
|
end
|
@@ -79,10 +79,10 @@ module RuboCop
|
|
79
79
|
node.each_descendant do |descendant|
|
80
80
|
next unless descendant.pair_type?
|
81
81
|
|
82
|
-
key = descendant.children.first.
|
82
|
+
key = descendant.children.first.source
|
83
83
|
|
84
|
-
allow_nil = descendant if key ==
|
85
|
-
allow_blank = descendant if key ==
|
84
|
+
allow_nil = descendant if key == 'allow_nil'
|
85
|
+
allow_blank = descendant if key == 'allow_blank'
|
86
86
|
|
87
87
|
break if allow_nil && allow_blank
|
88
88
|
end
|
@@ -56,13 +56,7 @@ module RuboCop
|
|
56
56
|
def on_send(node)
|
57
57
|
return if whitelist.include?(node.method_name.to_s)
|
58
58
|
return unless blacklist.include?(node.method_name.to_s)
|
59
|
-
|
60
|
-
_receiver, method_name, *args = *node
|
61
|
-
|
62
|
-
if METHODS_WITH_ARGUMENTS.include?(method_name.to_s) && args.empty?
|
63
|
-
return
|
64
|
-
end
|
65
|
-
|
59
|
+
return if allowed_method?(node)
|
66
60
|
return if good_touch?(node)
|
67
61
|
|
68
62
|
add_offense(node, location: :selector)
|
@@ -75,6 +69,11 @@ module RuboCop
|
|
75
69
|
format(MSG, method: node.method_name)
|
76
70
|
end
|
77
71
|
|
72
|
+
def allowed_method?(node)
|
73
|
+
METHODS_WITH_ARGUMENTS.include?(node.method_name.to_s) &&
|
74
|
+
!node.arguments?
|
75
|
+
end
|
76
|
+
|
78
77
|
def blacklist
|
79
78
|
cop_config['Blacklist'] || []
|
80
79
|
end
|
@@ -128,17 +128,12 @@ module RuboCop
|
|
128
128
|
def extract_method_chain(node)
|
129
129
|
chain = []
|
130
130
|
while !node.nil? && node.send_type?
|
131
|
-
chain <<
|
131
|
+
chain << node.method_name if method_from_time_class?(node)
|
132
132
|
node = node.parent
|
133
133
|
end
|
134
134
|
chain
|
135
135
|
end
|
136
136
|
|
137
|
-
def extract_method(node)
|
138
|
-
_receiver, method_name, *_args = *node
|
139
|
-
method_name
|
140
|
-
end
|
141
|
-
|
142
137
|
# Only add the method to the chain if the method being
|
143
138
|
# called is part of the time class.
|
144
139
|
def method_from_time_class?(node)
|
@@ -155,9 +150,7 @@ module RuboCop
|
|
155
150
|
def method_send?(node)
|
156
151
|
return false unless node.parent && node.parent.send_type?
|
157
152
|
|
158
|
-
receiver
|
159
|
-
|
160
|
-
receiver == node
|
153
|
+
node.parent.receiver == node
|
161
154
|
end
|
162
155
|
|
163
156
|
def safe_method(method_name, node)
|
@@ -174,7 +167,7 @@ module RuboCop
|
|
174
167
|
selector_node = node
|
175
168
|
|
176
169
|
while node && node.send_type?
|
177
|
-
break if
|
170
|
+
break if node.method_name == :localtime
|
178
171
|
|
179
172
|
node = node.parent
|
180
173
|
end
|
@@ -15,6 +15,7 @@ module RuboCop
|
|
15
15
|
# validates_length_of :foo
|
16
16
|
# validates_numericality_of :foo
|
17
17
|
# validates_presence_of :foo
|
18
|
+
# validates_absence_of :foo
|
18
19
|
# validates_size_of :foo
|
19
20
|
# validates_uniqueness_of :foo
|
20
21
|
#
|
@@ -27,6 +28,7 @@ module RuboCop
|
|
27
28
|
# validates :foo, length: true
|
28
29
|
# validates :foo, numericality: true
|
29
30
|
# validates :foo, presence: true
|
31
|
+
# validates :foo, absence: true
|
30
32
|
# validates :foo, size: true
|
31
33
|
# validates :foo, uniqueness: true
|
32
34
|
#
|
@@ -43,6 +45,7 @@ module RuboCop
|
|
43
45
|
length
|
44
46
|
numericality
|
45
47
|
presence
|
48
|
+
absence
|
46
49
|
size
|
47
50
|
uniqueness
|
48
51
|
].freeze
|
data/lib/rubocop/cop/registry.rb
CHANGED
@@ -65,17 +65,17 @@ module RuboCop
|
|
65
65
|
#
|
66
66
|
# cops = RuboCop::Cop::Cop.all
|
67
67
|
# cops.
|
68
|
-
# qualified_cop_name('Layout/
|
68
|
+
# qualified_cop_name('Layout/EndOfLine') # => 'Layout/EndOfLine'
|
69
69
|
#
|
70
70
|
# @example fixes incorrect namespaces
|
71
71
|
#
|
72
72
|
# cops = RuboCop::Cop::Cop.all
|
73
|
-
# cops.qualified_cop_name('Lint/
|
73
|
+
# cops.qualified_cop_name('Lint/EndOfLine') # => 'Layout/EndOfLine'
|
74
74
|
#
|
75
75
|
# @example namespaces bare cop identifiers
|
76
76
|
#
|
77
77
|
# cops = RuboCop::Cop::Cop.all
|
78
|
-
# cops.qualified_cop_name('
|
78
|
+
# cops.qualified_cop_name('EndOfLine') # => 'Layout/EndOfLine'
|
79
79
|
#
|
80
80
|
# @example passes back unrecognized cop names
|
81
81
|
#
|
@@ -114,27 +114,33 @@ module RuboCop
|
|
114
114
|
def correct_alias_method_to_alias(send_node)
|
115
115
|
lambda do |corrector|
|
116
116
|
new, old = *send_node.arguments
|
117
|
-
replacement = "alias #{new
|
117
|
+
replacement = "alias #{identifier(new)} #{identifier(old)}"
|
118
118
|
corrector.replace(send_node.source_range, replacement)
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|
122
122
|
def correct_alias_to_alias_method(node)
|
123
123
|
lambda do |corrector|
|
124
|
-
|
125
|
-
|
126
|
-
|
124
|
+
replacement =
|
125
|
+
'alias_method ' \
|
126
|
+
":#{identifier(node.new_identifier)}, " \
|
127
|
+
":#{identifier(node.old_identifier)}"
|
127
128
|
corrector.replace(node.source_range, replacement)
|
128
129
|
end
|
129
130
|
end
|
130
131
|
|
131
132
|
def correct_alias_with_symbol_args(node)
|
132
133
|
lambda do |corrector|
|
133
|
-
|
134
|
-
|
135
|
-
corrector.replace(
|
134
|
+
corrector.replace(node.new_identifier.source_range,
|
135
|
+
node.new_identifier.source[1..-1])
|
136
|
+
corrector.replace(node.old_identifier.source_range,
|
137
|
+
node.old_identifier.source[1..-1])
|
136
138
|
end
|
137
139
|
end
|
140
|
+
|
141
|
+
def_node_matcher :identifier, <<-PATTERN
|
142
|
+
(sym $_)
|
143
|
+
PATTERN
|
138
144
|
end
|
139
145
|
end
|
140
146
|
end
|
@@ -95,10 +95,24 @@ module RuboCop
|
|
95
95
|
# word.flip.flop
|
96
96
|
# }.join("-")
|
97
97
|
#
|
98
|
+
# @example EnforcedStyle: always_braces
|
99
|
+
# # bad
|
100
|
+
# words.each do |word|
|
101
|
+
# word.flip.flop
|
102
|
+
# end
|
103
|
+
#
|
104
|
+
# # good
|
105
|
+
# words.each { |word|
|
106
|
+
# word.flip.flop
|
107
|
+
# }
|
108
|
+
#
|
98
109
|
class BlockDelimiters < Cop
|
99
110
|
include ConfigurableEnforcedStyle
|
100
111
|
include IgnoredMethods
|
101
112
|
|
113
|
+
ALWAYS_BRACES_MESSAGE = 'Prefer `{...}` over `do...end` for blocks.'
|
114
|
+
.freeze
|
115
|
+
|
102
116
|
def on_send(node)
|
103
117
|
return unless node.arguments?
|
104
118
|
return if node.parenthesized? || node.operator_method?
|
@@ -166,6 +180,7 @@ module RuboCop
|
|
166
180
|
when :line_count_based then line_count_based_message(node)
|
167
181
|
when :semantic then semantic_message(node)
|
168
182
|
when :braces_for_chaining then braces_for_chaining_message(node)
|
183
|
+
when :always_braces then ALWAYS_BRACES_MESSAGE
|
169
184
|
end
|
170
185
|
end
|
171
186
|
|
@@ -229,6 +244,7 @@ module RuboCop
|
|
229
244
|
when :line_count_based then line_count_based_block_style?(node)
|
230
245
|
when :semantic then semantic_block_style?(node)
|
231
246
|
when :braces_for_chaining then braces_for_chaining_style?(node)
|
247
|
+
when :always_braces then braces_style?(node)
|
232
248
|
end
|
233
249
|
end
|
234
250
|
|
@@ -257,6 +273,10 @@ module RuboCop
|
|
257
273
|
end
|
258
274
|
end
|
259
275
|
|
276
|
+
def braces_style?(node)
|
277
|
+
node.loc.begin.source == '{'
|
278
|
+
end
|
279
|
+
|
260
280
|
def return_value_chaining?(node)
|
261
281
|
node.parent && node.parent.send_type? && node.parent.dot?
|
262
282
|
end
|
@@ -31,36 +31,30 @@ module RuboCop
|
|
31
31
|
'nested style.'.freeze
|
32
32
|
|
33
33
|
def on_class(node)
|
34
|
-
|
35
|
-
return if superclass && style != :nested
|
34
|
+
return if node.parent_class && style != :nested
|
36
35
|
|
37
|
-
check_style(node, body)
|
36
|
+
check_style(node, node.body)
|
38
37
|
end
|
39
38
|
|
40
39
|
def on_module(node)
|
41
|
-
|
42
|
-
check_style(node, body)
|
40
|
+
check_style(node, node.body)
|
43
41
|
end
|
44
42
|
|
45
43
|
def autocorrect(node)
|
46
44
|
lambda do |corrector|
|
47
|
-
if node.class_type?
|
48
|
-
|
49
|
-
|
50
|
-
else
|
51
|
-
name, body = *node
|
52
|
-
end
|
53
|
-
nest_or_compact(corrector, node, name, body)
|
45
|
+
return if node.class_type? && node.parent_class && style != :nested
|
46
|
+
|
47
|
+
nest_or_compact(corrector, node)
|
54
48
|
end
|
55
49
|
end
|
56
50
|
|
57
51
|
private
|
58
52
|
|
59
|
-
def nest_or_compact(corrector, node
|
53
|
+
def nest_or_compact(corrector, node)
|
60
54
|
if style == :nested
|
61
55
|
nest_definition(corrector, node)
|
62
56
|
else
|
63
|
-
compact_definition(corrector, node
|
57
|
+
compact_definition(corrector, node)
|
64
58
|
end
|
65
59
|
end
|
66
60
|
|
@@ -91,19 +85,23 @@ module RuboCop
|
|
91
85
|
corrector.replace(node.loc.end, replacement)
|
92
86
|
end
|
93
87
|
|
94
|
-
def compact_definition(corrector, node
|
95
|
-
compact_node(corrector, node
|
96
|
-
remove_end(corrector, body)
|
88
|
+
def compact_definition(corrector, node)
|
89
|
+
compact_node(corrector, node)
|
90
|
+
remove_end(corrector, node.body)
|
97
91
|
end
|
98
92
|
|
99
|
-
def compact_node(corrector, node
|
100
|
-
|
101
|
-
replacement = "#{body.type} #{const_name}"
|
93
|
+
def compact_node(corrector, node)
|
94
|
+
replacement = "#{node.body.type} #{compact_identifier_name(node)}"
|
102
95
|
range = range_between(node.loc.keyword.begin_pos,
|
103
|
-
body.loc.name.end_pos)
|
96
|
+
node.body.loc.name.end_pos)
|
104
97
|
corrector.replace(range, replacement)
|
105
98
|
end
|
106
99
|
|
100
|
+
def compact_identifier_name(node)
|
101
|
+
"#{node.identifier.const_name}::" \
|
102
|
+
"#{node.body.children.first.const_name}"
|
103
|
+
end
|
104
|
+
|
107
105
|
def remove_end(corrector, body)
|
108
106
|
range = range_between(
|
109
107
|
body.loc.end.begin_pos - leading_spaces(body).size,
|