rubocop 1.92.2 → 1.93.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +5 -1
- data/lib/rubocop/#directive_comment.rb# +224 -0
- data/lib/rubocop/cache_config.rb +3 -0
- data/lib/rubocop/cli.rb +5 -4
- data/lib/rubocop/config.rb +22 -0
- data/lib/rubocop/config_store.rb +1 -0
- data/lib/rubocop/config_validator.rb +45 -1
- data/lib/rubocop/cop/base.rb +5 -1
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +32 -18
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
- data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
- data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
- data/lib/rubocop/cop/layout/class_structure.rb +91 -13
- data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
- data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +26 -6
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +55 -16
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
- data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
- data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -2
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +14 -1
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +53 -15
- data/lib/rubocop/cop/lint/duplicate_require.rb +9 -2
- data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
- data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
- data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +53 -5
- data/lib/rubocop/cop/lint/number_conversion.rb +14 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
- data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
- data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +71 -1
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
- data/lib/rubocop/cop/lint/useless_times.rb +52 -12
- data/lib/rubocop/cop/lint/void.rb +20 -9
- data/lib/rubocop/cop/message_annotator.rb +6 -8
- data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +49 -18
- data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
- data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
- data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
- data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
- data/lib/rubocop/cop/mixin/range_help.rb +13 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +11 -3
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
- data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
- data/lib/rubocop/cop/style/and_or.rb +8 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
- data/lib/rubocop/cop/style/array_coercion.rb +1 -1
- data/lib/rubocop/cop/style/array_intersect.rb +15 -7
- data/lib/rubocop/cop/style/block_delimiters.rb +5 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
- data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
- data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
- data/lib/rubocop/cop/style/data_inheritance.rb +82 -9
- data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
- data/lib/rubocop/cop/style/dig_chain.rb +13 -2
- data/lib/rubocop/cop/style/each_with_object.rb +21 -5
- data/lib/rubocop/cop/style/empty_class_definition.rb +21 -4
- data/lib/rubocop/cop/style/empty_literal.rb +7 -0
- data/lib/rubocop/cop/style/empty_method.rb +27 -4
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
- data/lib/rubocop/cop/style/endless_method.rb +30 -10
- data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
- data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
- data/lib/rubocop/cop/style/file_read.rb +5 -1
- data/lib/rubocop/cop/style/file_write.rb +89 -20
- data/lib/rubocop/cop/style/for.rb +59 -4
- data/lib/rubocop/cop/style/guard_clause.rb +56 -11
- data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
- data/lib/rubocop/cop/style/hash_lookup_method.rb +12 -5
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
- data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
- data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
- data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
- data/lib/rubocop/cop/style/lambda.rb +13 -5
- data/lib/rubocop/cop/style/map_into_array.rb +4 -0
- data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
- data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
- data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +22 -17
- data/lib/rubocop/cop/style/non_nil_check.rb +14 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +37 -24
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
- data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
- data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
- data/lib/rubocop/cop/style/raise_args.rb +15 -0
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +18 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +117 -17
- data/lib/rubocop/cop/style/redundant_each.rb +26 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
- data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
- data/lib/rubocop/cop/style/redundant_interpolation.rb +37 -16
- data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +15 -2
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
- data/lib/rubocop/cop/style/redundant_sort.rb +6 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
- data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
- data/lib/rubocop/cop/style/select_by_range.rb +1 -6
- data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
- data/lib/rubocop/cop/style/self_assignment.rb +0 -4
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
- data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
- data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
- data/lib/rubocop/cop/style/struct_inheritance.rb +82 -16
- data/lib/rubocop/cop/style/swap_values.rb +10 -13
- data/lib/rubocop/cop/style/symbol_array.rb +1 -3
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
- data/lib/rubocop/cop/style/word_array.rb +1 -3
- data/lib/rubocop/cop/util.rb +2 -0
- data/lib/rubocop/cop/variable_force.rb +14 -1
- data/lib/rubocop/cops_documentation_generator.rb +1 -1
- data/lib/rubocop/formatter/junit_formatter.rb +13 -6
- data/lib/rubocop/result_cache.rb +9 -3
- data/lib/rubocop/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/shared_contexts.rb +11 -11
- data/lib/rubocop/runner.rb +21 -7
- data/lib/rubocop/server/cache.rb +24 -5
- data/lib/rubocop/target_ruby.rb +6 -8
- data/lib/rubocop/todo_audit.rb +61 -15
- data/lib/rubocop/version.rb +18 -8
- metadata +9 -5
|
@@ -49,9 +49,7 @@ module RuboCop
|
|
|
49
49
|
return unless (receiver, operation, number = useless_operation?(node))
|
|
50
50
|
return unless useless?(operation, number)
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
corrector.replace(node, receiver.source)
|
|
54
|
-
end
|
|
52
|
+
register_offense(node, receiver.source)
|
|
55
53
|
end
|
|
56
54
|
alias on_csend on_send
|
|
57
55
|
|
|
@@ -59,9 +57,7 @@ module RuboCop
|
|
|
59
57
|
return unless (variable, operation, number = useless_abbreviated_assignment?(node))
|
|
60
58
|
return unless useless?(operation, number)
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
corrector.replace(node, "#{variable.source} = #{variable.source}")
|
|
64
|
-
end
|
|
60
|
+
register_offense(node, "#{variable.source} = #{variable.source}")
|
|
65
61
|
end
|
|
66
62
|
|
|
67
63
|
private
|
|
@@ -73,6 +69,22 @@ module RuboCop
|
|
|
73
69
|
true if %i[* / **].include?(operation)
|
|
74
70
|
end
|
|
75
71
|
end
|
|
72
|
+
|
|
73
|
+
def register_offense(node, replacement)
|
|
74
|
+
add_offense(node) do |corrector|
|
|
75
|
+
next if unsafe_replacement?(node)
|
|
76
|
+
|
|
77
|
+
corrector.replace(node, replacement)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def unsafe_replacement?(node)
|
|
82
|
+
return false if node.single_line?
|
|
83
|
+
|
|
84
|
+
processed_source.tokens_within(node).any? do |token|
|
|
85
|
+
token.comment? || token.type == :tSTRING_END
|
|
86
|
+
end
|
|
87
|
+
end
|
|
76
88
|
end
|
|
77
89
|
end
|
|
78
90
|
end
|
|
@@ -38,9 +38,12 @@ module RuboCop
|
|
|
38
38
|
(block _ (args (arg $_)) ...)
|
|
39
39
|
PATTERN
|
|
40
40
|
|
|
41
|
-
# @!method
|
|
42
|
-
def_node_search :
|
|
43
|
-
|
|
41
|
+
# @!method argument_references(node, name)
|
|
42
|
+
def_node_search :argument_references, '$(lvar %)'
|
|
43
|
+
|
|
44
|
+
# @!method writes_argument?(node, name)
|
|
45
|
+
def_node_search :writes_argument?, <<~PATTERN
|
|
46
|
+
{(argument % ...) (lvasgn % ...) (match_var %) (match_with_lvasgn #named_capture?(%) _)}
|
|
44
47
|
PATTERN
|
|
45
48
|
|
|
46
49
|
def on_send(node)
|
|
@@ -74,9 +77,17 @@ module RuboCop
|
|
|
74
77
|
end
|
|
75
78
|
|
|
76
79
|
def remove_node(corrector, node)
|
|
80
|
+
return if code_after?(node)
|
|
81
|
+
|
|
77
82
|
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
|
|
78
83
|
end
|
|
79
84
|
|
|
85
|
+
def code_after?(node)
|
|
86
|
+
last_line = processed_source.buffer.source_line(node.last_line)
|
|
87
|
+
|
|
88
|
+
last_line[node.loc.last_column..].match?(/\A\s*[^\s#]/)
|
|
89
|
+
end
|
|
90
|
+
|
|
80
91
|
def autocorrect_block_pass(corrector, node, proc_name)
|
|
81
92
|
corrector.replace(node, proc_name)
|
|
82
93
|
end
|
|
@@ -85,10 +96,28 @@ module RuboCop
|
|
|
85
96
|
block_arg = block_arg(node)
|
|
86
97
|
return unless reducible_to_body?(node, block_arg)
|
|
87
98
|
|
|
88
|
-
source = node.body.source
|
|
89
|
-
|
|
99
|
+
source = block_arg ? substituted_body(node.body, block_arg) : node.body.source
|
|
100
|
+
return unless source
|
|
90
101
|
|
|
91
|
-
corrector.replace(node, fix_indentation(source, node
|
|
102
|
+
corrector.replace(node, fix_indentation(source, node))
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def substituted_body(body, name)
|
|
106
|
+
references = argument_references(body, name).sort_by(&:source_range)
|
|
107
|
+
return unless references.all? { |variable| replaceable_reference?(variable) }
|
|
108
|
+
|
|
109
|
+
source = body.source
|
|
110
|
+
references.reverse_each do |variable|
|
|
111
|
+
offset = variable.source_range.begin_pos - body.source_range.begin_pos
|
|
112
|
+
source[offset, variable.source_length] = '0'
|
|
113
|
+
end
|
|
114
|
+
source
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def replaceable_reference?(variable)
|
|
118
|
+
return false if variable.parent.pin_type? || variable.each_ancestor(:defined?).any?
|
|
119
|
+
|
|
120
|
+
!variable.parent.pair_type? || !variable.parent.value_omission?
|
|
92
121
|
end
|
|
93
122
|
|
|
94
123
|
def reducible_to_body?(node, block_arg)
|
|
@@ -101,8 +130,15 @@ module RuboCop
|
|
|
101
130
|
# substituted either, so reducing to the body would leave it referencing an
|
|
102
131
|
# undefined variable.
|
|
103
132
|
return false if node.arguments.one? && block_arg.nil?
|
|
133
|
+
return false if needs_enclosing_block?(node.body)
|
|
134
|
+
|
|
135
|
+
!block_arg || !writes_argument?(node.body, block_arg)
|
|
136
|
+
end
|
|
104
137
|
|
|
105
|
-
|
|
138
|
+
# A `rescue` or `ensure` clause is only valid inside the block, and the source of
|
|
139
|
+
# the body doesn't cover the body of a heredoc that ends it.
|
|
140
|
+
def needs_enclosing_block?(body)
|
|
141
|
+
body.type?(:rescue, :ensure) || body.each_node(:any_str, :xstr).any?(&:heredoc?)
|
|
106
142
|
end
|
|
107
143
|
|
|
108
144
|
def orphans_loop_control_keyword?(node)
|
|
@@ -112,14 +148,18 @@ module RuboCop
|
|
|
112
148
|
end
|
|
113
149
|
end
|
|
114
150
|
|
|
115
|
-
def
|
|
116
|
-
|
|
151
|
+
def named_capture?(regexp, name)
|
|
152
|
+
regexp.to_regexp.names.include?(name.to_s)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def fix_indentation(source, node)
|
|
156
|
+
shift = node.body.loc.column - node.loc.column
|
|
157
|
+
shift_range = begins_its_line?(node.body.source_range) ? "0,#{shift}" : shift
|
|
158
|
+
excess_indentation = /\A([ \t]{#{node.loc.column}})[ \t]{#{shift_range}}/
|
|
117
159
|
source_lines = source.split("\n")
|
|
118
160
|
|
|
119
161
|
source_lines[1..].each do |line|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
line[range] = ''
|
|
162
|
+
line.sub!(excess_indentation, '\1')
|
|
123
163
|
end
|
|
124
164
|
source_lines.join("\n")
|
|
125
165
|
end
|
|
@@ -75,6 +75,7 @@ module RuboCop
|
|
|
75
75
|
BINARY_OPERATORS = %i[* / % + - == === != < > <= >= <=>].freeze
|
|
76
76
|
UNARY_OPERATORS = %i[+@ -@ ~ !].freeze
|
|
77
77
|
OPERATORS = (BINARY_OPERATORS + UNARY_OPERATORS).freeze
|
|
78
|
+
NON_VALUE_ARGUMENT_TYPES = %i[splat block_pass forwarded_args forwarded_restarg].freeze
|
|
78
79
|
NONMUTATING_METHODS_WITH_BANG_VERSION = %i[capitalize chomp chop compact
|
|
79
80
|
delete_prefix delete_suffix downcase
|
|
80
81
|
encode flatten gsub lstrip merge next
|
|
@@ -153,13 +154,11 @@ module RuboCop
|
|
|
153
154
|
check_expression(case_node.else_branch) if case_node.else_branch
|
|
154
155
|
end
|
|
155
156
|
|
|
156
|
-
# rubocop:disable-next Metrics/
|
|
157
|
+
# rubocop:disable-next Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
157
158
|
def check_void_op(node, &block)
|
|
158
159
|
node = node.children.first while node&.begin_type?
|
|
159
160
|
return unless node&.call_type? && OPERATORS.include?(node.method_name)
|
|
160
|
-
|
|
161
|
-
return
|
|
162
|
-
end
|
|
161
|
+
return unless operation?(node)
|
|
163
162
|
|
|
164
163
|
return if block && yield(node)
|
|
165
164
|
|
|
@@ -169,6 +168,19 @@ module RuboCop
|
|
|
169
168
|
end
|
|
170
169
|
end
|
|
171
170
|
|
|
171
|
+
def operation?(node)
|
|
172
|
+
return UNARY_OPERATORS.include?(node.method_name) || !node.loc.dot if node.arguments.none?
|
|
173
|
+
return false if !node.arguments.one? || trailing_comma?(node)
|
|
174
|
+
|
|
175
|
+
argument = node.first_argument
|
|
176
|
+
|
|
177
|
+
argument.hash_type? ? argument.braces? : !argument.type?(*NON_VALUE_ARGUMENT_TYPES)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def trailing_comma?(node)
|
|
181
|
+
node.last_argument.source_range.end.join(node.source_range.end).source.include?(',')
|
|
182
|
+
end
|
|
183
|
+
|
|
172
184
|
def check_var(node)
|
|
173
185
|
return unless node.variable? || node.const_type?
|
|
174
186
|
|
|
@@ -193,6 +205,8 @@ module RuboCop
|
|
|
193
205
|
end
|
|
194
206
|
|
|
195
207
|
add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
|
|
208
|
+
next if node.each_descendant(:begin).any?
|
|
209
|
+
|
|
196
210
|
autocorrect_void_expression(corrector, node)
|
|
197
211
|
end
|
|
198
212
|
end
|
|
@@ -282,11 +296,8 @@ module RuboCop
|
|
|
282
296
|
end
|
|
283
297
|
|
|
284
298
|
def autocorrect_nonmutating_send(corrector, node, suggestion)
|
|
285
|
-
send_node =
|
|
286
|
-
|
|
287
|
-
else
|
|
288
|
-
node.send_node
|
|
289
|
-
end
|
|
299
|
+
send_node = node.call_type? ? node : node.send_node
|
|
300
|
+
|
|
290
301
|
corrector.replace(send_node.loc.selector, suggestion)
|
|
291
302
|
end
|
|
292
303
|
|
|
@@ -75,14 +75,12 @@ module RuboCop
|
|
|
75
75
|
url = cop_config['StyleGuide']
|
|
76
76
|
return nil if url.nil? || url.empty?
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
end
|
|
85
|
-
end
|
|
78
|
+
base_url = style_guide_base_url
|
|
79
|
+
return url if base_url.nil? || base_url.empty?
|
|
80
|
+
|
|
81
|
+
# Departments can have style guides of their own, so the same guideline
|
|
82
|
+
# can resolve against different bases.
|
|
83
|
+
self.class.style_guide_urls[[base_url, url]] ||= URI.join(base_url, url).to_s
|
|
86
84
|
end
|
|
87
85
|
|
|
88
86
|
# Returns the base style guide URL from AllCops or the specific department
|
|
@@ -21,9 +21,16 @@ module RuboCop
|
|
|
21
21
|
# @!method constant_definition?(node)
|
|
22
22
|
def_node_matcher :constant_definition?, '{class module}'
|
|
23
23
|
|
|
24
|
+
# @!method access_modifier_send?(node)
|
|
25
|
+
def_node_matcher :access_modifier_send?, '(send nil? {:private :protected :public})'
|
|
26
|
+
|
|
24
27
|
# @!method empty_line_required?(node)
|
|
25
|
-
def_node_matcher :empty_line_required?,
|
|
26
|
-
|
|
28
|
+
def_node_matcher :empty_line_required?, <<~PATTERN
|
|
29
|
+
{
|
|
30
|
+
any_def class module #access_modifier_send?
|
|
31
|
+
[{(if _ #access_modifier_send? nil?) (if _ nil? #access_modifier_send?)} modifier_form?]
|
|
32
|
+
}
|
|
33
|
+
PATTERN
|
|
27
34
|
|
|
28
35
|
def check(node, body, adjusted_first_line: nil)
|
|
29
36
|
return if valid_body_style?(body)
|
|
@@ -39,15 +46,15 @@ module RuboCop
|
|
|
39
46
|
when :empty_lines_special
|
|
40
47
|
check_empty_lines_special(body, first_line, last_line)
|
|
41
48
|
else
|
|
42
|
-
check_both(style, first_line, last_line)
|
|
49
|
+
check_both(style, body, first_line, last_line)
|
|
43
50
|
end
|
|
44
51
|
end
|
|
45
52
|
|
|
46
53
|
def check_empty_lines_except_namespace(body, first_line, last_line)
|
|
47
54
|
if namespace?(body, with_one_child: true)
|
|
48
|
-
check_both(:no_empty_lines, first_line, last_line)
|
|
55
|
+
check_both(:no_empty_lines, body, first_line, last_line)
|
|
49
56
|
else
|
|
50
|
-
check_both(:empty_lines, first_line, last_line)
|
|
57
|
+
check_both(:empty_lines, body, first_line, last_line)
|
|
51
58
|
end
|
|
52
59
|
end
|
|
53
60
|
|
|
@@ -55,37 +62,42 @@ module RuboCop
|
|
|
55
62
|
return unless body
|
|
56
63
|
|
|
57
64
|
if namespace?(body, with_one_child: true)
|
|
58
|
-
check_both(:no_empty_lines, first_line, last_line)
|
|
65
|
+
check_both(:no_empty_lines, body, first_line, last_line)
|
|
59
66
|
else
|
|
60
67
|
if first_child_requires_empty_line?(body)
|
|
61
|
-
check_beginning(:empty_lines, first_line)
|
|
68
|
+
check_beginning(:empty_lines, body, first_line)
|
|
62
69
|
else
|
|
63
|
-
|
|
70
|
+
check_no_empty_line_at_beginning(body, first_line)
|
|
64
71
|
check_deferred_empty_line(body)
|
|
65
72
|
end
|
|
66
|
-
check_ending(:empty_lines, last_line)
|
|
73
|
+
check_ending(:empty_lines, body, last_line)
|
|
67
74
|
end
|
|
68
75
|
end
|
|
69
76
|
|
|
70
|
-
def check_both(style, first_line, last_line)
|
|
77
|
+
def check_both(style, body, first_line, last_line)
|
|
71
78
|
case style
|
|
72
79
|
when :beginning_only
|
|
73
|
-
check_beginning(:empty_lines, first_line)
|
|
74
|
-
check_ending(:no_empty_lines, last_line)
|
|
80
|
+
check_beginning(:empty_lines, body, first_line)
|
|
81
|
+
check_ending(:no_empty_lines, body, last_line)
|
|
75
82
|
when :ending_only
|
|
76
|
-
check_beginning(:no_empty_lines, first_line)
|
|
77
|
-
check_ending(:empty_lines, last_line)
|
|
83
|
+
check_beginning(:no_empty_lines, body, first_line)
|
|
84
|
+
check_ending(:empty_lines, body, last_line)
|
|
78
85
|
else
|
|
79
|
-
check_beginning(style, first_line)
|
|
80
|
-
check_ending(style, last_line)
|
|
86
|
+
check_beginning(style, body, first_line)
|
|
87
|
+
check_ending(style, body, last_line)
|
|
81
88
|
end
|
|
82
89
|
end
|
|
83
90
|
|
|
84
|
-
def check_beginning(style, first_line)
|
|
91
|
+
def check_beginning(style, body, first_line)
|
|
92
|
+
return if style == :no_empty_lines && body && body.last_line <= first_line
|
|
93
|
+
return if style == :empty_lines && body&.first_line == first_line
|
|
94
|
+
|
|
85
95
|
check_source(style, first_line, 'beginning')
|
|
86
96
|
end
|
|
87
97
|
|
|
88
|
-
def check_ending(style, last_line)
|
|
98
|
+
def check_ending(style, body, last_line)
|
|
99
|
+
return if style == :empty_lines && body&.last_line == last_line
|
|
100
|
+
|
|
89
101
|
check_source(style, last_line - 2, 'end')
|
|
90
102
|
end
|
|
91
103
|
|
|
@@ -116,9 +128,24 @@ module RuboCop
|
|
|
116
128
|
LEADING_DOT_TYPES.include?(first_token&.type)
|
|
117
129
|
end
|
|
118
130
|
|
|
131
|
+
def check_no_empty_line_at_beginning(body, first_line)
|
|
132
|
+
return if deferred_empty_line_at_beginning?(body, first_line)
|
|
133
|
+
|
|
134
|
+
check_beginning(:no_empty_lines, body, first_line)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def deferred_empty_line_at_beginning?(body, first_line)
|
|
138
|
+
node = first_empty_line_required_child(body)
|
|
139
|
+
return false unless node
|
|
140
|
+
return false if node.first_line <= first_line
|
|
141
|
+
|
|
142
|
+
previous_line_ignoring_comments(node.first_line) <= first_line
|
|
143
|
+
end
|
|
144
|
+
|
|
119
145
|
def check_deferred_empty_line(body)
|
|
120
146
|
node = first_empty_line_required_child(body)
|
|
121
147
|
return unless node
|
|
148
|
+
return if same_line_as_previous_statement?(node)
|
|
122
149
|
|
|
123
150
|
line = previous_line_ignoring_comments(node.first_line)
|
|
124
151
|
return if processed_source[line].empty?
|
|
@@ -156,6 +183,10 @@ module RuboCop
|
|
|
156
183
|
end
|
|
157
184
|
end
|
|
158
185
|
|
|
186
|
+
def same_line_as_previous_statement?(node)
|
|
187
|
+
node.left_sibling.last_line == node.first_line
|
|
188
|
+
end
|
|
189
|
+
|
|
159
190
|
def previous_line_ignoring_comments(send_line)
|
|
160
191
|
(send_line - 2).downto(0) do |line|
|
|
161
192
|
return line unless comment_line?(processed_source[line])
|
|
@@ -5,6 +5,10 @@ module RuboCop
|
|
|
5
5
|
# Common functionality for checking whether a method body can become the
|
|
6
6
|
# body of an endless method definition without changing its meaning.
|
|
7
7
|
module EndlessMethodBodyHelp
|
|
8
|
+
STATEMENT_BODY_PARENT_TYPES = %i[
|
|
9
|
+
resbody when in_pattern while until for class module sclass any_block
|
|
10
|
+
].freeze
|
|
11
|
+
|
|
8
12
|
private
|
|
9
13
|
|
|
10
14
|
def endless_method_body?(body)
|
|
@@ -45,7 +49,7 @@ module RuboCop
|
|
|
45
49
|
|
|
46
50
|
def command_disallowed_in_endless_body?(body)
|
|
47
51
|
return true if body.assignment? && command_call?(assigned_value(body))
|
|
48
|
-
return true if
|
|
52
|
+
return true if body_disallowed_outside_statement?(body)
|
|
49
53
|
return true if negated_command_call?(body)
|
|
50
54
|
|
|
51
55
|
body.each_node(:any_block).any? do |block|
|
|
@@ -53,20 +57,53 @@ module RuboCop
|
|
|
53
57
|
end
|
|
54
58
|
end
|
|
55
59
|
|
|
56
|
-
def
|
|
57
|
-
|
|
60
|
+
def body_disallowed_outside_statement?(body)
|
|
61
|
+
return true if endless_method_with_command_body?(body)
|
|
62
|
+
|
|
63
|
+
command_body_outside_statement?(body) || do_end_block_body_in_command_argument?(body)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def endless_method_with_command_body?(node)
|
|
67
|
+
node.any_def_type? && node.endless? && command_call?(node.body)
|
|
58
68
|
end
|
|
59
69
|
|
|
60
|
-
|
|
61
|
-
def command_body_in_argument?(body)
|
|
70
|
+
def command_body_outside_statement?(body)
|
|
62
71
|
return false unless command_call?(body)
|
|
72
|
+
return false if command_position?(body.parent)
|
|
63
73
|
|
|
64
74
|
call = argument_receiving_call(body.parent)
|
|
65
|
-
return
|
|
75
|
+
return true if call.nil?
|
|
66
76
|
|
|
67
77
|
target_ruby_version < 4.0 || !call.arguments.one?
|
|
68
78
|
end
|
|
69
79
|
|
|
80
|
+
def command_position?(def_node)
|
|
81
|
+
parent = def_node.parent
|
|
82
|
+
return true if parent.nil? || parent.type?(:begin, :kwbegin, :rescue, :ensure)
|
|
83
|
+
return false if parent.masgn_type?
|
|
84
|
+
return true if parent.assignment?
|
|
85
|
+
return parent.last_argument.equal?(def_node) if setter_call?(parent)
|
|
86
|
+
|
|
87
|
+
statement_body?(def_node, parent)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def setter_call?(node)
|
|
91
|
+
node.call_type? && node.assignment_method?
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def statement_body?(def_node, parent)
|
|
95
|
+
return def_node.equal?(parent.body) if parent.type?(*STATEMENT_BODY_PARENT_TYPES)
|
|
96
|
+
|
|
97
|
+
case parent.type
|
|
98
|
+
when :if
|
|
99
|
+
!parent.ternary? && !def_node.equal?(parent.condition)
|
|
100
|
+
when :case, :case_match
|
|
101
|
+
def_node.equal?(parent.else_branch)
|
|
102
|
+
else
|
|
103
|
+
false
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
70
107
|
def argument_receiving_call(def_node)
|
|
71
108
|
call = def_node.parent
|
|
72
109
|
return unless call&.call_type?
|
|
@@ -90,8 +127,10 @@ module RuboCop
|
|
|
90
127
|
node
|
|
91
128
|
end
|
|
92
129
|
|
|
130
|
+
# `return foo` is a command too, even as `return(foo)`.
|
|
93
131
|
def command_call?(node)
|
|
94
132
|
node = node.send_node if node.any_block_type?
|
|
133
|
+
return node.arguments? if node.return_type?
|
|
95
134
|
return false unless node.type?(:call, :super, :yield)
|
|
96
135
|
|
|
97
136
|
node.arguments? && !node.parenthesized? && !node.operator_method? && !node.setter_method?
|
|
@@ -77,6 +77,7 @@ module RuboCop
|
|
|
77
77
|
add_offense(node.value, message: message) do |corrector|
|
|
78
78
|
corrector.replace(node, replacement)
|
|
79
79
|
|
|
80
|
+
next if node.value_omission?
|
|
80
81
|
next unless (def_node = def_node_that_require_parentheses(node))
|
|
81
82
|
|
|
82
83
|
last_argument = def_node.last_argument
|
|
@@ -33,9 +33,9 @@ module RuboCop
|
|
|
33
33
|
def_node_matcher :hash_receiver?, <<~PATTERN
|
|
34
34
|
{(hash ...)
|
|
35
35
|
(send _ {:to_h :to_hash :merge :merge! :update :invert :except :tally} ...)
|
|
36
|
-
(
|
|
37
|
-
|
|
38
|
-
(
|
|
36
|
+
(any_block (send _ {:group_by :to_h :tally :transform_keys :transform_keys!
|
|
37
|
+
:transform_values :transform_values!}) ...)
|
|
38
|
+
(any_block (send _ :each_with_object (hash)) ...)}
|
|
39
39
|
PATTERN
|
|
40
40
|
|
|
41
41
|
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler -- the patterns matched here all name their block parameters
|
|
@@ -16,6 +16,7 @@ module RuboCop
|
|
|
16
16
|
# to adjust the brace layout because this will result in invalid code.
|
|
17
17
|
return if last_line_heredoc?(node.children.last)
|
|
18
18
|
return if heredoc_opened_on_closing_brace_line?(node)
|
|
19
|
+
return if heredoc_method_call_to_be_repositioned?(node.children.last)
|
|
19
20
|
|
|
20
21
|
check(node)
|
|
21
22
|
end
|
|
@@ -145,6 +146,17 @@ module RuboCop
|
|
|
145
146
|
str.heredoc? && str.first_line == closing_brace_line
|
|
146
147
|
end
|
|
147
148
|
end
|
|
149
|
+
|
|
150
|
+
def heredoc_method_call_to_be_repositioned?(node)
|
|
151
|
+
return false unless config.cop_enabled?('Lint/HeredocMethodCallPosition')
|
|
152
|
+
|
|
153
|
+
node.each_node(:call).any? do |call_node|
|
|
154
|
+
receiver = call_node.receiver
|
|
155
|
+
next false unless receiver&.any_str_type? && receiver.heredoc?
|
|
156
|
+
|
|
157
|
+
receiver.loc.heredoc_end.end_pos < call_node.source_range.end_pos
|
|
158
|
+
end
|
|
159
|
+
end
|
|
148
160
|
end
|
|
149
161
|
end
|
|
150
162
|
end
|
|
@@ -77,6 +77,27 @@ module RuboCop
|
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
+
# Builds a double-quoted string literal for an interpolated element of a `%W` or `%I`
|
|
81
|
+
# array. Only the text around the interpolations is escaped, the code inside them is
|
|
82
|
+
# kept as written.
|
|
83
|
+
# @param [RuboCop::AST::Node] node a `dstr` or `dsym` node
|
|
84
|
+
# @return [String]
|
|
85
|
+
def build_interpolated_string_literal(node)
|
|
86
|
+
contents = node.children.map do |child|
|
|
87
|
+
if child.str_type?
|
|
88
|
+
child.value.inspect[1..-2]
|
|
89
|
+
elsif child.begin_type?
|
|
90
|
+
child.source
|
|
91
|
+
else
|
|
92
|
+
# A variable interpolated without braces, such as `#@foo`. The braces keep
|
|
93
|
+
# the text that follows from becoming part of the variable name.
|
|
94
|
+
"\#{#{child.source}}"
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
"\"#{contents.join}\""
|
|
99
|
+
end
|
|
100
|
+
|
|
80
101
|
# @param [RuboCop::AST::ArrayNode] node
|
|
81
102
|
# @param [Array<String>] elements
|
|
82
103
|
# @return [String]
|
|
@@ -97,7 +118,7 @@ module RuboCop
|
|
|
97
118
|
# @return [String]
|
|
98
119
|
def whitespace_between(node)
|
|
99
120
|
if node.children.length >= 2
|
|
100
|
-
node.children[0].source_range.end.join(node.children[1]
|
|
121
|
+
node.children[0].source_range.end.join(element_begin(node.children[1])).source
|
|
101
122
|
else
|
|
102
123
|
' '
|
|
103
124
|
end
|
|
@@ -109,7 +130,19 @@ module RuboCop
|
|
|
109
130
|
# @param [RuboCop::AST::ArrayNode] node
|
|
110
131
|
# @return [String]
|
|
111
132
|
def whitespace_leading(node)
|
|
112
|
-
node.loc.begin.end.join(node.children[0]
|
|
133
|
+
node.loc.begin.end.join(element_begin(node.children[0])).source
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The location of an element starting with a variable interpolated without
|
|
137
|
+
# braces, such as `#@foo`, does not include the `#`.
|
|
138
|
+
# @param [RuboCop::AST::Node] element
|
|
139
|
+
# @return [Parser::Source::Range]
|
|
140
|
+
def element_begin(element)
|
|
141
|
+
range = element.source_range.begin
|
|
142
|
+
return range unless element.type?(:dstr, :dsym)
|
|
143
|
+
return range if element.children.first.type?(:str, :begin)
|
|
144
|
+
|
|
145
|
+
range.adjust(begin_pos: -1, end_pos: -1)
|
|
113
146
|
end
|
|
114
147
|
|
|
115
148
|
# Provides trailing whitespace for building a bracketed array.
|
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
|
19
19
|
|
|
20
20
|
class << self
|
|
21
21
|
# The signature is a property of the index, not of the cop, and computing
|
|
22
|
-
# it
|
|
22
|
+
# it reads every indexed file, so all cops sharing an index share one
|
|
23
23
|
# computation. A single-entry cache (instead of a hash keyed by index)
|
|
24
24
|
# avoids retaining stale graphs in long-lived processes.
|
|
25
25
|
attr_accessor :cached_index_signature
|
|
@@ -188,14 +188,13 @@ module RuboCop
|
|
|
188
188
|
next if uri == BUILTIN_DOCUMENT_URI
|
|
189
189
|
|
|
190
190
|
path = uri.delete_prefix(FILE_URI_PREFIX).sub(WINDOWS_DRIVE_PREFIX, '')
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
[stat.mtime.to_f, stat.size]
|
|
191
|
+
digest = begin
|
|
192
|
+
Digest::SHA1.file(path).hexdigest
|
|
194
193
|
rescue StandardError
|
|
195
|
-
|
|
194
|
+
''
|
|
196
195
|
end
|
|
197
196
|
|
|
198
|
-
"#{path}:#{
|
|
197
|
+
"#{path}:#{digest}"
|
|
199
198
|
end.sort
|
|
200
199
|
end
|
|
201
200
|
end
|
|
@@ -90,6 +90,19 @@ module RuboCop
|
|
|
90
90
|
range.adjust(begin_pos: -range.column, end_pos: end_offset).intersect(buffer.source_range)
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
+
# Returns the range to remove a statement: its whole lines, including a trailing comment
|
|
94
|
+
# and the final newline, when no other code shares them, and otherwise the range itself
|
|
95
|
+
# with the spaces after it.
|
|
96
|
+
def range_for_statement_removal(range)
|
|
97
|
+
rest_of_line = range.source_buffer.source_line(range.last_line)[range.last_column..]
|
|
98
|
+
|
|
99
|
+
if begins_its_line?(range) && rest_of_line.match?(/\A\s*(#|\z)/)
|
|
100
|
+
range_by_whole_lines(range, include_final_newline: true)
|
|
101
|
+
else
|
|
102
|
+
range_with_surrounding_space(range, side: :right, newlines: false)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
93
106
|
def column_offset_between(base_range, range)
|
|
94
107
|
effective_column(base_range) - effective_column(range)
|
|
95
108
|
end
|
|
@@ -8,6 +8,7 @@ module RuboCop
|
|
|
8
8
|
include RangeHelp
|
|
9
9
|
|
|
10
10
|
MSG = 'Space found before %<token>s.'
|
|
11
|
+
SPACE_INSIDE_PARENS_STYLES = %w[space compact].freeze
|
|
11
12
|
|
|
12
13
|
def on_new_investigation
|
|
13
14
|
each_missing_space(processed_source.sorted_tokens) do |token, pos_before|
|
|
@@ -36,7 +37,10 @@ module RuboCop
|
|
|
36
37
|
end
|
|
37
38
|
|
|
38
39
|
def space_required_after?(token)
|
|
39
|
-
token.left_curly_brace?
|
|
40
|
+
return space_required_after_lcurly? if token.left_curly_brace?
|
|
41
|
+
return space_required_after_lparen? if token.left_parens?
|
|
42
|
+
|
|
43
|
+
false
|
|
40
44
|
end
|
|
41
45
|
|
|
42
46
|
def space_required_after_lcurly?
|
|
@@ -44,6 +48,12 @@ module RuboCop
|
|
|
44
48
|
style = cfg['EnforcedStyle'] || 'space'
|
|
45
49
|
style == 'space'
|
|
46
50
|
end
|
|
51
|
+
|
|
52
|
+
def space_required_after_lparen?
|
|
53
|
+
style = config.for_enabled_cop('Layout/SpaceInsideParens')['EnforcedStyle']
|
|
54
|
+
|
|
55
|
+
SPACE_INSIDE_PARENS_STYLES.include?(style)
|
|
56
|
+
end
|
|
47
57
|
end
|
|
48
58
|
end
|
|
49
59
|
end
|