rubocop 0.93.1 → 1.3.1
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 +36 -16
- data/config/default.yml +207 -72
- data/exe/rubocop +1 -1
- data/lib/rubocop.rb +16 -2
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
- data/lib/rubocop/cli/command/version.rb +1 -1
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/config.rb +4 -0
- data/lib/rubocop/config_loader.rb +33 -7
- data/lib/rubocop/config_loader_resolver.rb +7 -5
- data/lib/rubocop/config_validator.rb +7 -6
- data/lib/rubocop/cop/badge.rb +9 -24
- data/lib/rubocop/cop/base.rb +16 -1
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +26 -6
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
- data/lib/rubocop/cop/commissioner.rb +37 -23
- data/lib/rubocop/cop/corrector.rb +3 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/force.rb +1 -1
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -3
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +4 -5
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/generator.rb +2 -9
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +3 -4
- data/lib/rubocop/cop/layout/class_structure.rb +22 -3
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +15 -2
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +3 -3
- data/lib/rubocop/cop/layout/extra_spacing.rb +1 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +4 -4
- data/lib/rubocop/cop/layout/line_length.rb +8 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +24 -18
- data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +35 -13
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +37 -13
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +26 -2
- data/lib/rubocop/cop/lint/debugger.rb +17 -28
- data/lib/rubocop/cop/lint/duplicate_branch.rb +93 -0
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +2 -12
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +77 -0
- data/lib/rubocop/cop/lint/else_layout.rb +29 -3
- data/lib/rubocop/cop/lint/empty_block.rb +82 -0
- data/lib/rubocop/cop/lint/empty_class.rb +93 -0
- data/lib/rubocop/cop/lint/flip_flop.rb +8 -2
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +38 -6
- data/lib/rubocop/cop/lint/loop.rb +4 -4
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +14 -0
- data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +58 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +46 -13
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +27 -8
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +19 -16
- data/lib/rubocop/cop/lint/shadowed_exception.rb +4 -5
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +86 -0
- data/lib/rubocop/cop/lint/to_json.rb +1 -1
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +185 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
- data/lib/rubocop/cop/lint/useless_method_definition.rb +2 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -1
- data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
- data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
- data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -4
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +12 -2
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +11 -5
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +67 -18
- data/lib/rubocop/cop/naming/predicate_name.rb +2 -1
- data/lib/rubocop/cop/naming/variable_number.rb +98 -8
- data/lib/rubocop/cop/offense.rb +3 -3
- data/lib/rubocop/cop/security/open.rb +12 -10
- data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
- data/lib/rubocop/cop/style/and_or.rb +1 -3
- data/lib/rubocop/cop/style/arguments_forwarding.rb +142 -0
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +0 -4
- data/lib/rubocop/cop/style/case_like_if.rb +0 -4
- data/lib/rubocop/cop/style/collection_compact.rb +91 -0
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +169 -0
- data/lib/rubocop/cop/style/double_negation.rb +6 -1
- data/lib/rubocop/cop/style/format_string_token.rb +47 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -3
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +7 -2
- data/lib/rubocop/cop/style/if_inside_else.rb +37 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +7 -3
- data/lib/rubocop/cop/style/infinite_loop.rb +4 -0
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +10 -13
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -11
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +7 -11
- data/lib/rubocop/cop/style/mixin_grouping.rb +0 -4
- data/lib/rubocop/cop/style/multiple_comparison.rb +55 -7
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +106 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +52 -0
- data/lib/rubocop/cop/style/raise_args.rb +21 -6
- data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +7 -1
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self.rb +3 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +16 -4
- data/lib/rubocop/cop/style/semicolon.rb +3 -0
- data/lib/rubocop/cop/style/static_class.rb +97 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +13 -1
- data/lib/rubocop/cop/style/swap_values.rb +108 -0
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +3 -1
- data/lib/rubocop/cop/style/while_until_modifier.rb +9 -0
- data/lib/rubocop/cop/team.rb +6 -1
- data/lib/rubocop/cop/util.rb +5 -1
- data/lib/rubocop/ext/regexp_node.rb +17 -9
- data/lib/rubocop/ext/regexp_parser.rb +84 -0
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +47 -0
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +2 -2
- data/lib/rubocop/options.rb +6 -1
- data/lib/rubocop/rspec/shared_contexts.rb +4 -0
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/target_ruby.rb +65 -1
- data/lib/rubocop/version.rb +56 -6
- metadata +22 -6
@@ -5,6 +5,7 @@ module RuboCop
|
|
5
5
|
module Lint
|
6
6
|
# This cop checks for setter call to local variable as the final
|
7
7
|
# expression of a function definition.
|
8
|
+
# Its auto-correction is marked as unsafe because return value will be changed.
|
8
9
|
#
|
9
10
|
# NOTE: There are edge cases in which the local variable references a
|
10
11
|
# value that is also accessible outside the local scope. This is not
|
@@ -29,6 +30,8 @@ module RuboCop
|
|
29
30
|
# x
|
30
31
|
# end
|
31
32
|
class UselessSetterCall < Base
|
33
|
+
extend AutoCorrector
|
34
|
+
|
32
35
|
MSG = 'Useless setter call to local variable `%<variable>s`.'
|
33
36
|
ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn].freeze
|
34
37
|
|
@@ -45,7 +48,9 @@ module RuboCop
|
|
45
48
|
|
46
49
|
loc_name = receiver.loc.name
|
47
50
|
|
48
|
-
add_offense(loc_name, message: format(MSG, variable: loc_name.source))
|
51
|
+
add_offense(loc_name, message: format(MSG, variable: loc_name.source)) do |corrector|
|
52
|
+
corrector.insert_after(last_expr, "\n#{indent(last_expr)}#{loc_name.source}")
|
53
|
+
end
|
49
54
|
end
|
50
55
|
alias on_defs on_def
|
51
56
|
|
@@ -12,6 +12,9 @@ module RuboCop
|
|
12
12
|
MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
|
13
13
|
'[%<count>d/%<max>d]'
|
14
14
|
|
15
|
+
NAMED_KEYWORD_TYPES = %i[kwoptarg kwarg].freeze
|
16
|
+
private_constant :NAMED_KEYWORD_TYPES
|
17
|
+
|
15
18
|
def on_args(node)
|
16
19
|
count = args_count(node)
|
17
20
|
return unless count > max_params
|
@@ -33,7 +36,7 @@ module RuboCop
|
|
33
36
|
if count_keyword_args?
|
34
37
|
node.children.size
|
35
38
|
else
|
36
|
-
node.children.count { |a|
|
39
|
+
node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) }
|
37
40
|
end
|
38
41
|
end
|
39
42
|
|
@@ -103,7 +103,7 @@ module RuboCop
|
|
103
103
|
# hashes wrapped in a set of curly braces like {foo: 1}.
|
104
104
|
# That is, not a kwargs hash. For method calls, this ensures
|
105
105
|
# the method call is made with parens.
|
106
|
-
starts_with_bracket = node.loc.begin
|
106
|
+
starts_with_bracket = !node.hash_type? || node.loc.begin
|
107
107
|
|
108
108
|
# If the call has a second argument, we can insert a line
|
109
109
|
# break before the second argument and the rest of the
|
@@ -8,9 +8,9 @@ module RuboCop
|
|
8
8
|
include ConfigurableFormatting
|
9
9
|
|
10
10
|
FORMATS = {
|
11
|
-
snake_case: /(
|
12
|
-
normalcase: /(
|
13
|
-
non_integer: /
|
11
|
+
snake_case: /(?:\D|_\d+|\A\d+)\z/,
|
12
|
+
normalcase: /(?:\D|[^_\d]\d+|\A\d+)\z/,
|
13
|
+
non_integer: /(\D|\A\d+)\z/
|
14
14
|
}.freeze
|
15
15
|
end
|
16
16
|
end
|
@@ -19,7 +19,8 @@ module RuboCop
|
|
19
19
|
def non_eligible_node?(node)
|
20
20
|
node.modifier_form? ||
|
21
21
|
node.nonempty_line_count > 3 ||
|
22
|
-
processed_source.line_with_comment?(node.loc.last_line)
|
22
|
+
processed_source.line_with_comment?(node.loc.last_line) ||
|
23
|
+
(first_line_comment(node) && code_after(node))
|
23
24
|
end
|
24
25
|
|
25
26
|
def non_eligible_body?(body)
|
@@ -41,11 +42,9 @@ module RuboCop
|
|
41
42
|
|
42
43
|
def length_in_modifier_form(node)
|
43
44
|
keyword_element = node.loc.keyword
|
44
|
-
end_element = node.loc.end
|
45
45
|
code_before = keyword_element.source_line[0...keyword_element.column]
|
46
|
-
code_after = end_element.source_line[end_element.last_column..-1]
|
47
46
|
expression = to_modifier_form(node)
|
48
|
-
line_length("#{code_before}#{expression}#{code_after}")
|
47
|
+
line_length("#{code_before}#{expression}#{code_after(node)}")
|
49
48
|
end
|
50
49
|
|
51
50
|
def to_modifier_form(node)
|
@@ -64,6 +63,12 @@ module RuboCop
|
|
64
63
|
comment_source unless comment_disables_cop?(comment_source)
|
65
64
|
end
|
66
65
|
|
66
|
+
def code_after(node)
|
67
|
+
end_element = node.loc.end
|
68
|
+
code = end_element.source_line[end_element.last_column..-1]
|
69
|
+
code unless code.empty?
|
70
|
+
end
|
71
|
+
|
67
72
|
def parenthesize?(node)
|
68
73
|
# Parenthesize corrected expression if changing to modifier-if form
|
69
74
|
# would change the meaning of the parent expression
|
@@ -14,11 +14,13 @@ module RuboCop
|
|
14
14
|
# # good
|
15
15
|
# def +(other); end
|
16
16
|
class BinaryOperatorParameterName < Base
|
17
|
+
extend AutoCorrector
|
18
|
+
|
17
19
|
MSG = 'When defining the `%<opr>s` operator, ' \
|
18
20
|
'name its argument `other`.'
|
19
21
|
|
20
22
|
OP_LIKE_METHODS = %i[eql? equal?].freeze
|
21
|
-
EXCLUDED = %i[+@ -@ [] []= << === `].freeze
|
23
|
+
EXCLUDED = %i[+@ -@ [] []= << === ` =~].freeze
|
22
24
|
|
23
25
|
def_node_matcher :op_method_candidate?, <<~PATTERN
|
24
26
|
(def [#op_method? $_] (args $(arg [!:other !:_other])) _)
|
@@ -26,7 +28,15 @@ module RuboCop
|
|
26
28
|
|
27
29
|
def on_def(node)
|
28
30
|
op_method_candidate?(node) do |name, arg|
|
29
|
-
add_offense(arg, message: format(MSG, opr: name))
|
31
|
+
add_offense(arg, message: format(MSG, opr: name)) do |corrector|
|
32
|
+
corrector.replace(arg, 'other')
|
33
|
+
node.each_descendant(:lvar, :lvasgn) do |lvar|
|
34
|
+
lvar_location = lvar.loc.name
|
35
|
+
next unless lvar_location.source == arg.source
|
36
|
+
|
37
|
+
corrector.replace(lvar_location, 'other')
|
38
|
+
end
|
39
|
+
end
|
30
40
|
end
|
31
41
|
end
|
32
42
|
|
@@ -30,13 +30,19 @@ module RuboCop
|
|
30
30
|
class HeredocDelimiterCase < Base
|
31
31
|
include Heredoc
|
32
32
|
include ConfigurableEnforcedStyle
|
33
|
+
extend AutoCorrector
|
33
34
|
|
34
35
|
MSG = 'Use %<style>s heredoc delimiters.'
|
35
36
|
|
36
37
|
def on_heredoc(node)
|
37
38
|
return if correct_case_delimiters?(node)
|
38
39
|
|
39
|
-
add_offense(node.loc.heredoc_end)
|
40
|
+
add_offense(node.loc.heredoc_end) do |corrector|
|
41
|
+
expr = node.loc.expression
|
42
|
+
|
43
|
+
corrector.replace(expr, correct_delimiters(expr.source))
|
44
|
+
corrector.replace(node.loc.heredoc_end, correct_delimiters(delimiter_string(expr)))
|
45
|
+
end
|
40
46
|
end
|
41
47
|
|
42
48
|
private
|
@@ -46,14 +52,14 @@ module RuboCop
|
|
46
52
|
end
|
47
53
|
|
48
54
|
def correct_case_delimiters?(node)
|
49
|
-
delimiter_string(node) == correct_delimiters(node)
|
55
|
+
delimiter_string(node) == correct_delimiters(delimiter_string(node))
|
50
56
|
end
|
51
57
|
|
52
|
-
def correct_delimiters(
|
58
|
+
def correct_delimiters(source)
|
53
59
|
if style == :uppercase
|
54
|
-
|
60
|
+
source.upcase
|
55
61
|
else
|
56
|
-
|
62
|
+
source.downcase
|
57
63
|
end
|
58
64
|
end
|
59
65
|
end
|
@@ -20,6 +20,11 @@ module RuboCop
|
|
20
20
|
# @something ||= calculate_expensive_thing
|
21
21
|
# end
|
22
22
|
#
|
23
|
+
# def foo
|
24
|
+
# return @something if defined?(@something)
|
25
|
+
# @something = calculate_expensive_thing
|
26
|
+
# end
|
27
|
+
#
|
23
28
|
# # good
|
24
29
|
# def _foo
|
25
30
|
# @foo ||= calculate_expensive_thing
|
@@ -54,6 +59,11 @@ module RuboCop
|
|
54
59
|
# @foo ||= calculate_expensive_thing
|
55
60
|
# end
|
56
61
|
#
|
62
|
+
# def foo
|
63
|
+
# return @foo if defined?(@foo)
|
64
|
+
# @foo = calculate_expensive_thing
|
65
|
+
# end
|
66
|
+
#
|
57
67
|
# # good
|
58
68
|
# def foo
|
59
69
|
# @_foo ||= calculate_expensive_thing
|
@@ -64,6 +74,11 @@ module RuboCop
|
|
64
74
|
# @_foo ||= calculate_expensive_thing
|
65
75
|
# end
|
66
76
|
#
|
77
|
+
# def foo
|
78
|
+
# return @_foo if defined?(@_foo)
|
79
|
+
# @_foo = calculate_expensive_thing
|
80
|
+
# end
|
81
|
+
#
|
67
82
|
# @example EnforcedStyleForLeadingUnderscores :optional
|
68
83
|
# # bad
|
69
84
|
# def foo
|
@@ -84,6 +99,12 @@ module RuboCop
|
|
84
99
|
# def _foo
|
85
100
|
# @_foo ||= calculate_expensive_thing
|
86
101
|
# end
|
102
|
+
#
|
103
|
+
# # good
|
104
|
+
# def foo
|
105
|
+
# return @_foo if defined?(@_foo)
|
106
|
+
# @_foo = calculate_expensive_thing
|
107
|
+
# end
|
87
108
|
class MemoizedInstanceVariableName < Base
|
88
109
|
include ConfigurableEnforcedStyle
|
89
110
|
|
@@ -92,32 +113,60 @@ module RuboCop
|
|
92
113
|
UNDERSCORE_REQUIRED = 'Memoized variable `%<var>s` does not start ' \
|
93
114
|
'with `_`. Use `@%<suggested_var>s` instead.'
|
94
115
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
class_method =
|
101
|
-
"(defs self $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
|
102
|
-
"{#{instance_method} #{class_method}}"
|
103
|
-
end
|
116
|
+
# rubocop:disable Metrics/AbcSize
|
117
|
+
def on_or_asgn(node)
|
118
|
+
lhs, _value = *node
|
119
|
+
return unless lhs.ivasgn_type?
|
120
|
+
return unless (method_node = node.each_ancestor(:def, :defs).first)
|
104
121
|
|
105
|
-
|
106
|
-
|
122
|
+
body = method_node.body
|
123
|
+
return unless body == node || body.children.last == node
|
107
124
|
|
108
|
-
|
109
|
-
(method_name,
|
110
|
-
return if matches?(method_name, ivar_assign)
|
125
|
+
method_name = method_node.method_name
|
126
|
+
return if matches?(method_name, lhs)
|
111
127
|
|
112
128
|
msg = format(
|
113
|
-
message(
|
114
|
-
var:
|
129
|
+
message(lhs.children.first.to_s),
|
130
|
+
var: lhs.children.first.to_s,
|
115
131
|
suggested_var: suggested_var(method_name),
|
116
132
|
method: method_name
|
117
133
|
)
|
118
|
-
add_offense(
|
134
|
+
add_offense(lhs, message: msg)
|
135
|
+
end
|
136
|
+
# rubocop:enable Metrics/AbcSize
|
137
|
+
|
138
|
+
def_node_matcher :defined_memoized?, <<~PATTERN
|
139
|
+
(begin
|
140
|
+
(if (defined $(ivar %1)) (return $(ivar %1)) nil?)
|
141
|
+
...
|
142
|
+
$(ivasgn %1 _))
|
143
|
+
PATTERN
|
144
|
+
|
145
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
146
|
+
def on_defined?(node)
|
147
|
+
arg = node.arguments.first
|
148
|
+
return unless arg.ivar_type?
|
149
|
+
|
150
|
+
method_node = node.each_ancestor(:def, :defs).first
|
151
|
+
return unless method_node
|
152
|
+
|
153
|
+
var_name = arg.children.first
|
154
|
+
method_name = method_node.method_name
|
155
|
+
defined_memoized?(method_node.body, var_name) do |defined_ivar, return_ivar, ivar_assign|
|
156
|
+
return if matches?(method_name, ivar_assign)
|
157
|
+
|
158
|
+
msg = format(
|
159
|
+
message(var_name.to_s),
|
160
|
+
var: var_name.to_s,
|
161
|
+
suggested_var: suggested_var(method_name),
|
162
|
+
method: method_name
|
163
|
+
)
|
164
|
+
add_offense(defined_ivar, message: msg)
|
165
|
+
add_offense(return_ivar, message: msg)
|
166
|
+
add_offense(ivar_assign.loc.name, message: msg)
|
167
|
+
end
|
119
168
|
end
|
120
|
-
|
169
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
121
170
|
|
122
171
|
private
|
123
172
|
|
@@ -67,7 +67,8 @@ module RuboCop
|
|
67
67
|
private
|
68
68
|
|
69
69
|
def allowed_method_name?(method_name, prefix)
|
70
|
-
!method_name.
|
70
|
+
!(method_name.start_with?(prefix) && # cheap check to avoid allocating Regexp
|
71
|
+
method_name.match?(/^#{prefix}[^0-9]/)) ||
|
71
72
|
method_name == expected_name(method_name, prefix) ||
|
72
73
|
method_name.end_with?('=') ||
|
73
74
|
allowed_method?(method_name)
|
@@ -7,42 +7,102 @@ module RuboCop
|
|
7
7
|
# configured style, snake_case, normalcase, or non_integer,
|
8
8
|
# for their numbering.
|
9
9
|
#
|
10
|
+
# Additionally, `CheckMethodNames` and `CheckSymbols` configuration options
|
11
|
+
# can be used to specify whether method names and symbols should be checked.
|
12
|
+
# Both are enabled by default.
|
13
|
+
#
|
10
14
|
# @example EnforcedStyle: snake_case
|
11
15
|
# # bad
|
12
|
-
#
|
16
|
+
# :some_sym1
|
13
17
|
# variable1 = 1
|
14
18
|
#
|
15
|
-
#
|
19
|
+
# def some_method1; end
|
16
20
|
#
|
21
|
+
# def some_method_1(arg1); end
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# :some_sym_1
|
17
25
|
# variable_1 = 1
|
18
26
|
#
|
27
|
+
# def some_method_1; end
|
28
|
+
#
|
29
|
+
# def some_method_1(arg_1); end
|
30
|
+
#
|
19
31
|
# @example EnforcedStyle: normalcase (default)
|
20
32
|
# # bad
|
21
|
-
#
|
33
|
+
# :some_sym_1
|
22
34
|
# variable_1 = 1
|
23
35
|
#
|
24
|
-
#
|
36
|
+
# def some_method_1; end
|
25
37
|
#
|
38
|
+
# def some_method1(arg_1); end
|
39
|
+
#
|
40
|
+
# # good
|
41
|
+
# :some_sym1
|
26
42
|
# variable1 = 1
|
27
43
|
#
|
44
|
+
# def some_method1; end
|
45
|
+
#
|
46
|
+
# def some_method1(arg1); end
|
47
|
+
#
|
28
48
|
# @example EnforcedStyle: non_integer
|
29
49
|
# # bad
|
50
|
+
# :some_sym1
|
51
|
+
# :some_sym_1
|
30
52
|
#
|
31
53
|
# variable1 = 1
|
32
|
-
#
|
33
54
|
# variable_1 = 1
|
34
55
|
#
|
56
|
+
# def some_method1; end
|
57
|
+
#
|
58
|
+
# def some_method_1; end
|
59
|
+
#
|
60
|
+
# def some_methodone(arg1); end
|
61
|
+
# def some_methodone(arg_1); end
|
62
|
+
#
|
35
63
|
# # good
|
64
|
+
# :some_symone
|
65
|
+
# :some_sym_one
|
36
66
|
#
|
37
67
|
# variableone = 1
|
38
|
-
#
|
39
68
|
# variable_one = 1
|
69
|
+
#
|
70
|
+
# def some_methodone; end
|
71
|
+
#
|
72
|
+
# def some_method_one; end
|
73
|
+
#
|
74
|
+
# def some_methodone(argone); end
|
75
|
+
# def some_methodone(arg_one); end
|
76
|
+
#
|
77
|
+
# # In the following examples, we assume `EnforcedStyle: normalcase` (default).
|
78
|
+
#
|
79
|
+
# @example CheckMethodNames: true (default)
|
80
|
+
# # bad
|
81
|
+
# def some_method_1; end
|
82
|
+
#
|
83
|
+
# @example CheckMethodNames: false
|
84
|
+
# # good
|
85
|
+
# def some_method_1; end
|
86
|
+
#
|
87
|
+
# @example CheckSymbols: true (default)
|
88
|
+
# # bad
|
89
|
+
# :some_sym_1
|
90
|
+
#
|
91
|
+
# @example CheckSymbols: false
|
92
|
+
# # good
|
93
|
+
# :some_sym_1
|
94
|
+
#
|
95
|
+
# @example AllowedIdentifier: [capture3]
|
96
|
+
# # good
|
97
|
+
# expect(Open3).to receive(:capture3)
|
98
|
+
#
|
40
99
|
class VariableNumber < Base
|
41
100
|
include ConfigurableNumbering
|
42
101
|
|
43
|
-
MSG = 'Use %<style>s for
|
102
|
+
MSG = 'Use %<style>s for %<identifier_type>s numbers.'
|
44
103
|
|
45
104
|
def on_arg(node)
|
105
|
+
@node = node
|
46
106
|
name, = *node
|
47
107
|
check_name(node, name, node.loc.name)
|
48
108
|
end
|
@@ -50,10 +110,40 @@ module RuboCop
|
|
50
110
|
alias on_ivasgn on_arg
|
51
111
|
alias on_cvasgn on_arg
|
52
112
|
|
113
|
+
def on_def(node)
|
114
|
+
@node = node
|
115
|
+
return if allowed_identifier?(node.method_name)
|
116
|
+
|
117
|
+
check_name(node, node.method_name, node.loc.name) if cop_config['CheckMethodNames']
|
118
|
+
end
|
119
|
+
alias on_defs on_def
|
120
|
+
|
121
|
+
def on_sym(node)
|
122
|
+
@node = node
|
123
|
+
return if allowed_identifier?(node.value)
|
124
|
+
|
125
|
+
check_name(node, node.value, node) if cop_config['CheckSymbols']
|
126
|
+
end
|
127
|
+
|
53
128
|
private
|
54
129
|
|
55
130
|
def message(style)
|
56
|
-
|
131
|
+
identifier_type =
|
132
|
+
case @node.type
|
133
|
+
when :def, :defs then 'method name'
|
134
|
+
when :sym then 'symbol'
|
135
|
+
else 'variable'
|
136
|
+
end
|
137
|
+
|
138
|
+
format(MSG, style: style, identifier_type: identifier_type)
|
139
|
+
end
|
140
|
+
|
141
|
+
def allowed_identifier?(name)
|
142
|
+
allowed_identifiers.include?(name.to_s)
|
143
|
+
end
|
144
|
+
|
145
|
+
def allowed_identifiers
|
146
|
+
cop_config.fetch('AllowedIdentifiers', [])
|
57
147
|
end
|
58
148
|
end
|
59
149
|
end
|