rubocop 1.70.0 → 1.71.1
Sign up to get free protection for your applications and to get access to all the features.
- 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/empty_line_between_defs.rb +7 -5
- 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/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/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 +1 -1
- 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/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 +1 -1
- 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/yoda_expression.rb +1 -1
- data/lib/rubocop/cop/util.rb +2 -2
- data/lib/rubocop/cop/variable_force/variable.rb +1 -1
- 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
@@ -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
|
@@ -72,7 +72,7 @@ module RuboCop
|
|
72
72
|
parent = parent.parent if parent&.begin_type?
|
73
73
|
return false if parent.nil?
|
74
74
|
|
75
|
-
|
75
|
+
parent.type?(:if, :while, :until) && parent.modifier_form?
|
76
76
|
end
|
77
77
|
|
78
78
|
def capture_with_block!
|
@@ -100,7 +100,7 @@ module RuboCop
|
|
100
100
|
|
101
101
|
# Only block scope allows referencing outer scope variables.
|
102
102
|
node = scope.node
|
103
|
-
return nil unless node.
|
103
|
+
return nil unless node.any_block_type?
|
104
104
|
end
|
105
105
|
|
106
106
|
nil
|
@@ -113,14 +113,14 @@ module RuboCop
|
|
113
113
|
def accessible_variables
|
114
114
|
scope_stack.reverse_each.with_object([]) do |scope, variables|
|
115
115
|
variables.concat(scope.variables.values)
|
116
|
-
break variables unless scope.node.
|
116
|
+
break variables unless scope.node.any_block_type?
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
120
|
private
|
121
121
|
|
122
122
|
def mark_variable_as_captured_by_block_if_so(variable)
|
123
|
-
return unless current_scope.node.
|
123
|
+
return unless current_scope.node.any_block_type?
|
124
124
|
return if variable.scope == current_scope
|
125
125
|
|
126
126
|
variable.capture_with_block!
|
@@ -18,7 +18,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
18
18
|
description: ->(data) { "#{data.description}\n" },
|
19
19
|
safety: ->(data) { safety_object(data.safety_objects, data.cop) },
|
20
20
|
examples: ->(data) { examples(data.example_objects, data.cop) },
|
21
|
-
configuration: ->(data) { configurations(data.cop.department, data.
|
21
|
+
configuration: ->(data) { configurations(data.cop.department, data.cop, data.config) },
|
22
22
|
references: ->(data) { references(data.cop, data.see_objects) }
|
23
23
|
}.freeze
|
24
24
|
|
@@ -180,17 +180,17 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
180
180
|
content
|
181
181
|
end
|
182
182
|
|
183
|
-
def configurations(department,
|
184
|
-
return '' if pars.empty?
|
185
|
-
|
183
|
+
def configurations(department, cop, cop_config)
|
186
184
|
header = ['Name', 'Default value', 'Configurable values']
|
187
|
-
configs =
|
185
|
+
configs = cop_config
|
188
186
|
.each_key
|
189
187
|
.reject { |key| key.start_with?('Supported') }
|
190
188
|
.reject { |key| key.start_with?('AllowMultipleStyles') }
|
189
|
+
return '' if configs.empty?
|
190
|
+
|
191
191
|
content = configs.map do |name|
|
192
|
-
configurable = configurable_values(
|
193
|
-
default = format_table_value(
|
192
|
+
configurable = configurable_values(cop_config, name)
|
193
|
+
default = format_table_value(cop_config[name])
|
194
194
|
|
195
195
|
[configuration_name(department, name), default, configurable]
|
196
196
|
end
|
@@ -206,17 +206,17 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
206
206
|
end
|
207
207
|
|
208
208
|
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
|
209
|
-
def configurable_values(
|
209
|
+
def configurable_values(cop_config, name)
|
210
210
|
case name
|
211
211
|
when /^Enforced/
|
212
212
|
supported_style_name = RuboCop::Cop::Util.to_supported_styles(name)
|
213
|
-
format_table_value(
|
213
|
+
format_table_value(cop_config[supported_style_name])
|
214
214
|
when 'IndentationWidth'
|
215
215
|
'Integer'
|
216
216
|
when 'Database'
|
217
|
-
format_table_value(
|
217
|
+
format_table_value(cop_config['SupportedDatabases'])
|
218
218
|
else
|
219
|
-
case
|
219
|
+
case cop_config[name]
|
220
220
|
when String
|
221
221
|
'String'
|
222
222
|
when Integer
|
@@ -319,7 +319,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
319
319
|
AutoCorrect Description Enabled StyleGuide Reference Safe SafeAutoCorrect VersionAdded
|
320
320
|
VersionChanged
|
321
321
|
]
|
322
|
-
|
322
|
+
parameters = cop_config.reject { |k| non_display_keys.include? k }
|
323
323
|
description = 'No documentation'
|
324
324
|
example_objects = safety_objects = see_objects = []
|
325
325
|
cop_code(cop) do |code_object|
|
@@ -329,7 +329,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
329
329
|
see_objects = code_object.tags('see')
|
330
330
|
end
|
331
331
|
data = CopData.new(cop: cop, description: description, example_objects: example_objects,
|
332
|
-
safety_objects: safety_objects, see_objects: see_objects, config:
|
332
|
+
safety_objects: safety_objects, see_objects: see_objects, config: parameters)
|
333
333
|
cops_body(data)
|
334
334
|
end
|
335
335
|
|
@@ -88,10 +88,15 @@ module RuboCop
|
|
88
88
|
@cop_names ||= all_cops? ? all_cop_names : parsed_cop_names
|
89
89
|
end
|
90
90
|
|
91
|
+
# Returns an array of cops for this directive comment, without resolving departments
|
92
|
+
def raw_cop_names
|
93
|
+
@raw_cop_names ||= (cops || '').split(/,\s*/)
|
94
|
+
end
|
95
|
+
|
91
96
|
# Returns array of specified in this directive department names
|
92
97
|
# when all department disabled
|
93
98
|
def department_names
|
94
|
-
|
99
|
+
raw_cop_names.select { |cop| department?(cop) }
|
95
100
|
end
|
96
101
|
|
97
102
|
# Checks if directive departments include cop
|
@@ -101,11 +106,11 @@ module RuboCop
|
|
101
106
|
|
102
107
|
# Checks if cop department has already used in directive comment
|
103
108
|
def overridden_by_department?(cop)
|
104
|
-
in_directive_department?(cop) &&
|
109
|
+
in_directive_department?(cop) && raw_cop_names.include?(cop)
|
105
110
|
end
|
106
111
|
|
107
112
|
def directive_count
|
108
|
-
|
113
|
+
raw_cop_names.count
|
109
114
|
end
|
110
115
|
|
111
116
|
# Returns line number for directive
|
@@ -115,12 +120,8 @@ module RuboCop
|
|
115
120
|
|
116
121
|
private
|
117
122
|
|
118
|
-
def splitted_cops_string
|
119
|
-
(cops || '').split(/,\s*/)
|
120
|
-
end
|
121
|
-
|
122
123
|
def parsed_cop_names
|
123
|
-
cops =
|
124
|
+
cops = raw_cop_names.map do |name|
|
124
125
|
department?(name) ? cop_names_for_department(name) : name
|
125
126
|
end.flatten
|
126
127
|
cops - [LINT_SYNTAX_COP]
|
data/lib/rubocop/options.rb
CHANGED
@@ -579,7 +579,8 @@ module RuboCop
|
|
579
579
|
'when combined with --display-only-correctable.'],
|
580
580
|
show_cops: ['Shows the given cops, or all cops by',
|
581
581
|
'default, and their configurations for the',
|
582
|
-
'current directory.'
|
582
|
+
'current directory.',
|
583
|
+
'You can use `*` as a wildcard.'],
|
583
584
|
show_docs_url: ['Display url to documentation for the given',
|
584
585
|
'cops, or base url by default.'],
|
585
586
|
fail_fast: ['Inspect files in order of modification',
|