rubocop 1.33.0 → 1.35.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 +29 -1
- data/lib/rubocop/cli/command/suggest_extensions.rb +53 -15
- data/lib/rubocop/config_loader.rb +12 -0
- data/lib/rubocop/config_loader_resolver.rb +1 -5
- data/lib/rubocop/cop/cop.rb +1 -1
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +58 -0
- data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
- data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +61 -0
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/block_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/block_end_newline.rb +4 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +5 -2
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +2 -0
- data/lib/rubocop/cop/layout/indentation_width.rb +2 -0
- data/lib/rubocop/cop/layout/line_length.rb +4 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
- data/lib/rubocop/cop/legacy/corrections_proxy.rb +1 -1
- data/lib/rubocop/cop/legacy/corrector.rb +1 -1
- data/lib/rubocop/cop/lint/debugger.rb +15 -15
- data/lib/rubocop/cop/lint/empty_block.rb +1 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +5 -0
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -9
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +25 -6
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +12 -0
- data/lib/rubocop/cop/lint/redundant_with_index.rb +13 -10
- data/lib/rubocop/cop/lint/redundant_with_object.rb +12 -11
- data/lib/rubocop/cop/lint/shadowed_exception.rb +15 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +10 -1
- data/lib/rubocop/cop/lint/unreachable_loop.rb +7 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +6 -4
- data/lib/rubocop/cop/lint/void.rb +2 -0
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +76 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
- data/lib/rubocop/cop/mixin/method_complexity.rb +4 -4
- data/lib/rubocop/cop/mixin/range_help.rb +4 -5
- data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +2 -2
- data/lib/rubocop/cop/style/class_methods_definitions.rb +2 -1
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +3 -1
- data/lib/rubocop/cop/style/double_negation.rb +2 -0
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
- data/lib/rubocop/cop/style/each_with_object.rb +39 -8
- data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
- data/lib/rubocop/cop/style/for.rb +2 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +3 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +17 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +8 -6
- data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
- data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -1
- data/lib/rubocop/cop/style/next.rb +2 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +16 -1
- data/lib/rubocop/cop/style/object_then.rb +2 -0
- data/lib/rubocop/cop/style/proc.rb +4 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -0
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +15 -22
- data/lib/rubocop/cop/style/redundant_self.rb +2 -0
- data/lib/rubocop/cop/style/redundant_sort_by.rb +24 -8
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +14 -3
- data/lib/rubocop/cop/style/symbol_proc.rb +5 -0
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -13
- data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
- data/lib/rubocop/ext/range.rb +15 -0
- data/lib/rubocop/feature_loader.rb +92 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +1 -1
- data/lib/rubocop/formatter/markdown_formatter.rb +1 -1
- data/lib/rubocop/formatter/tap_formatter.rb +1 -1
- data/lib/rubocop/server/cache.rb +4 -2
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +4 -1
- metadata +13 -8
@@ -23,13 +23,8 @@ module RuboCop
|
|
23
23
|
MSG = 'Use `each_with_object` instead of `%<method>s`.'
|
24
24
|
METHODS = %i[inject reduce].freeze
|
25
25
|
|
26
|
-
# @!method each_with_object_candidate?(node)
|
27
|
-
def_node_matcher :each_with_object_candidate?, <<~PATTERN
|
28
|
-
(block $(send _ {:inject :reduce} _) $_ $_)
|
29
|
-
PATTERN
|
30
|
-
|
31
26
|
def on_block(node)
|
32
|
-
|
27
|
+
each_with_object_block_candidate?(node) do |method, args, body|
|
33
28
|
_, method_name, method_arg = *method
|
34
29
|
return if simple_method_arg?(method_arg)
|
35
30
|
|
@@ -40,14 +35,38 @@ module RuboCop
|
|
40
35
|
|
41
36
|
message = format(MSG, method: method_name)
|
42
37
|
add_offense(method.loc.selector, message: message) do |corrector|
|
43
|
-
|
38
|
+
autocorrect_block(corrector, node, return_value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def on_numblock(node)
|
44
|
+
each_with_object_numblock_candidate?(node) do |method, body|
|
45
|
+
_, method_name, method_arg = *method
|
46
|
+
return if simple_method_arg?(method_arg)
|
47
|
+
|
48
|
+
return unless return_value(body)&.source == '_1'
|
49
|
+
|
50
|
+
message = format(MSG, method: method_name)
|
51
|
+
add_offense(method.loc.selector, message: message) do |corrector|
|
52
|
+
autocorrect_numblock(corrector, node)
|
44
53
|
end
|
45
54
|
end
|
46
55
|
end
|
47
56
|
|
48
57
|
private
|
49
58
|
|
50
|
-
|
59
|
+
# @!method each_with_object_block_candidate?(node)
|
60
|
+
def_node_matcher :each_with_object_block_candidate?, <<~PATTERN
|
61
|
+
(block $(send _ {:inject :reduce} _) $_ $_)
|
62
|
+
PATTERN
|
63
|
+
|
64
|
+
# @!method each_with_object_numblock_candidate?(node)
|
65
|
+
def_node_matcher :each_with_object_numblock_candidate?, <<~PATTERN
|
66
|
+
(numblock $(send _ {:inject :reduce} _) 2 $_)
|
67
|
+
PATTERN
|
68
|
+
|
69
|
+
def autocorrect_block(corrector, node, return_value)
|
51
70
|
corrector.replace(node.send_node.loc.selector, 'each_with_object')
|
52
71
|
|
53
72
|
first_arg, second_arg = *node.arguments
|
@@ -62,6 +81,18 @@ module RuboCop
|
|
62
81
|
end
|
63
82
|
end
|
64
83
|
|
84
|
+
def autocorrect_numblock(corrector, node)
|
85
|
+
corrector.replace(node.send_node.loc.selector, 'each_with_object')
|
86
|
+
|
87
|
+
# We don't remove the return value to avoid a clobbering error.
|
88
|
+
node.body.each_descendant do |var|
|
89
|
+
next unless var.lvar_type?
|
90
|
+
|
91
|
+
corrector.replace(var, '_2') if var.source == '_1'
|
92
|
+
corrector.replace(var, '_1') if var.source == '_2'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
65
96
|
def simple_method_arg?(method_arg)
|
66
97
|
method_arg&.basic_literal?
|
67
98
|
end
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
28
28
|
|
29
29
|
MSG = 'Omit pipes for the empty block parameters.'
|
30
30
|
|
31
|
-
def on_block(node)
|
31
|
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
|
32
32
|
send_node = node.send_node
|
33
33
|
check(node) unless send_node.send_type? && send_node.lambda_literal?
|
34
34
|
end
|
@@ -35,13 +35,15 @@ module RuboCop
|
|
35
35
|
|
36
36
|
# @!method kv_each(node)
|
37
37
|
def_node_matcher :kv_each, <<~PATTERN
|
38
|
-
(block $(send (send _ ${:keys :values}) :each) ...)
|
38
|
+
({block numblock} $(send (send _ ${:keys :values}) :each) ...)
|
39
39
|
PATTERN
|
40
40
|
|
41
41
|
def on_block(node)
|
42
42
|
register_kv_offense(node)
|
43
43
|
end
|
44
44
|
|
45
|
+
alias on_numblock on_block
|
46
|
+
|
45
47
|
private
|
46
48
|
|
47
49
|
def register_kv_offense(node)
|
@@ -28,6 +28,7 @@ module RuboCop
|
|
28
28
|
# * always - forces use of the 3.1 syntax (e.g. {foo:})
|
29
29
|
# * never - forces use of explicit hash literal value
|
30
30
|
# * either - accepts both shorthand and explicit use of hash literal value
|
31
|
+
# * consistent - like "always", but will avoid mixing styles in a single hash
|
31
32
|
#
|
32
33
|
# @example EnforcedStyle: ruby19 (default)
|
33
34
|
# # bad
|
@@ -89,6 +90,20 @@ module RuboCop
|
|
89
90
|
# # good
|
90
91
|
# {foo:, bar:}
|
91
92
|
#
|
93
|
+
# @example EnforcedShorthandSyntax: consistent
|
94
|
+
#
|
95
|
+
# # bad
|
96
|
+
# {foo: , bar: bar}
|
97
|
+
#
|
98
|
+
# # good
|
99
|
+
# {foo:, bar:}
|
100
|
+
#
|
101
|
+
# # bad
|
102
|
+
# {foo: , bar: baz}
|
103
|
+
#
|
104
|
+
# # good
|
105
|
+
# {foo: foo, bar: baz}
|
106
|
+
#
|
92
107
|
class HashSyntax < Base
|
93
108
|
include ConfigurableEnforcedStyle
|
94
109
|
include HashShorthandSyntax
|
@@ -104,6 +119,8 @@ module RuboCop
|
|
104
119
|
|
105
120
|
return if pairs.empty?
|
106
121
|
|
122
|
+
on_hash_for_mixed_shorthand(node)
|
123
|
+
|
107
124
|
if style == :hash_rockets || force_hash_rockets?(pairs)
|
108
125
|
hash_rockets_check(pairs)
|
109
126
|
elsif style == :ruby19_no_mixed_keys
|
@@ -96,7 +96,7 @@ module RuboCop
|
|
96
96
|
return false unless max_line_length
|
97
97
|
|
98
98
|
range = node.source_range
|
99
|
-
return false unless range.
|
99
|
+
return false unless range.single_line?
|
100
100
|
return false unless line_length_enabled_at_line?(range.first_line)
|
101
101
|
|
102
102
|
line = range.source_line
|
@@ -59,18 +59,18 @@ module RuboCop
|
|
59
59
|
def_node_matcher :inverse_candidate?, <<~PATTERN
|
60
60
|
{
|
61
61
|
(send $(send $(...) $_ $...) :!)
|
62
|
-
(send (block $(send $(...) $_) $...) :!)
|
62
|
+
(send ({block numblock} $(send $(...) $_) $...) :!)
|
63
63
|
(send (begin $(send $(...) $_ $...)) :!)
|
64
64
|
}
|
65
65
|
PATTERN
|
66
66
|
|
67
67
|
# @!method inverse_block?(node)
|
68
68
|
def_node_matcher :inverse_block?, <<~PATTERN
|
69
|
-
(block $(send (...) $_) ... { $(send ... :!)
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
69
|
+
({block numblock} $(send (...) $_) ... { $(send ... :!)
|
70
|
+
$(send (...) {:!= :!~} ...)
|
71
|
+
(begin ... $(send ... :!))
|
72
|
+
(begin ... $(send (...) {:!= :!~} ...))
|
73
|
+
})
|
74
74
|
PATTERN
|
75
75
|
|
76
76
|
def on_send(node)
|
@@ -102,6 +102,8 @@ module RuboCop
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
+
alias on_numblock on_block
|
106
|
+
|
105
107
|
private
|
106
108
|
|
107
109
|
def correct_inverse_method(corrector, node)
|
@@ -0,0 +1,307 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Ensures magic comments are written consistently throughout your code base.
|
7
|
+
# Looks for discrepancies in separators (`-` vs `_`) and capitalization for
|
8
|
+
# both magic comment directives and values.
|
9
|
+
#
|
10
|
+
# Required capitalization can be set with the `DirectiveCapitalization` and
|
11
|
+
# `ValueCapitalization` configuration keys.
|
12
|
+
#
|
13
|
+
# NOTE: If one of these configuration is set to nil, any capitalization is allowed.
|
14
|
+
#
|
15
|
+
# @example EnforcedStyle: snake_case (default)
|
16
|
+
# # The `snake_case` style will enforce that the frozen string literal
|
17
|
+
# # comment is written in snake case. (Words separated by underscores)
|
18
|
+
# # bad
|
19
|
+
# # frozen-string-literal: true
|
20
|
+
#
|
21
|
+
# module Bar
|
22
|
+
# # ...
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# # frozen_string_literal: false
|
27
|
+
#
|
28
|
+
# module Bar
|
29
|
+
# # ...
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @example EnforcedStyle: kebab_case
|
33
|
+
# # The `kebab_case` style will enforce that the frozen string literal
|
34
|
+
# # comment is written in kebab case. (Words separated by hyphens)
|
35
|
+
# # bad
|
36
|
+
# # frozen_string_literal: true
|
37
|
+
#
|
38
|
+
# module Baz
|
39
|
+
# # ...
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# # good
|
43
|
+
# # frozen-string-literal: true
|
44
|
+
#
|
45
|
+
# module Baz
|
46
|
+
# # ...
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# @example DirectiveCapitalization: lowercase (default)
|
50
|
+
# # bad
|
51
|
+
# # FROZEN-STRING-LITERAL: true
|
52
|
+
#
|
53
|
+
# # good
|
54
|
+
# # frozen-string-literal: true
|
55
|
+
#
|
56
|
+
# @example DirectiveCapitalization: uppercase
|
57
|
+
# # bad
|
58
|
+
# # frozen-string-literal: true
|
59
|
+
#
|
60
|
+
# # good
|
61
|
+
# # FROZEN-STRING-LITERAL: true
|
62
|
+
#
|
63
|
+
# @example DirectiveCapitalization: nil
|
64
|
+
# # any capitalization is accepted
|
65
|
+
#
|
66
|
+
# # good
|
67
|
+
# # frozen-string-literal: true
|
68
|
+
#
|
69
|
+
# # good
|
70
|
+
# # FROZEN-STRING-LITERAL: true
|
71
|
+
#
|
72
|
+
# @example ValueCapitalization: nil (default)
|
73
|
+
# # any capitalization is accepted
|
74
|
+
#
|
75
|
+
# # good
|
76
|
+
# # frozen-string-literal: true
|
77
|
+
#
|
78
|
+
# # good
|
79
|
+
# # frozen-string-literal: TRUE
|
80
|
+
#
|
81
|
+
# @example ValueCapitalization: lowercase
|
82
|
+
# # when a value is not given, any capitalization is accepted
|
83
|
+
#
|
84
|
+
# # bad
|
85
|
+
# # frozen-string-literal: TRUE
|
86
|
+
#
|
87
|
+
# # good
|
88
|
+
# # frozen-string-literal: TRUE
|
89
|
+
#
|
90
|
+
# @example ValueCapitalization: uppercase
|
91
|
+
# # bad
|
92
|
+
# # frozen-string-literal: true
|
93
|
+
#
|
94
|
+
# # good
|
95
|
+
# # frozen-string-literal: TRUE
|
96
|
+
#
|
97
|
+
class MagicCommentFormat < Base
|
98
|
+
include ConfigurableEnforcedStyle
|
99
|
+
extend AutoCorrector
|
100
|
+
|
101
|
+
SNAKE_SEPARATOR = '_'
|
102
|
+
KEBAB_SEPARATOR = '-'
|
103
|
+
MSG = 'Prefer %<style>s case for magic comments.'
|
104
|
+
MSG_VALUE = 'Prefer %<case>s for magic comment values.'
|
105
|
+
|
106
|
+
# Value object to extract source ranges for the different parts of a magic comment
|
107
|
+
class CommentRange
|
108
|
+
extend Forwardable
|
109
|
+
|
110
|
+
DIRECTIVE_REGEXP = Regexp.union(MagicComment::KEYWORDS.map do |_, v|
|
111
|
+
Regexp.new(v, Regexp::IGNORECASE)
|
112
|
+
end).freeze
|
113
|
+
|
114
|
+
VALUE_REGEXP = Regexp.new("(?:#{DIRECTIVE_REGEXP}:\s*)(.*?)(?=;|$)")
|
115
|
+
|
116
|
+
def_delegators :@comment, :text, :loc
|
117
|
+
attr_reader :comment
|
118
|
+
|
119
|
+
def initialize(comment)
|
120
|
+
@comment = comment
|
121
|
+
end
|
122
|
+
|
123
|
+
# A magic comment can contain one directive (normal style) or
|
124
|
+
# multiple directives (emacs style)
|
125
|
+
def directives
|
126
|
+
@directives ||= begin
|
127
|
+
matches = []
|
128
|
+
|
129
|
+
text.scan(DIRECTIVE_REGEXP) do
|
130
|
+
offset = Regexp.last_match.offset(0)
|
131
|
+
matches << loc.expression.adjust(begin_pos: offset.first)
|
132
|
+
.with(end_pos: loc.expression.begin_pos + offset.last)
|
133
|
+
end
|
134
|
+
|
135
|
+
matches
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# A magic comment can contain one value (normal style) or
|
140
|
+
# multiple directives (emacs style)
|
141
|
+
def values
|
142
|
+
@values ||= begin
|
143
|
+
matches = []
|
144
|
+
|
145
|
+
text.scan(VALUE_REGEXP) do
|
146
|
+
offset = Regexp.last_match.offset(1)
|
147
|
+
matches << loc.expression.adjust(begin_pos: offset.first)
|
148
|
+
.with(end_pos: loc.expression.begin_pos + offset.last)
|
149
|
+
end
|
150
|
+
|
151
|
+
matches
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def on_new_investigation
|
157
|
+
return unless processed_source.ast
|
158
|
+
|
159
|
+
magic_comments.each do |comment|
|
160
|
+
issues = find_issues(comment)
|
161
|
+
register_offenses(issues) if issues.any?
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
private
|
166
|
+
|
167
|
+
def magic_comments
|
168
|
+
processed_source.each_comment_in_lines(leading_comment_lines)
|
169
|
+
.select { |comment| MagicComment.parse(comment.text).valid? }
|
170
|
+
.map { |comment| CommentRange.new(comment) }
|
171
|
+
end
|
172
|
+
|
173
|
+
def leading_comment_lines
|
174
|
+
first_non_comment_token = processed_source.tokens.find { |token| !token.comment? }
|
175
|
+
|
176
|
+
if first_non_comment_token
|
177
|
+
0...first_non_comment_token.line
|
178
|
+
else
|
179
|
+
(0..)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def find_issues(comment)
|
184
|
+
issues = { directives: [], values: [] }
|
185
|
+
|
186
|
+
comment.directives.each do |directive|
|
187
|
+
issues[:directives] << directive if directive_offends?(directive)
|
188
|
+
end
|
189
|
+
|
190
|
+
comment.values.each do |value| # rubocop:disable Style/HashEachMethods
|
191
|
+
issues[:values] << value if wrong_capitalization?(value.source, value_capitalization)
|
192
|
+
end
|
193
|
+
|
194
|
+
issues
|
195
|
+
end
|
196
|
+
|
197
|
+
def directive_offends?(directive)
|
198
|
+
incorrect_separator?(directive.source) ||
|
199
|
+
wrong_capitalization?(directive.source, directive_capitalization)
|
200
|
+
end
|
201
|
+
|
202
|
+
def register_offenses(issues)
|
203
|
+
fix_directives(issues[:directives])
|
204
|
+
fix_values(issues[:values])
|
205
|
+
end
|
206
|
+
|
207
|
+
def fix_directives(issues)
|
208
|
+
return if issues.empty?
|
209
|
+
|
210
|
+
msg = format(MSG, style: expected_style)
|
211
|
+
|
212
|
+
issues.each do |directive|
|
213
|
+
add_offense(directive, message: msg) do |corrector|
|
214
|
+
replacement = replace_separator(replace_capitalization(directive.source,
|
215
|
+
directive_capitalization))
|
216
|
+
corrector.replace(directive, replacement)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def fix_values(issues)
|
222
|
+
return if issues.empty?
|
223
|
+
|
224
|
+
msg = format(MSG_VALUE, case: value_capitalization)
|
225
|
+
|
226
|
+
issues.each do |value|
|
227
|
+
add_offense(value, message: msg) do |corrector|
|
228
|
+
corrector.replace(value, replace_capitalization(value.source, value_capitalization))
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
def expected_style
|
234
|
+
[directive_capitalization, style].compact.join(' ').gsub(/_?case\b/, '')
|
235
|
+
end
|
236
|
+
|
237
|
+
def wrong_separator
|
238
|
+
style == :snake_case ? KEBAB_SEPARATOR : SNAKE_SEPARATOR
|
239
|
+
end
|
240
|
+
|
241
|
+
def correct_separator
|
242
|
+
style == :snake_case ? SNAKE_SEPARATOR : KEBAB_SEPARATOR
|
243
|
+
end
|
244
|
+
|
245
|
+
def incorrect_separator?(text)
|
246
|
+
text[wrong_separator]
|
247
|
+
end
|
248
|
+
|
249
|
+
def wrong_capitalization?(text, expected_case)
|
250
|
+
return false unless expected_case
|
251
|
+
|
252
|
+
case expected_case
|
253
|
+
when :lowercase
|
254
|
+
text != text.downcase
|
255
|
+
when :uppercase
|
256
|
+
text != text.upcase
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
def replace_separator(text)
|
261
|
+
text.tr(wrong_separator, correct_separator)
|
262
|
+
end
|
263
|
+
|
264
|
+
def replace_capitalization(text, style)
|
265
|
+
return text unless style
|
266
|
+
|
267
|
+
case style
|
268
|
+
when :lowercase
|
269
|
+
text.downcase
|
270
|
+
when :uppercase
|
271
|
+
text.upcase
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
def line_range(line)
|
276
|
+
processed_source.buffer.line_range(line)
|
277
|
+
end
|
278
|
+
|
279
|
+
def directive_capitalization
|
280
|
+
cop_config['DirectiveCapitalization']&.to_sym.tap do |style|
|
281
|
+
unless valid_capitalization?(style)
|
282
|
+
raise "Unknown `DirectiveCapitalization` #{style} selected!"
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
def value_capitalization
|
288
|
+
cop_config['ValueCapitalization']&.to_sym.tap do |style|
|
289
|
+
unless valid_capitalization?(style)
|
290
|
+
raise "Unknown `ValueCapitalization` #{style} selected!"
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
def valid_capitalization?(style)
|
296
|
+
return true unless style
|
297
|
+
|
298
|
+
supported_capitalizations.include?(style)
|
299
|
+
end
|
300
|
+
|
301
|
+
def supported_capitalizations
|
302
|
+
cop_config['SupportedCapitalizations'].map(&:to_sym)
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
@@ -35,12 +35,15 @@ module RuboCop
|
|
35
35
|
ignore_node(node.send_node)
|
36
36
|
end
|
37
37
|
|
38
|
+
alias on_numblock on_block
|
39
|
+
|
38
40
|
def on_send(node)
|
39
41
|
return if ignored_node?(node)
|
40
42
|
|
41
43
|
receiver = node.receiver
|
42
44
|
|
43
|
-
return unless receiver&.block_type?
|
45
|
+
return unless (receiver&.block_type? || receiver&.numblock_type?) &&
|
46
|
+
receiver.loc.end.is?('end')
|
44
47
|
|
45
48
|
range = range_between(receiver.loc.end.begin_pos, node.source_range.end_pos)
|
46
49
|
|
@@ -31,7 +31,7 @@ module RuboCop
|
|
31
31
|
node.send_node.each_node(:send) do |send_node|
|
32
32
|
receiver = send_node.receiver
|
33
33
|
|
34
|
-
next unless receiver&.block_type? && receiver&.multiline?
|
34
|
+
next unless (receiver&.block_type? || receiver&.numblock_type?) && receiver&.multiline?
|
35
35
|
|
36
36
|
range = range_between(receiver.loc.end.begin_pos, node.send_node.source_range.end_pos)
|
37
37
|
|
@@ -42,6 +42,8 @@ module RuboCop
|
|
42
42
|
break
|
43
43
|
end
|
44
44
|
end
|
45
|
+
|
46
|
+
alias on_numblock on_block
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
@@ -3,9 +3,17 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Checks for big numeric literals without _ between groups
|
6
|
+
# Checks for big numeric literals without `_` between groups
|
7
7
|
# of digits in them.
|
8
8
|
#
|
9
|
+
# Additional allowed patterns can be added by adding regexps to
|
10
|
+
# the `AllowedPatterns` configuration. All regexps are treated
|
11
|
+
# as anchored even if the patterns do not contain anchors (so
|
12
|
+
# `\d{4}_\d{4}` will allow `1234_5678` but not `1234_5678_9012`).
|
13
|
+
#
|
14
|
+
# NOTE: Even if `AllowedPatterns` are given, autocorrection will
|
15
|
+
# only correct to the standard pattern of an `_` every 3 digits.
|
16
|
+
#
|
9
17
|
# @example
|
10
18
|
#
|
11
19
|
# # bad
|
@@ -34,6 +42,7 @@ module RuboCop
|
|
34
42
|
#
|
35
43
|
class NumericLiterals < Base
|
36
44
|
include IntegerNode
|
45
|
+
include AllowedPattern
|
37
46
|
extend AutoCorrector
|
38
47
|
|
39
48
|
MSG = 'Use underscores(_) as thousands separator and separate every 3 digits with them.'
|
@@ -59,6 +68,7 @@ module RuboCop
|
|
59
68
|
# TODO: handle non-decimal literals as well
|
60
69
|
return if int.start_with?('0')
|
61
70
|
return if allowed_numbers.include?(int)
|
71
|
+
return if matches_allowed_pattern?(int)
|
62
72
|
return unless int.size >= min_digits
|
63
73
|
|
64
74
|
case int
|
@@ -108,6 +118,11 @@ module RuboCop
|
|
108
118
|
def allowed_numbers
|
109
119
|
cop_config.fetch('AllowedNumbers', []).map(&:to_s)
|
110
120
|
end
|
121
|
+
|
122
|
+
def allowed_patterns
|
123
|
+
# Convert the patterns to be anchored
|
124
|
+
super.map { |regexp| Regexp.new(/\A#{regexp}\z/) }
|
125
|
+
end
|
111
126
|
end
|
112
127
|
end
|
113
128
|
end
|
@@ -19,7 +19,8 @@ module RuboCop
|
|
19
19
|
MSG = 'Use `proc` instead of `Proc.new`.'
|
20
20
|
|
21
21
|
# @!method proc_new?(node)
|
22
|
-
def_node_matcher :proc_new?,
|
22
|
+
def_node_matcher :proc_new?,
|
23
|
+
'({block numblock} $(send (const {nil? cbase} :Proc) :new) ...)'
|
23
24
|
|
24
25
|
def on_block(node)
|
25
26
|
proc_new?(node) do |block_method|
|
@@ -28,6 +29,8 @@ module RuboCop
|
|
28
29
|
end
|
29
30
|
end
|
30
31
|
end
|
32
|
+
|
33
|
+
alias on_numblock on_block
|
31
34
|
end
|
32
35
|
end
|
33
36
|
end
|
@@ -50,7 +50,7 @@ module RuboCop
|
|
50
50
|
${nil? #basic_literal? #const_type?})
|
51
51
|
PATTERN
|
52
52
|
|
53
|
-
def on_block(node)
|
53
|
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
|
54
54
|
redundant_fetch_block_candidate?(node) do |send, body|
|
55
55
|
return if should_not_check?(send, body)
|
56
56
|
|