rubocop 0.91.1 → 1.1.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 +8 -5
- data/config/default.yml +143 -56
- data/lib/rubocop.rb +17 -5
- data/lib/rubocop/cached_data.rb +2 -1
- 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 +19 -2
- data/lib/rubocop/config_loader_resolver.rb +7 -5
- data/lib/rubocop/config_regeneration.rb +33 -0
- 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 +23 -3
- data/lib/rubocop/cop/commissioner.rb +36 -22
- data/lib/rubocop/cop/corrector.rb +3 -1
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/force.rb +1 -1
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +10 -10
- data/lib/rubocop/cop/layout/array_alignment.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +7 -0
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +6 -9
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +7 -7
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -1
- data/lib/rubocop/cop/layout/extra_spacing.rb +1 -2
- data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +2 -11
- data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +0 -4
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +37 -13
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +2 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +18 -1
- data/lib/rubocop/cop/lint/boolean_symbol.rb +3 -0
- data/lib/rubocop/cop/lint/debugger.rb +2 -3
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +77 -0
- data/lib/rubocop/cop/lint/empty_block.rb +46 -0
- data/lib/rubocop/cop/lint/flip_flop.rb +8 -2
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +37 -0
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +17 -3
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -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/parentheses_as_grouped_expression.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +78 -0
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +95 -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/metrics/block_length.rb +3 -1
- data/lib/rubocop/cop/metrics/class_length.rb +14 -6
- data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
- data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +2 -1
- data/lib/rubocop/cop/offense.rb +18 -5
- data/lib/rubocop/cop/security/open.rb +12 -10
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +6 -2
- data/lib/rubocop/cop/style/accessor_grouping.rb +3 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +142 -0
- data/lib/rubocop/cop/style/array_coercion.rb +4 -0
- data/lib/rubocop/cop/style/case_like_if.rb +20 -4
- data/lib/rubocop/cop/style/class_equality_comparison.rb +64 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +8 -1
- data/lib/rubocop/cop/style/comment_annotation.rb +6 -0
- data/lib/rubocop/cop/style/date_time.rb +12 -1
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +67 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +6 -2
- data/lib/rubocop/cop/style/for.rb +0 -4
- data/lib/rubocop/cop/style/format_string_token.rb +48 -3
- 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/method_def_parentheses.rb +0 -4
- data/lib/rubocop/cop/style/mixin_usage.rb +7 -27
- data/lib/rubocop/cop/style/multiple_comparison.rb +54 -7
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +2 -0
- data/lib/rubocop/cop/style/optional_boolean_parameter.rb +11 -3
- data/lib/rubocop/cop/style/raise_args.rb +0 -3
- data/lib/rubocop/cop/style/redundant_begin.rb +36 -8
- data/lib/rubocop/cop/style/redundant_condition.rb +5 -1
- data/lib/rubocop/cop/style/redundant_interpolation.rb +6 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +45 -24
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -15
- 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/string_concatenation.rb +14 -2
- data/lib/rubocop/cop/style/swap_values.rb +108 -0
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +3 -1
- data/lib/rubocop/cop/team.rb +6 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/variable_force/branch.rb +0 -4
- data/lib/rubocop/ext/regexp_node.rb +29 -10
- data/lib/rubocop/ext/regexp_parser.rb +77 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +12 -5
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- 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 +22 -17
- data/lib/rubocop/result_cache.rb +8 -2
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +5 -5
- data/lib/rubocop/rspec/shared_contexts.rb +4 -0
- data/lib/rubocop/runner.rb +9 -5
- data/lib/rubocop/target_finder.rb +27 -26
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/version.rb +61 -6
- metadata +21 -16
- data/lib/rubocop/cop/mixin/regexp_literal_help.rb +0 -43
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for blocks without a body.
|
7
|
+
# Such empty blocks are typically an oversight or we should provide a comment
|
8
|
+
# be clearer what we're aiming for.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# # bad
|
12
|
+
# items.each { |item| }
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# items.each { |item| puts item }
|
16
|
+
#
|
17
|
+
# @example AllowComments: true (default)
|
18
|
+
# # good
|
19
|
+
# items.each do |item|
|
20
|
+
# # TODO: implement later (inner comment)
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# items.each { |item| } # TODO: implement later (inline comment)
|
24
|
+
#
|
25
|
+
# @example AllowComments: false
|
26
|
+
# # bad
|
27
|
+
# items.each do |item|
|
28
|
+
# # TODO: implement later (inner comment)
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# items.each { |item| } # TODO: implement later (inline comment)
|
32
|
+
#
|
33
|
+
class EmptyBlock < Base
|
34
|
+
MSG = 'Empty block detected.'
|
35
|
+
|
36
|
+
def on_block(node)
|
37
|
+
return if node.body
|
38
|
+
return if cop_config['AllowComments'] &&
|
39
|
+
processed_source.contains_comment?(node.source_range)
|
40
|
+
|
41
|
+
add_offense(node)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -3,8 +3,14 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
# This cop looks for uses of flip-flop operator
|
7
|
-
#
|
6
|
+
# This cop looks for uses of flip-flop operator
|
7
|
+
# based on the Ruby Style Guide.
|
8
|
+
#
|
9
|
+
# Here is the history of flip-flops in Ruby.
|
10
|
+
# flip-flop operator is deprecated in Ruby 2.6.0 and
|
11
|
+
# the deprecation has been reverted by Ruby 2.7.0 and
|
12
|
+
# backported to Ruby 2.6.
|
13
|
+
# See: https://bugs.ruby-lang.org/issues/5400
|
8
14
|
#
|
9
15
|
# @example
|
10
16
|
# # bad
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# Prefer using `Hash#compare_by_identity` than using `object_id` for hash keys.
|
7
|
+
#
|
8
|
+
# This cop is marked as unsafe as a hash possibly can contain other keys
|
9
|
+
# besides `object_id`s.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# # bad
|
13
|
+
# hash = {}
|
14
|
+
# hash[foo.object_id] = :bar
|
15
|
+
# hash.key?(baz.object_id)
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# hash = {}.compare_by_identity
|
19
|
+
# hash[foo] = :bar
|
20
|
+
# hash.key?(baz)
|
21
|
+
#
|
22
|
+
class HashCompareByIdentity < Base
|
23
|
+
RESTRICT_ON_SEND = %i[key? has_key? fetch [] []=].freeze
|
24
|
+
|
25
|
+
MSG = 'Use `Hash#compare_by_identity` instead of using `object_id` for keys.'
|
26
|
+
|
27
|
+
def_node_matcher :id_as_hash_key?, <<~PATTERN
|
28
|
+
(send _ {:key? :has_key? :fetch :[] :[]=} (send _ :object_id) ...)
|
29
|
+
PATTERN
|
30
|
+
|
31
|
+
def on_send(node)
|
32
|
+
add_offense(node) if id_as_hash_key?(node)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -31,12 +31,18 @@ module RuboCop
|
|
31
31
|
return if special_keyword?(final_node)
|
32
32
|
return unless prints_as_self?(final_node)
|
33
33
|
|
34
|
+
# %W and %I split the content into words before expansion
|
35
|
+
# treating each interpolation as a word component, so
|
36
|
+
# interpolation should not be removed if the expanded value
|
37
|
+
# contains a space character.
|
38
|
+
expanded_value = autocorrected_value(final_node)
|
39
|
+
return if in_array_percent_literal?(begin_node) &&
|
40
|
+
/\s/.match?(expanded_value)
|
41
|
+
|
34
42
|
add_offense(final_node) do |corrector|
|
35
43
|
return if final_node.dstr_type? # nested, fixed in next iteration
|
36
44
|
|
37
|
-
|
38
|
-
|
39
|
-
corrector.replace(final_node.parent, value)
|
45
|
+
corrector.replace(final_node.parent, expanded_value)
|
40
46
|
end
|
41
47
|
end
|
42
48
|
|
@@ -92,6 +98,14 @@ module RuboCop
|
|
92
98
|
(COMPOSITE.include?(node.type) &&
|
93
99
|
node.children.all? { |child| prints_as_self?(child) })
|
94
100
|
end
|
101
|
+
|
102
|
+
def in_array_percent_literal?(node)
|
103
|
+
parent = node.parent
|
104
|
+
return false unless parent.dstr_type? || parent.dsym_type?
|
105
|
+
|
106
|
+
grandparent = parent.parent
|
107
|
+
grandparent&.array_type? && grandparent&.percent_literal?
|
108
|
+
end
|
95
109
|
end
|
96
110
|
end
|
97
111
|
end
|
@@ -7,6 +7,17 @@ module RuboCop
|
|
7
7
|
# number conversion can cause unexpected error if auto type conversion
|
8
8
|
# fails. Cop prefer parsing with number class instead.
|
9
9
|
#
|
10
|
+
# Conversion with `Integer`, `Float`, etc. will raise an `ArgumentError`
|
11
|
+
# if given input that is not numeric (eg. an empty string), whereas
|
12
|
+
# `to_i`, etc. will try to convert regardless of input (`''.to_i => 0`).
|
13
|
+
# As such, this cop is disabled by default because it's not necessarily
|
14
|
+
# always correct to raise if a value is not numeric.
|
15
|
+
#
|
16
|
+
# NOTE: Some values cannot be converted properly using one of the `Kernel`
|
17
|
+
# method (for instance, `Time` and `DateTime` values are allowed by this
|
18
|
+
# cop by default). Similarly, Rails' duration methods do not work well
|
19
|
+
# with `Integer()` and can be ignored with `IgnoredMethods`.
|
20
|
+
#
|
10
21
|
# @example
|
11
22
|
#
|
12
23
|
# # bad
|
@@ -20,8 +31,19 @@ module RuboCop
|
|
20
31
|
# Integer('10', 10)
|
21
32
|
# Float('10.2')
|
22
33
|
# Complex('10')
|
34
|
+
#
|
35
|
+
# @example IgnoredMethods: [minutes]
|
36
|
+
#
|
37
|
+
# # good
|
38
|
+
# 10.minutes.to_i
|
39
|
+
#
|
40
|
+
# @example IgnoredClasses: [Time, DateTime] (default)
|
41
|
+
#
|
42
|
+
# # good
|
43
|
+
# Time.now.to_datetime.to_i
|
23
44
|
class NumberConversion < Base
|
24
45
|
extend AutoCorrector
|
46
|
+
include IgnoredMethods
|
25
47
|
|
26
48
|
CONVERSION_METHOD_CLASS_MAPPING = {
|
27
49
|
to_i: "#{Integer.name}(%<number_object>s, 10)",
|
@@ -38,13 +60,9 @@ module RuboCop
|
|
38
60
|
(send $_ ${:to_i :to_f :to_c})
|
39
61
|
PATTERN
|
40
62
|
|
41
|
-
def_node_matcher :datetime?, <<~PATTERN
|
42
|
-
(send (const {nil? (cbase)} {:Time :DateTime}) ...)
|
43
|
-
PATTERN
|
44
|
-
|
45
63
|
def on_send(node)
|
46
64
|
to_method(node) do |receiver, to_method|
|
47
|
-
next if receiver.nil? ||
|
65
|
+
next if receiver.nil? || ignore_receiver?(receiver)
|
48
66
|
|
49
67
|
message = format(
|
50
68
|
MSG,
|
@@ -60,18 +78,33 @@ module RuboCop
|
|
60
78
|
|
61
79
|
private
|
62
80
|
|
63
|
-
def
|
64
|
-
|
65
|
-
|
66
|
-
|
81
|
+
def correct_method(node, receiver)
|
82
|
+
format(CONVERSION_METHOD_CLASS_MAPPING[node.method_name],
|
83
|
+
number_object: receiver.source)
|
84
|
+
end
|
67
85
|
|
68
|
-
|
86
|
+
def ignore_receiver?(receiver)
|
87
|
+
if receiver.send_type? && ignored_method?(receiver.method_name)
|
88
|
+
true
|
89
|
+
elsif (receiver = top_receiver(receiver))
|
90
|
+
receiver.const_type? && ignored_class?(receiver.const_name)
|
91
|
+
else
|
92
|
+
false
|
69
93
|
end
|
70
94
|
end
|
71
95
|
|
72
|
-
def
|
73
|
-
|
74
|
-
|
96
|
+
def top_receiver(node)
|
97
|
+
receiver = node
|
98
|
+
receiver = receiver.receiver until receiver.receiver.nil?
|
99
|
+
receiver
|
100
|
+
end
|
101
|
+
|
102
|
+
def ignored_classes
|
103
|
+
cop_config.fetch('IgnoredClasses', [])
|
104
|
+
end
|
105
|
+
|
106
|
+
def ignored_class?(name)
|
107
|
+
ignored_classes.include?(name.to_s)
|
75
108
|
end
|
76
109
|
end
|
77
110
|
end
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
# puts $1 # => foo
|
20
20
|
#
|
21
21
|
class OutOfRangeRegexpRef < Base
|
22
|
-
MSG = '
|
22
|
+
MSG = '$%<backref>s is out of range (%<count>s regexp capture %<group>s detected).'
|
23
23
|
|
24
24
|
REGEXP_RECEIVER_METHODS = %i[=~ === match].to_set.freeze
|
25
25
|
REGEXP_ARGUMENT_METHODS = %i[=~ match grep gsub gsub! sub sub! [] slice slice! index rindex
|
@@ -35,14 +35,13 @@ module RuboCop
|
|
35
35
|
check_regexp(node.children.first)
|
36
36
|
end
|
37
37
|
|
38
|
-
def
|
38
|
+
def after_send(node)
|
39
39
|
@valid_ref = nil
|
40
40
|
|
41
|
-
if node
|
42
|
-
check_regexp(node.receiver)
|
43
|
-
elsif node.first_argument&.regexp_type? \
|
44
|
-
&& REGEXP_ARGUMENT_METHODS.include?(node.method_name)
|
41
|
+
if regexp_first_argument?(node)
|
45
42
|
check_regexp(node.first_argument)
|
43
|
+
elsif regexp_receiver?(node)
|
44
|
+
check_regexp(node.receiver)
|
46
45
|
end
|
47
46
|
end
|
48
47
|
|
@@ -56,9 +55,16 @@ module RuboCop
|
|
56
55
|
|
57
56
|
def on_nth_ref(node)
|
58
57
|
backref, = *node
|
59
|
-
return if @valid_ref.nil?
|
58
|
+
return if @valid_ref.nil? || backref <= @valid_ref
|
59
|
+
|
60
|
+
message = format(
|
61
|
+
MSG,
|
62
|
+
backref: backref,
|
63
|
+
count: @valid_ref.zero? ? 'no' : @valid_ref,
|
64
|
+
group: @valid_ref == 1 ? 'group' : 'groups'
|
65
|
+
)
|
60
66
|
|
61
|
-
add_offense(node
|
67
|
+
add_offense(node, message: message)
|
62
68
|
end
|
63
69
|
|
64
70
|
private
|
@@ -73,6 +79,19 @@ module RuboCop
|
|
73
79
|
node.each_capture(named: false).count
|
74
80
|
end
|
75
81
|
end
|
82
|
+
|
83
|
+
def regexp_first_argument?(send_node)
|
84
|
+
send_node.first_argument&.regexp_type? \
|
85
|
+
&& REGEXP_ARGUMENT_METHODS.include?(send_node.method_name)
|
86
|
+
end
|
87
|
+
|
88
|
+
def regexp_receiver?(send_node)
|
89
|
+
send_node.receiver&.regexp_type?
|
90
|
+
end
|
91
|
+
|
92
|
+
def nth_ref_receiver?(send_node)
|
93
|
+
send_node.receiver&.nth_ref_type?
|
94
|
+
end
|
76
95
|
end
|
77
96
|
end
|
78
97
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for redundant safe navigation calls.
|
7
|
+
# `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, and `equal?` methods
|
8
|
+
# are checked by default. These are customizable with `AllowedMethods` option.
|
9
|
+
#
|
10
|
+
# This cop is marked as unsafe, because auto-correction can change the
|
11
|
+
# return type of the expression. An offending expression that previously
|
12
|
+
# could return `nil` will be auto-corrected to never return `nil`.
|
13
|
+
#
|
14
|
+
# In the example below, the safe navigation operator (`&.`) is unnecessary
|
15
|
+
# because `NilClass` has methods like `respond_to?` and `is_a?`.
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# # bad
|
19
|
+
# do_something if attrs&.respond_to?(:[])
|
20
|
+
#
|
21
|
+
# # good
|
22
|
+
# do_something if attrs.respond_to?(:[])
|
23
|
+
#
|
24
|
+
# # bad
|
25
|
+
# while node&.is_a?(BeginNode)
|
26
|
+
# node = node.parent
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# while node.is_a?(BeginNode)
|
31
|
+
# node = node.parent
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# # good - without `&.` this will always return `true`
|
35
|
+
# foo&.respond_to?(:to_a)
|
36
|
+
#
|
37
|
+
class RedundantSafeNavigation < Base
|
38
|
+
include AllowedMethods
|
39
|
+
include RangeHelp
|
40
|
+
extend AutoCorrector
|
41
|
+
|
42
|
+
MSG = 'Redundant safe navigation detected.'
|
43
|
+
|
44
|
+
NIL_SPECIFIC_METHODS = (nil.methods - Object.new.methods).to_set.freeze
|
45
|
+
|
46
|
+
def_node_matcher :respond_to_nil_specific_method?, <<~PATTERN
|
47
|
+
(csend _ :respond_to? (sym %NIL_SPECIFIC_METHODS))
|
48
|
+
PATTERN
|
49
|
+
|
50
|
+
def on_csend(node)
|
51
|
+
return unless check?(node) && allowed_method?(node.method_name)
|
52
|
+
return if respond_to_nil_specific_method?(node)
|
53
|
+
|
54
|
+
range = range_between(node.loc.dot.begin_pos, node.source_range.end_pos)
|
55
|
+
add_offense(range) do |corrector|
|
56
|
+
corrector.replace(node.loc.dot, '.')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def check?(node)
|
63
|
+
parent = node.parent
|
64
|
+
return false unless parent
|
65
|
+
|
66
|
+
condition?(parent, node) ||
|
67
|
+
parent.and_type? ||
|
68
|
+
parent.or_type? ||
|
69
|
+
(parent.send_type? && parent.negation_method?)
|
70
|
+
end
|
71
|
+
|
72
|
+
def condition?(parent, node)
|
73
|
+
(parent.conditional? || parent.post_condition_loop?) && parent.condition == node
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop ensures that `to_enum`/`enum_for`, called for the current method,
|
7
|
+
# has correct arguments.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# def method(x, y = 1)
|
12
|
+
# return to_enum(__method__, x) # `y` is missing
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # good
|
16
|
+
# def method(x, y = 1)
|
17
|
+
# return to_enum(__method__, x, y)
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# # bad
|
21
|
+
# def method(required:)
|
22
|
+
# return to_enum(:method, required: something) # `required` has incorrect value
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# def method(required:)
|
27
|
+
# return to_enum(:method, required: required)
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
class ToEnumArguments < Base
|
31
|
+
MSG = 'Ensure you correctly provided all the arguments.'
|
32
|
+
|
33
|
+
RESTRICT_ON_SEND = %i[to_enum enum_for].freeze
|
34
|
+
|
35
|
+
def_node_matcher :enum_conversion_call?, <<~PATTERN
|
36
|
+
(send {nil? self} {:to_enum :enum_for} $_ $...)
|
37
|
+
PATTERN
|
38
|
+
|
39
|
+
def_node_matcher :method_name?, <<~PATTERN
|
40
|
+
{(send nil? :__method__) (sym %1)}
|
41
|
+
PATTERN
|
42
|
+
|
43
|
+
def_node_matcher :passing_keyword_arg?, <<~PATTERN
|
44
|
+
(pair (sym %1) (lvar %1))
|
45
|
+
PATTERN
|
46
|
+
|
47
|
+
def on_send(node)
|
48
|
+
def_node = node.each_ancestor(:def, :defs).first
|
49
|
+
return unless def_node
|
50
|
+
|
51
|
+
enum_conversion_call?(node) do |method_node, arguments|
|
52
|
+
add_offense(node) unless method_name?(method_node, def_node.method_name) &&
|
53
|
+
arguments_match?(arguments, def_node)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def arguments_match?(arguments, def_node)
|
60
|
+
index = 0
|
61
|
+
|
62
|
+
def_node.arguments.reject(&:blockarg_type?).all? do |def_arg|
|
63
|
+
send_arg = arguments[index]
|
64
|
+
case def_arg.type
|
65
|
+
when :arg, :restarg, :optarg
|
66
|
+
index += 1
|
67
|
+
end
|
68
|
+
|
69
|
+
send_arg && argument_match?(send_arg, def_arg)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
74
|
+
def argument_match?(send_arg, def_arg)
|
75
|
+
def_arg_name = def_arg.children[0]
|
76
|
+
|
77
|
+
case def_arg.type
|
78
|
+
when :arg, :restarg
|
79
|
+
send_arg.source == def_arg.source
|
80
|
+
when :optarg
|
81
|
+
send_arg.source == def_arg_name.to_s
|
82
|
+
when :kwoptarg, :kwarg
|
83
|
+
send_arg.hash_type? &&
|
84
|
+
send_arg.pairs.any? { |pair| passing_keyword_arg?(pair, def_arg_name) }
|
85
|
+
when :kwrestarg
|
86
|
+
send_arg.each_child_node(:kwsplat).any? { |child| child.source == def_arg.source }
|
87
|
+
when :forward_arg
|
88
|
+
send_arg.forwarded_args_type?
|
89
|
+
end
|
90
|
+
end
|
91
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|