rubocop 1.70.0 → 1.71.2
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 +17 -0
- data/lib/rubocop/cli/command/show_cops.rb +24 -2
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/cop/autocorrect_logic.rb +1 -1
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/location_expression.rb +2 -1
- data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +3 -2
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb +63 -0
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +131 -0
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +229 -0
- data/lib/rubocop/cop/internal_affairs/node_type_multiple_predicates.rb +126 -0
- data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +90 -0
- data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +3 -1
- data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +5 -4
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/class_structure.rb +9 -9
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +7 -11
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -1
- data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -5
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
- data/lib/rubocop/cop/layout/space_after_comma.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_semicolon.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -0
- data/lib/rubocop/cop/layout/space_before_comma.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_semicolon.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
- data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +119 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -3
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +1 -1
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +3 -3
- data/lib/rubocop/cop/lint/constant_reassignment.rb +2 -6
- data/lib/rubocop/cop/lint/debugger.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_set_element.rb +1 -1
- data/lib/rubocop/cop/lint/empty_expression.rb +0 -2
- data/lib/rubocop/cop/lint/float_comparison.rb +5 -2
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +13 -3
- data/lib/rubocop/cop/lint/missing_super.rb +2 -2
- data/lib/rubocop/cop/lint/mixed_case_range.rb +1 -1
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -3
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +1 -1
- data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +13 -18
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -5
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +2 -2
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +8 -1
- data/lib/rubocop/cop/lint/shared_mutable_default.rb +3 -3
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +1 -1
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -4
- data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
- data/lib/rubocop/cop/lint/useless_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +2 -1
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +2 -7
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/collection_literal_length.rb +7 -0
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/module_length.rb +1 -1
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -5
- data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +4 -4
- data/lib/rubocop/cop/mixin/hash_subset.rb +188 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +1 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +22 -8
- data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -2
- data/lib/rubocop/cop/mixin/string_help.rb +1 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -3
- data/lib/rubocop/cop/naming/block_forwarding.rb +18 -14
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +3 -3
- data/lib/rubocop/cop/security/compound_hash.rb +1 -0
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -4
- data/lib/rubocop/cop/style/arguments_forwarding.rb +38 -19
- data/lib/rubocop/cop/style/array_first_last.rb +18 -2
- data/lib/rubocop/cop/style/block_delimiters.rb +7 -20
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +1 -1
- data/lib/rubocop/cop/style/combinable_defined.rb +1 -1
- data/lib/rubocop/cop/style/combinable_loops.rb +2 -2
- data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +6 -4
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/double_negation.rb +3 -3
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
- data/lib/rubocop/cop/style/each_with_object.rb +2 -3
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/exact_regexp_match.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +15 -2
- data/lib/rubocop/cop/style/fetch_env_var.rb +1 -1
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/hash_each_methods.rb +3 -6
- data/lib/rubocop/cop/style/hash_except.rb +20 -131
- data/lib/rubocop/cop/style/hash_slice.rb +80 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +22 -3
- data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -3
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
- data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +6 -6
- data/lib/rubocop/cop/style/it_assignment.rb +1 -1
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
- data/lib/rubocop/cop/style/map_into_array.rb +1 -1
- data/lib/rubocop/cop/style/map_to_hash.rb +1 -1
- data/lib/rubocop/cop/style/map_to_set.rb +3 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -13
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +2 -2
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
- data/lib/rubocop/cop/style/open_struct_use.rb +5 -5
- data/lib/rubocop/cop/style/parallel_assignment.rb +1 -5
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -2
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_condition.rb +2 -2
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +6 -10
- data/lib/rubocop/cop/style/redundant_each.rb +1 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +2 -2
- data/lib/rubocop/cop/style/redundant_freeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +27 -10
- data/lib/rubocop/cop/style/redundant_parentheses.rb +9 -6
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +12 -27
- data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
- data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -2
- data/lib/rubocop/cop/style/return_nil.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
- data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/string_methods.rb +1 -1
- data/lib/rubocop/cop/style/super_arguments.rb +4 -4
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -1
- data/lib/rubocop/cop/style/while_until_modifier.rb +0 -1
- data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
- data/lib/rubocop/cop/util.rb +2 -2
- data/lib/rubocop/cop/variable_force/variable.rb +14 -2
- data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
- data/lib/rubocop/cops_documentation_generator.rb +13 -13
- data/lib/rubocop/directive_comment.rb +9 -8
- data/lib/rubocop/options.rb +2 -1
- data/lib/rubocop/result_cache.rb +13 -13
- data/lib/rubocop/rspec/expect_offense.rb +6 -2
- data/lib/rubocop/rspec/support.rb +1 -2
- data/lib/rubocop/target_finder.rb +1 -0
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +3 -0
- metadata +15 -11
- data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -28
@@ -69,11 +69,7 @@ module RuboCop
|
|
69
69
|
elements = Array(node).compact
|
70
70
|
|
71
71
|
# Account for edge case of `Constant::CONSTANT`
|
72
|
-
!node.array_type? ||
|
73
|
-
end
|
74
|
-
|
75
|
-
def return_of_method_call?(node)
|
76
|
-
node.block_type? || node.send_type?
|
72
|
+
!node.array_type? || elements.any?(&:splat_type?)
|
77
73
|
end
|
78
74
|
|
79
75
|
def assignment_corrector(node, rhs, order)
|
@@ -94,8 +94,8 @@ module RuboCop
|
|
94
94
|
end
|
95
95
|
|
96
96
|
def require_parentheses?(node, condition_body)
|
97
|
-
return false
|
98
|
-
return false
|
97
|
+
return false unless node.type?(:while, :until)
|
98
|
+
return false unless condition_body.any_block_type?
|
99
99
|
|
100
100
|
condition_body.send_node.block_literal? && condition_body.keywords?
|
101
101
|
end
|
@@ -19,8 +19,7 @@ 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?,
|
23
|
-
'({block numblock} $(send (const {nil? cbase} :Proc) :new) ...)'
|
22
|
+
def_node_matcher :proc_new?, '(any_block $(send (const {nil? cbase} :Proc) :new) ...)'
|
24
23
|
|
25
24
|
def on_block(node)
|
26
25
|
proc_new?(node) do |block_method|
|
@@ -14,7 +14,7 @@ module RuboCop
|
|
14
14
|
# passed multiple arguments.
|
15
15
|
#
|
16
16
|
# The exploded style has an `AllowedCompactTypes` configuration
|
17
|
-
# option that takes an Array of exception name Strings.
|
17
|
+
# option that takes an `Array` of exception name Strings.
|
18
18
|
#
|
19
19
|
# @safety
|
20
20
|
# This cop is unsafe because `raise Foo` calls `Foo.exception`, not `Foo.new`.
|
@@ -157,7 +157,7 @@ module RuboCop
|
|
157
157
|
end
|
158
158
|
|
159
159
|
def asgn_type?(node)
|
160
|
-
node.
|
160
|
+
node.type?(:lvasgn, :ivasgn, :cvasgn, :gvasgn)
|
161
161
|
end
|
162
162
|
|
163
163
|
def branches_have_method?(node)
|
@@ -184,7 +184,7 @@ module RuboCop
|
|
184
184
|
return false unless argument.hash_type?
|
185
185
|
return false unless (node = argument.children.first)
|
186
186
|
|
187
|
-
node.
|
187
|
+
node.type?(:kwsplat, :forwarded_kwrestarg)
|
188
188
|
end
|
189
189
|
|
190
190
|
def if_source(if_branch, arithmetic_operation)
|
@@ -25,11 +25,11 @@ module RuboCop
|
|
25
25
|
MSG = 'Remove the redundant double splat and braces, use keyword arguments directly.'
|
26
26
|
MERGE_METHODS = %i[merge merge!].freeze
|
27
27
|
|
28
|
-
# rubocop:disable Metrics/
|
28
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
29
29
|
def on_hash(node)
|
30
30
|
return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
|
31
31
|
return unless (parent = node.parent)
|
32
|
-
return unless parent.
|
32
|
+
return unless parent.type?(:call, :kwsplat)
|
33
33
|
return unless mergeable?(parent)
|
34
34
|
return unless (kwsplat = node.each_ancestor(:kwsplat).first)
|
35
35
|
return if !node.braces? || allowed_double_splat_receiver?(kwsplat)
|
@@ -38,13 +38,13 @@ module RuboCop
|
|
38
38
|
autocorrect(corrector, node, kwsplat)
|
39
39
|
end
|
40
40
|
end
|
41
|
-
# rubocop:enable Metrics/
|
41
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
42
42
|
|
43
43
|
private
|
44
44
|
|
45
45
|
def allowed_double_splat_receiver?(kwsplat)
|
46
46
|
first_child = kwsplat.children.first
|
47
|
-
return true if first_child.
|
47
|
+
return true if first_child.any_block_type?
|
48
48
|
return false unless first_child.call_type?
|
49
49
|
|
50
50
|
root_receiver = root_receiver(first_child)
|
@@ -73,7 +73,7 @@ module RuboCop
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def select_merge_method_nodes(kwsplat)
|
76
|
-
|
76
|
+
kwsplat.each_descendant(:call).select do |node|
|
77
77
|
mergeable?(node)
|
78
78
|
end
|
79
79
|
end
|
@@ -89,7 +89,7 @@ module RuboCop
|
|
89
89
|
def autocorrect_merge_methods(corrector, merge_methods, kwsplat)
|
90
90
|
range = range_of_merge_methods(merge_methods)
|
91
91
|
|
92
|
-
new_kwsplat_arguments =
|
92
|
+
new_kwsplat_arguments = kwsplat.each_descendant(:call).map do |descendant|
|
93
93
|
convert_to_new_arguments(descendant)
|
94
94
|
end
|
95
95
|
new_source = new_kwsplat_arguments.compact.reverse.unshift('').join(', ')
|
@@ -104,10 +104,6 @@ module RuboCop
|
|
104
104
|
begin_merge_method.loc.dot.begin.join(end_merge_method.source_range.end)
|
105
105
|
end
|
106
106
|
|
107
|
-
def extract_send_methods(kwsplat)
|
108
|
-
kwsplat.each_descendant(:send, :csend)
|
109
|
-
end
|
110
|
-
|
111
107
|
def convert_to_new_arguments(node)
|
112
108
|
return unless mergeable?(node)
|
113
109
|
|
@@ -65,7 +65,7 @@ module RuboCop
|
|
65
65
|
return if node.last_argument&.block_pass_type?
|
66
66
|
|
67
67
|
if node.method?(:each) && !node.parent&.block_type?
|
68
|
-
ancestor_node = node.each_ancestor(:
|
68
|
+
ancestor_node = node.each_ancestor(:call).detect do |ancestor|
|
69
69
|
ancestor.receiver == node &&
|
70
70
|
(RESTRICT_ON_SEND.include?(ancestor.method_name) || ancestor.method?(:reverse_each))
|
71
71
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Checks for RuntimeError as the argument of raise
|
6
|
+
# Checks for `RuntimeError` as the argument of `raise`/`fail`.
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
# # bad
|
@@ -51,7 +51,7 @@ module RuboCop
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def string_message?(message)
|
54
|
-
message.
|
54
|
+
message.type?(:str, :dstr, :xstr)
|
55
55
|
end
|
56
56
|
|
57
57
|
def fix_compact(node)
|
@@ -5,7 +5,7 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# Check for uses of `Object#freeze` on immutable objects.
|
7
7
|
#
|
8
|
-
# NOTE: Regexp and Range literals are frozen objects since Ruby 3.0.
|
8
|
+
# NOTE: `Regexp` and `Range` literals are frozen objects since Ruby 3.0.
|
9
9
|
#
|
10
10
|
# NOTE: From Ruby 3.0, this cop allows explicit freezing of interpolated
|
11
11
|
# string literals when `# frozen-string-literal: true` is used.
|
@@ -42,7 +42,7 @@ module RuboCop
|
|
42
42
|
return true if node.immutable_literal?
|
43
43
|
return true if frozen_string_literal?(node)
|
44
44
|
|
45
|
-
target_ruby_version >= 3.0 &&
|
45
|
+
target_ruby_version >= 3.0 && node.type?(:regexp, :range)
|
46
46
|
end
|
47
47
|
|
48
48
|
def strip_parenthesis(node)
|
@@ -73,10 +73,15 @@ module RuboCop
|
|
73
73
|
LINE_CONTINUATION_PATTERN = /(\\\n)/.freeze
|
74
74
|
ALLOWED_STRING_TOKENS = %i[tSTRING tSTRING_CONTENT].freeze
|
75
75
|
ARGUMENT_TYPES = %i[
|
76
|
-
kDEF kFALSE kNIL kSELF kTRUE
|
77
|
-
|
76
|
+
kDEF kDEFINED kFALSE kNIL kSELF kTRUE tAMPER tBANG tCARET tCHARACTER tCOLON3 tCONSTANT
|
77
|
+
tCVAR tDOT2 tDOT3 tFLOAT tGVAR tIDENTIFIER tINTEGER tIVAR tLAMBDA tLBRACK tLCURLY
|
78
|
+
tLPAREN_ARG tPIPE tQSYMBOLS_BEG tQWORDS_BEG tREGEXP_BEG tSTAR tSTRING tSTRING_BEG tSYMBEG
|
79
|
+
tSYMBOL tSYMBOLS_BEG tTILDE tUMINUS tUNARY_NUM tUPLUS tWORDS_BEG tXSTRING_BEG
|
78
80
|
].freeze
|
79
|
-
ARGUMENT_TAKING_FLOW_TOKEN_TYPES = %i[
|
81
|
+
ARGUMENT_TAKING_FLOW_TOKEN_TYPES = %i[
|
82
|
+
tIDENTIFIER kBREAK kNEXT kRETURN kSUPER kYIELD
|
83
|
+
].freeze
|
84
|
+
ARITHMETIC_OPERATOR_TOKENS = %i[tDIVIDE tDSTAR tMINUS tPERCENT tPLUS tSTAR2].freeze
|
80
85
|
|
81
86
|
def on_new_investigation
|
82
87
|
return unless processed_source.ast
|
@@ -96,15 +101,20 @@ module RuboCop
|
|
96
101
|
private
|
97
102
|
|
98
103
|
def require_line_continuation?(range)
|
99
|
-
!
|
104
|
+
!ends_with_uncommented_backslash?(range) ||
|
100
105
|
string_concatenation?(range.source_line) ||
|
101
|
-
start_with_arithmetic_operator?(
|
106
|
+
start_with_arithmetic_operator?(range) ||
|
102
107
|
inside_string_literal_or_method_with_argument?(range) ||
|
103
108
|
leading_dot_method_chain_with_blank_line?(range)
|
104
109
|
end
|
105
110
|
|
106
|
-
def
|
107
|
-
|
111
|
+
def ends_with_uncommented_backslash?(range)
|
112
|
+
# A line continuation always needs to be the last character on the line, which
|
113
|
+
# means that it is impossible to have a comment following a continuation.
|
114
|
+
# Therefore, if the line contains a comment, it cannot end with a continuation.
|
115
|
+
return false if processed_source.line_with_comment?(range.line)
|
116
|
+
|
117
|
+
range.source_line.end_with?(LINE_CONTINUATION)
|
108
118
|
end
|
109
119
|
|
110
120
|
def string_concatenation?(source_line)
|
@@ -140,7 +150,7 @@ module RuboCop
|
|
140
150
|
|
141
151
|
def inspect_end_of_ruby_code_line_continuation
|
142
152
|
last_line = processed_source.lines[processed_source.ast.last_line - 1]
|
143
|
-
return unless
|
153
|
+
return unless code_ends_with_continuation?(last_line)
|
144
154
|
|
145
155
|
last_column = last_line.length
|
146
156
|
line_continuation_range = range_between(last_column - 1, last_column)
|
@@ -150,6 +160,12 @@ module RuboCop
|
|
150
160
|
end
|
151
161
|
end
|
152
162
|
|
163
|
+
def code_ends_with_continuation?(last_line)
|
164
|
+
return false if processed_source.line_with_comment?(processed_source.ast.last_line)
|
165
|
+
|
166
|
+
last_line.end_with?(LINE_CONTINUATION)
|
167
|
+
end
|
168
|
+
|
153
169
|
def inside_string_literal?(range, token)
|
154
170
|
ALLOWED_STRING_TOKENS.include?(token.type) && token.pos.overlaps?(range)
|
155
171
|
end
|
@@ -213,8 +229,9 @@ module RuboCop
|
|
213
229
|
node.call_type? && !node.arguments.empty?
|
214
230
|
end
|
215
231
|
|
216
|
-
def start_with_arithmetic_operator?(
|
217
|
-
|
232
|
+
def start_with_arithmetic_operator?(range)
|
233
|
+
line_range = processed_source.buffer.line_range(range.line + 1)
|
234
|
+
ARITHMETIC_OPERATOR_TOKENS.include?(processed_source.first_token_of(line_range).type)
|
218
235
|
end
|
219
236
|
end
|
220
237
|
end
|
@@ -50,7 +50,7 @@ module RuboCop
|
|
50
50
|
def ignore_syntax?(node)
|
51
51
|
return false unless (parent = node.parent)
|
52
52
|
|
53
|
-
parent.
|
53
|
+
parent.type?(:while_post, :until_post, :match_with_lvasgn) ||
|
54
54
|
like_method_argument_parentheses?(parent) || multiline_control_flow_statements?(node)
|
55
55
|
end
|
56
56
|
|
@@ -72,7 +72,7 @@ module RuboCop
|
|
72
72
|
ancestor = node.ancestors.first
|
73
73
|
return false unless ancestor
|
74
74
|
|
75
|
-
!ancestor.
|
75
|
+
!ancestor.type?(:begin, :def, :any_block)
|
76
76
|
end
|
77
77
|
|
78
78
|
def allowed_ternary?(node)
|
@@ -89,7 +89,7 @@ module RuboCop
|
|
89
89
|
end
|
90
90
|
|
91
91
|
def like_method_argument_parentheses?(node)
|
92
|
-
return false
|
92
|
+
return false unless node.type?(:send, :super, :yield)
|
93
93
|
|
94
94
|
node.arguments.one? && !node.parenthesized? &&
|
95
95
|
!node.arithmetic_operation? && node.first_argument.begin_type?
|
@@ -99,7 +99,7 @@ module RuboCop
|
|
99
99
|
return false unless (parent = node.parent)
|
100
100
|
return false if parent.single_line?
|
101
101
|
|
102
|
-
parent.
|
102
|
+
parent.type?(:return, :next, :break)
|
103
103
|
end
|
104
104
|
|
105
105
|
def empty_parentheses?(node)
|
@@ -140,6 +140,9 @@ module RuboCop
|
|
140
140
|
return 'a literal' if disallowed_literal?(begin_node, node)
|
141
141
|
return 'a variable' if node.variable?
|
142
142
|
return 'a constant' if node.const_type?
|
143
|
+
if node.assignment? && (begin_node.parent.nil? || begin_node.parent.begin_type?)
|
144
|
+
return 'an assignment'
|
145
|
+
end
|
143
146
|
if node.lambda_or_proc? && (node.braces? || node.send_node.lambda_literal?)
|
144
147
|
return 'an expression'
|
145
148
|
end
|
@@ -276,9 +279,9 @@ module RuboCop
|
|
276
279
|
end
|
277
280
|
|
278
281
|
def do_end_block_in_method_chain?(begin_node, node)
|
279
|
-
return false unless (block = node.each_descendant(:
|
282
|
+
return false unless (block = node.each_descendant(:any_block).first)
|
280
283
|
|
281
|
-
block.keywords? && begin_node.each_ancestor(:
|
284
|
+
block.keywords? && begin_node.each_ancestor(:call).any?
|
282
285
|
end
|
283
286
|
end
|
284
287
|
end
|
@@ -21,12 +21,8 @@ module RuboCop
|
|
21
21
|
# args += foo
|
22
22
|
# hash.merge!(other)
|
23
23
|
#
|
24
|
-
# # bad
|
25
|
-
# self.foo = foo.concat(ary)
|
26
|
-
#
|
27
24
|
# # good
|
28
25
|
# foo.concat(ary)
|
29
|
-
# self.foo += ary
|
30
26
|
#
|
31
27
|
class RedundantSelfAssignment < Base
|
32
28
|
include RangeHelp
|
@@ -49,6 +45,16 @@ module RuboCop
|
|
49
45
|
gvasgn: :gvar
|
50
46
|
}.freeze
|
51
47
|
|
48
|
+
# @!method redundant_self_assignment?
|
49
|
+
def_node_matcher :redundant_self_assignment?, <<~PATTERN
|
50
|
+
(call
|
51
|
+
%1 _
|
52
|
+
(call
|
53
|
+
(call
|
54
|
+
%1 %2) #method_returning_self?
|
55
|
+
...))
|
56
|
+
PATTERN
|
57
|
+
|
52
58
|
# rubocop:disable Metrics/AbcSize
|
53
59
|
def on_lvasgn(node)
|
54
60
|
return unless (rhs = node.rhs)
|
@@ -85,31 +91,10 @@ module RuboCop
|
|
85
91
|
METHODS_RETURNING_SELF.include?(method_name)
|
86
92
|
end
|
87
93
|
|
88
|
-
# @!method redundant_self_assignment?(node, method_name)
|
89
|
-
def_node_matcher :redundant_self_assignment?, <<~PATTERN
|
90
|
-
(send
|
91
|
-
(self) _
|
92
|
-
(call
|
93
|
-
(send
|
94
|
-
{(self) nil?} %1) #method_returning_self?
|
95
|
-
...))
|
96
|
-
PATTERN
|
97
|
-
|
98
|
-
# @!method redundant_nonself_assignment?(node, receiver, method_name)
|
99
|
-
def_node_matcher :redundant_nonself_assignment?, <<~PATTERN
|
100
|
-
(call
|
101
|
-
%1 _
|
102
|
-
(call
|
103
|
-
(call
|
104
|
-
%1 %2) #method_returning_self?
|
105
|
-
...))
|
106
|
-
PATTERN
|
107
|
-
|
108
94
|
def redundant_assignment?(node)
|
109
|
-
receiver_name = node.method_name.to_s
|
95
|
+
receiver_name = node.method_name.to_s.delete_suffix('=').to_sym
|
110
96
|
|
111
|
-
redundant_self_assignment?(node, receiver_name)
|
112
|
-
redundant_nonself_assignment?(node, node.receiver, receiver_name)
|
97
|
+
redundant_self_assignment?(node, node.receiver, receiver_name)
|
113
98
|
end
|
114
99
|
|
115
100
|
def correction_range(node)
|
@@ -93,9 +93,9 @@ module RuboCop
|
|
93
93
|
(call $(call _ $:sort_by _) ${:last :first})
|
94
94
|
(send $(send _ $:sort_by _) ${:[] :at :slice} {(int 0) (int -1)})
|
95
95
|
|
96
|
-
(call (
|
96
|
+
(call (any_block $(call _ ${:sort_by :sort}) ...) ${:last :first})
|
97
97
|
(call
|
98
|
-
(
|
98
|
+
(any_block $(call _ ${:sort_by :sort}) ...)
|
99
99
|
${:[] :at :slice} {(int 0) (int -1)}
|
100
100
|
)
|
101
101
|
}
|
@@ -41,7 +41,7 @@ module RuboCop
|
|
41
41
|
MSG = 'Redundant escape of %<char>s inside string literal.'
|
42
42
|
|
43
43
|
def on_str(node)
|
44
|
-
return if node.parent&.
|
44
|
+
return if node.parent&.type?(:regexp, :xstr) || node.character_literal?
|
45
45
|
|
46
46
|
str_contents_range = str_contents_range(node)
|
47
47
|
|
@@ -147,7 +147,7 @@ module RuboCop
|
|
147
147
|
end
|
148
148
|
|
149
149
|
def heredoc?(node)
|
150
|
-
|
150
|
+
node.type?(:str, :dstr) && node.heredoc?
|
151
151
|
end
|
152
152
|
|
153
153
|
def delimiter?(node, char)
|
@@ -312,7 +312,7 @@ module RuboCop
|
|
312
312
|
end
|
313
313
|
|
314
314
|
def chain_length(method_chain, method)
|
315
|
-
method.each_ancestor(:
|
315
|
+
method.each_ancestor(:call).inject(0) do |total, ancestor|
|
316
316
|
break total + 1 if ancestor == method_chain
|
317
317
|
|
318
318
|
total + 1
|
@@ -10,7 +10,7 @@ module RuboCop
|
|
10
10
|
# parameters.
|
11
11
|
#
|
12
12
|
# Configuration option: Methods
|
13
|
-
# Should be set to use this cop. Array of hashes, where each key is the
|
13
|
+
# Should be set to use this cop. `Array` of hashes, where each key is the
|
14
14
|
# method name and value - array of argument names.
|
15
15
|
#
|
16
16
|
# @example Methods: [{reduce: %w[a b]}]
|
@@ -68,7 +68,7 @@ module RuboCop
|
|
68
68
|
return false if body_node.parent.assignment_method? ||
|
69
69
|
NOT_SUPPORTED_ENDLESS_METHOD_BODY_TYPES.include?(body_node.type)
|
70
70
|
|
71
|
-
!
|
71
|
+
!body_node.type?(:begin, :kwbegin)
|
72
72
|
end
|
73
73
|
|
74
74
|
def correct_to_multiline(corrector, node)
|
@@ -156,7 +156,7 @@ module RuboCop
|
|
156
156
|
# Handle `send` and `block` nodes that need to be wrapped in parens
|
157
157
|
# FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens,
|
158
158
|
# but wrapping the argument list would be a more ergonomic correction.
|
159
|
-
node_to_check = condition&.
|
159
|
+
node_to_check = condition&.any_block_type? ? condition.send_node : condition
|
160
160
|
return unless wrap_condition?(node_to_check)
|
161
161
|
|
162
162
|
if condition.call_type?
|
@@ -243,7 +243,7 @@ module RuboCop
|
|
243
243
|
def replace_condition(condition)
|
244
244
|
return condition.source unless wrap_condition?(condition)
|
245
245
|
|
246
|
-
if condition.call_type?
|
246
|
+
if condition.call_type? && !condition.comparison_method?
|
247
247
|
parenthesized_method_arguments(condition)
|
248
248
|
else
|
249
249
|
"(#{condition.source})"
|
@@ -98,7 +98,7 @@ module RuboCop
|
|
98
98
|
# When defining dynamic methods, implicitly calling `super` is not possible.
|
99
99
|
# Since there is a possibility of delegation to `define_method`,
|
100
100
|
# `super` used within the block is always allowed.
|
101
|
-
break if node.
|
101
|
+
break if node.any_block_type? && !block_sends_to_super?(super_node, node)
|
102
102
|
|
103
103
|
break node if DEF_TYPES.include?(node.type)
|
104
104
|
end
|
@@ -136,13 +136,13 @@ module RuboCop
|
|
136
136
|
# Checks if the send node of a block is the given super node,
|
137
137
|
# or a method chain containing it.
|
138
138
|
return false unless parent_node
|
139
|
-
return false unless parent_node.
|
139
|
+
return false unless parent_node.any_block_type?
|
140
140
|
|
141
141
|
parent_node.send_node.each_node(:super).any?(super_node)
|
142
142
|
end
|
143
143
|
|
144
144
|
def positional_arg_same?(def_arg, super_arg)
|
145
|
-
return false unless def_arg.
|
145
|
+
return false unless def_arg.type?(:arg, :optarg)
|
146
146
|
return false unless super_arg.lvar_type?
|
147
147
|
|
148
148
|
def_arg.name == super_arg.children.first
|
@@ -159,7 +159,7 @@ module RuboCop
|
|
159
159
|
end
|
160
160
|
|
161
161
|
def keyword_arg_same?(def_arg, super_arg)
|
162
|
-
return false unless def_arg.
|
162
|
+
return false unless def_arg.type?(:kwarg, :kwoptarg)
|
163
163
|
return false unless (pair_node = super_arg).pair_type?
|
164
164
|
return false unless (sym_node = pair_node.key).sym_type?
|
165
165
|
return false unless (lvar_node = pair_node.value).lvar_type?
|
@@ -7,12 +7,15 @@ module RuboCop
|
|
7
7
|
# The supported styles are:
|
8
8
|
#
|
9
9
|
# * `consistent_comma`: Requires a comma after the last argument,
|
10
|
-
# for all parenthesized method calls with arguments.
|
10
|
+
# for all parenthesized multi-line method calls with arguments.
|
11
11
|
# * `comma`: Requires a comma after the last argument, but only for
|
12
12
|
# parenthesized method calls where each argument is on its own line.
|
13
13
|
# * `no_comma`: Requires that there is no comma after the last
|
14
14
|
# argument.
|
15
15
|
#
|
16
|
+
# Regardless of style, trailing commas are not allowed in
|
17
|
+
# single-line method calls.
|
18
|
+
#
|
16
19
|
# @example EnforcedStyleForMultiline: consistent_comma
|
17
20
|
# # bad
|
18
21
|
# method(1, 2,)
|
data/lib/rubocop/cop/util.rb
CHANGED
@@ -74,9 +74,9 @@ module RuboCop
|
|
74
74
|
|
75
75
|
def args_begin(node)
|
76
76
|
loc = node.loc
|
77
|
-
selector = if node.
|
77
|
+
selector = if node.type?(:super, :yield)
|
78
78
|
loc.keyword
|
79
|
-
elsif node.
|
79
|
+
elsif node.type?(:def, :defs)
|
80
80
|
loc.name
|
81
81
|
else
|
82
82
|
loc.selector
|