rubocop 1.88.2 → 1.90.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/config/default.yml +139 -16
- data/exe/rubocop +1 -1
- data/lib/rubocop/cached_data.rb +7 -2
- data/lib/rubocop/cli/command/execute_runner.rb +31 -0
- data/lib/rubocop/cli.rb +2 -4
- data/lib/rubocop/comment_config/directive_range.rb +32 -0
- data/lib/rubocop/comment_config/disable_next.rb +97 -0
- data/lib/rubocop/comment_config.rb +81 -49
- data/lib/rubocop/config.rb +76 -5
- data/lib/rubocop/config_loader.rb +1 -2
- data/lib/rubocop/config_loader_resolver.rb +1 -2
- data/lib/rubocop/config_validator.rb +39 -3
- data/lib/rubocop/cop/autocorrect_logic.rb +8 -0
- data/lib/rubocop/cop/badge.rb +8 -0
- data/lib/rubocop/cop/base.rb +40 -6
- data/lib/rubocop/cop/bundler.rb +19 -0
- data/lib/rubocop/cop/corrector.rb +4 -4
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +55 -5
- data/lib/rubocop/cop/gemspec.rb +22 -0
- data/lib/rubocop/cop/generator/registration_injector.rb +188 -0
- data/lib/rubocop/cop/generator.rb +4 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -2
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +1 -2
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -2
- data/lib/rubocop/cop/layout/class_structure.rb +108 -26
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -2
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +63 -5
- data/lib/rubocop/cop/layout/end_alignment.rb +1 -2
- data/lib/rubocop/cop/layout/extra_spacing.rb +105 -3
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +21 -1
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +3 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +27 -0
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -2
- data/lib/rubocop/cop/layout/line_length.rb +84 -12
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -2
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +12 -2
- data/lib/rubocop/cop/layout/redundant_line_break.rb +77 -0
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +7 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +31 -34
- data/lib/rubocop/cop/layout.rb +114 -0
- data/lib/rubocop/cop/lazy_loader.rb +41 -0
- data/lib/rubocop/cop/lint/ambiguous_assignment.rb +9 -7
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +71 -0
- data/lib/rubocop/cop/lint/ambiguous_range.rb +1 -2
- data/lib/rubocop/cop/lint/argument_mismatch.rb +89 -0
- data/lib/rubocop/cop/lint/constant_reassignment.rb +18 -21
- data/lib/rubocop/cop/lint/constant_resolution.rb +25 -0
- data/lib/rubocop/cop/lint/cop_directive_syntax.rb +84 -8
- data/lib/rubocop/cop/lint/deprecated_reference.rb +194 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +300 -22
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -2
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -2
- data/lib/rubocop/cop/lint/inherit_exception.rb +53 -7
- data/lib/rubocop/cop/lint/interpolation_check.rb +1 -2
- data/lib/rubocop/cop/lint/literal_as_condition.rb +8 -6
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +4 -7
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +25 -12
- data/lib/rubocop/cop/lint/missing_super.rb +31 -1
- data/lib/rubocop/cop/lint/name_typo.rb +270 -0
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +15 -4
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +98 -19
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +47 -7
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -2
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -2
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -2
- data/lib/rubocop/cop/lint/redundant_type_conversion.rb +1 -2
- data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -2
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +8 -2
- data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +2 -4
- data/lib/rubocop/cop/lint/super_argument_mismatch.rb +179 -0
- data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +1 -2
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +38 -3
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -2
- data/lib/rubocop/cop/lint/unused_private_method.rb +234 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -2
- data/lib/rubocop/cop/lint/useless_assignment.rb +1 -2
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +36 -0
- data/lib/rubocop/cop/lint/useless_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/useless_rescue.rb +1 -2
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +2 -0
- data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +61 -7
- data/lib/rubocop/cop/lint/void.rb +18 -8
- data/lib/rubocop/cop/lint.rb +178 -0
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -2
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics.rb +31 -0
- data/lib/rubocop/cop/migration/department_name.rb +1 -6
- data/lib/rubocop/cop/migration.rb +13 -0
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +23 -5
- data/lib/rubocop/cop/mixin/comments_help.rb +1 -2
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -2
- data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +13 -5
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +23 -6
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -2
- data/lib/rubocop/cop/mixin/hash_subset.rb +1 -2
- data/lib/rubocop/cop/mixin/indexed_method_arity.rb +94 -0
- data/lib/rubocop/cop/mixin/line_length_help.rb +7 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +51 -6
- data/lib/rubocop/cop/mixin/project_index_help.rb +140 -0
- data/lib/rubocop/cop/mixin/range_help.rb +1 -2
- data/lib/rubocop/cop/mixin/reparsed_equivalence.rb +171 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +46 -4
- data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
- data/lib/rubocop/cop/mixin.rb +82 -77
- data/lib/rubocop/cop/naming/accessor_method_name.rb +24 -0
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +16 -0
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -4
- data/lib/rubocop/cop/naming/method_name.rb +5 -2
- data/lib/rubocop/cop/naming/predicate_prefix.rb +33 -5
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -2
- data/lib/rubocop/cop/naming/variable_number.rb +3 -1
- data/lib/rubocop/cop/naming.rb +31 -0
- data/lib/rubocop/cop/offense.rb +37 -10
- data/lib/rubocop/cop/registry.rb +180 -46
- data/lib/rubocop/cop/security.rb +19 -0
- data/lib/rubocop/cop/style/accessor_grouping.rb +1 -2
- data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -6
- data/lib/rubocop/cop/style/array_first_last.rb +1 -2
- data/lib/rubocop/cop/style/array_intersect.rb +24 -5
- data/lib/rubocop/cop/style/bitwise_predicate.rb +1 -2
- data/lib/rubocop/cop/style/block_delimiters.rb +3 -6
- data/lib/rubocop/cop/style/case_like_if.rb +2 -4
- data/lib/rubocop/cop/style/class_and_module_children.rb +79 -20
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -2
- data/lib/rubocop/cop/style/concat_array_literals.rb +1 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +23 -2
- data/lib/rubocop/cop/style/constant_visibility.rb +1 -2
- data/lib/rubocop/cop/style/copyright.rb +5 -1
- data/lib/rubocop/cop/style/directive_scope.rb +171 -0
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +76 -12
- data/lib/rubocop/cop/style/documentation.rb +47 -0
- data/lib/rubocop/cop/style/empty_case_condition.rb +1 -2
- data/lib/rubocop/cop/style/endless_method.rb +0 -10
- data/lib/rubocop/cop/style/format_string_token.rb +1 -2
- data/lib/rubocop/cop/style/guard_clause.rb +1 -2
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +2 -4
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -2
- data/lib/rubocop/cop/style/if_unless_modifier.rb +8 -50
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -2
- data/lib/rubocop/cop/style/lambda.rb +4 -0
- data/lib/rubocop/cop/style/map_to_hash.rb +1 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +22 -7
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +1 -2
- data/lib/rubocop/cop/style/missing_respond_to_missing.rb +30 -0
- data/lib/rubocop/cop/style/multiline_if_then.rb +7 -1
- data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -2
- data/lib/rubocop/cop/style/multiple_comparison.rb +33 -22
- data/lib/rubocop/cop/style/mutable_constant.rb +5 -0
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -2
- data/lib/rubocop/cop/style/negative_array_index.rb +3 -3
- data/lib/rubocop/cop/style/nil_comparison.rb +1 -2
- data/lib/rubocop/cop/style/operator_method_call.rb +1 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +1 -2
- data/lib/rubocop/cop/style/redundant_assignment.rb +12 -2
- data/lib/rubocop/cop/style/redundant_condition.rb +2 -4
- data/lib/rubocop/cop/style/redundant_constant_base.rb +37 -1
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +1 -2
- data/lib/rubocop/cop/style/redundant_each.rb +1 -2
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +9 -0
- data/lib/rubocop/cop/style/redundant_format.rb +14 -7
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +78 -135
- data/lib/rubocop/cop/style/redundant_parentheses.rb +65 -67
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +1 -2
- data/lib/rubocop/cop/style/redundant_return.rb +1 -2
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -2
- data/lib/rubocop/cop/style/redundant_string_escape.rb +1 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -2
- data/lib/rubocop/cop/style/safe_navigation.rb +11 -2
- data/lib/rubocop/cop/style/sample.rb +11 -6
- data/lib/rubocop/cop/style/select_by_regexp.rb +1 -2
- data/lib/rubocop/cop/style/semicolon.rb +2 -4
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +21 -5
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/static_class.rb +21 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +18 -5
- data/lib/rubocop/cop/style/super_arguments.rb +1 -2
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -2
- data/lib/rubocop/cop/style/time_now.rb +41 -0
- data/lib/rubocop/cop/style/yoda_condition.rb +1 -2
- data/lib/rubocop/cop/style.rb +312 -0
- data/lib/rubocop/cop/team.rb +59 -6
- data/lib/rubocop/cop/util.rb +2 -4
- data/lib/rubocop/cop/variable_force/variable.rb +19 -8
- data/lib/rubocop/cops_documentation_generator.rb +4 -7
- data/lib/rubocop/directive_comment.rb +39 -3
- data/lib/rubocop/feature_loader.rb +0 -6
- data/lib/rubocop/formatter/emacs_style_formatter.rb +3 -1
- data/lib/rubocop/formatter/html_formatter.rb +2 -4
- data/lib/rubocop/formatter/json_formatter.rb +11 -6
- data/lib/rubocop/formatter/junit_formatter.rb +26 -9
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -2
- data/lib/rubocop/formatter/simple_text_formatter.rb +4 -3
- data/lib/rubocop/formatter/tap_formatter.rb +3 -1
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -2
- data/lib/rubocop/lockfile.rb +18 -0
- data/lib/rubocop/lsp/diagnostic.rb +4 -8
- data/lib/rubocop/lsp/routes.rb +89 -22
- data/lib/rubocop/lsp/runtime.rb +4 -0
- data/lib/rubocop/lsp/server.rb +4 -0
- data/lib/rubocop/lsp/stdin_runner.rb +20 -0
- data/lib/rubocop/mcp/server.rb +1 -2
- data/lib/rubocop/options.rb +10 -6
- data/lib/rubocop/path_util.rb +1 -2
- data/lib/rubocop/pending_cops_reporter.rb +1 -6
- data/lib/rubocop/plugin/configuration_integrator.rb +6 -3
- data/lib/rubocop/project_index_loader.rb +16 -0
- data/lib/rubocop/rake_task.rb +1 -2
- data/lib/rubocop/result_cache.rb +6 -4
- data/lib/rubocop/rspec/expect_offense.rb +2 -4
- data/lib/rubocop/rspec/shared_contexts.rb +16 -2
- data/lib/rubocop/runner.rb +71 -43
- data/lib/rubocop/server/cache.rb +11 -7
- data/lib/rubocop/server/cli.rb +2 -4
- data/lib/rubocop/server.rb +14 -0
- data/lib/rubocop/target_finder.rb +4 -1
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/todo_audit.rb +111 -0
- data/lib/rubocop/version.rb +3 -5
- data/lib/rubocop.rb +20 -626
- data/lib/ruby_lsp/rubocop/addon.rb +1 -2
- metadata +28 -5
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
|
4
4
|
module Cop
|
|
5
5
|
# Common functionality for checking whether an AST node/token is aligned
|
|
6
6
|
# with something on a preceding or following line
|
|
7
|
-
# rubocop:disable Metrics/ModuleLength
|
|
7
|
+
# rubocop:disable-next Metrics/ModuleLength
|
|
8
8
|
module PrecedingFollowingAlignment
|
|
9
9
|
# Tokens that end with an `=`, as well as `<<`, that can be aligned together:
|
|
10
10
|
# `=`, `==`, `===`, `!=`, `<=`, `>=`, `<<` and operator assignment (`+=`, etc).
|
|
@@ -69,7 +69,15 @@ module RuboCop
|
|
|
69
69
|
line = processed_source.lines[lineno]
|
|
70
70
|
index = line =~ /\S/
|
|
71
71
|
next unless index
|
|
72
|
-
|
|
72
|
+
|
|
73
|
+
if indent
|
|
74
|
+
# When searching for the nearest line with the same indentation,
|
|
75
|
+
# more deeply indented lines are nested content of the current group
|
|
76
|
+
# and are skipped, but a less deeply indented line ends the enclosing block:
|
|
77
|
+
# an alignment anchor beyond it would be coincidental.
|
|
78
|
+
break if index < indent
|
|
79
|
+
next if index > indent
|
|
80
|
+
end
|
|
73
81
|
|
|
74
82
|
return yield(range, line, lineno + 1)
|
|
75
83
|
end
|
|
@@ -174,20 +182,58 @@ module RuboCop
|
|
|
174
182
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
175
183
|
# rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength
|
|
176
184
|
def relevant_assignment_lines(line_range)
|
|
185
|
+
relevant_lines(line_range) { |line_number| assignment_lines.include?(line_number) }
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def alignment_lines(line_number)
|
|
189
|
+
@alignment_lines_by_line ||= {}
|
|
190
|
+
return @alignment_lines_by_line[line_number] if @alignment_lines_by_line.key?(line_number)
|
|
191
|
+
|
|
192
|
+
lines = alignment_line_ranges(line_number).flatten.uniq.sort.freeze
|
|
193
|
+
|
|
194
|
+
lines.each { |line| @alignment_lines_by_line[line] = lines }
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def alignment_line_ranges(line_number)
|
|
198
|
+
last_line = processed_source.lines.length
|
|
199
|
+
|
|
200
|
+
[
|
|
201
|
+
relevant_lines(line_number.downto(1), definition_boundary_lines) do |line|
|
|
202
|
+
!aligned_comment_lines.include?(line)
|
|
203
|
+
end,
|
|
204
|
+
relevant_lines(line_number.upto(last_line), definition_boundary_lines) do |line|
|
|
205
|
+
!aligned_comment_lines.include?(line)
|
|
206
|
+
end
|
|
207
|
+
]
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def definition_boundary_lines
|
|
211
|
+
@definition_boundary_lines ||= begin
|
|
212
|
+
nodes = processed_source.ast&.each_node(:any_def) || []
|
|
213
|
+
|
|
214
|
+
nodes.each_with_object(Set.new) do |node, lines|
|
|
215
|
+
lines << node.first_line << node.last_line
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def relevant_lines(line_range, boundary_lines = [])
|
|
177
221
|
result = []
|
|
178
|
-
|
|
222
|
+
original_line = line_range.first
|
|
223
|
+
original_line_indent = processed_source.line_indentation(original_line)
|
|
179
224
|
relevant_line_indent_at_level = true
|
|
180
225
|
|
|
181
226
|
line_range.each do |line_number|
|
|
182
227
|
current_line_indent = processed_source.line_indentation(line_number)
|
|
183
228
|
blank_line = processed_source.lines[line_number - 1].blank?
|
|
184
229
|
|
|
185
|
-
if (
|
|
230
|
+
if (line_number != original_line && boundary_lines.include?(line_number)) ||
|
|
231
|
+
(current_line_indent < original_line_indent && !blank_line) ||
|
|
186
232
|
(relevant_line_indent_at_level && blank_line)
|
|
187
233
|
break
|
|
188
234
|
end
|
|
189
235
|
|
|
190
|
-
result << line_number if
|
|
236
|
+
result << line_number if yield(line_number) &&
|
|
191
237
|
current_line_indent == original_line_indent
|
|
192
238
|
|
|
193
239
|
unless blank_line
|
|
@@ -214,6 +260,5 @@ module RuboCop
|
|
|
214
260
|
asgn_tokens.reject { |t| eqls_to_ignore.include?(t.begin_pos) }
|
|
215
261
|
end
|
|
216
262
|
end
|
|
217
|
-
# rubocop:enable Metrics/ModuleLength
|
|
218
263
|
end
|
|
219
264
|
end
|
|
@@ -17,6 +17,14 @@ module RuboCop
|
|
|
17
17
|
# remains after stripping `file://` from a `file:///C:/...` URI.
|
|
18
18
|
WINDOWS_DRIVE_PREFIX = %r{\A/(?=[A-Za-z]:[/\\])}.freeze
|
|
19
19
|
|
|
20
|
+
class << self
|
|
21
|
+
# The signature is a property of the index, not of the cop, and computing
|
|
22
|
+
# it stats every indexed file, so all cops sharing an index share one
|
|
23
|
+
# computation. A single-entry cache (instead of a hash keyed by index)
|
|
24
|
+
# avoids retaining stale graphs in long-lived processes.
|
|
25
|
+
attr_accessor :cached_index_signature
|
|
26
|
+
end
|
|
27
|
+
|
|
20
28
|
def external_dependency_checksum
|
|
21
29
|
return nil unless project_index
|
|
22
30
|
|
|
@@ -27,7 +35,139 @@ module RuboCop
|
|
|
27
35
|
|
|
28
36
|
private
|
|
29
37
|
|
|
38
|
+
# Returns the definitions among `definitions` that live in a file other than the
|
|
39
|
+
# one being inspected, ordered by path and line. Definitions without a `file://`
|
|
40
|
+
# URI (e.g. Rubydex's built-in declarations) are ignored.
|
|
41
|
+
def definitions_in_other_files(definitions)
|
|
42
|
+
current = processed_source.file_path
|
|
43
|
+
|
|
44
|
+
definitions
|
|
45
|
+
.select { |definition| definition.location.uri.start_with?(FILE_URI_PREFIX) }
|
|
46
|
+
.reject { |definition| File.identical?(definition.location.to_file_path, current) }
|
|
47
|
+
.sort_by do |definition|
|
|
48
|
+
[definition.location.to_file_path,
|
|
49
|
+
definition.location.start_line]
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def prior_definition_in_other_file(definitions)
|
|
54
|
+
definitions_in_other_files(definitions).first
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Resolves a constant node the way Ruby does: the first segment through
|
|
58
|
+
# the lexical nesting and every following segment inside the previous
|
|
59
|
+
# one. (Qualified names cannot be passed to `resolve_constant` as a
|
|
60
|
+
# whole, since it only applies the nesting to the full name. Later
|
|
61
|
+
# segments use `find_member` — the namespace and its ancestors — since
|
|
62
|
+
# `resolve_constant` cannot take an already-resolved namespace as the
|
|
63
|
+
# scope, only names as they appear in source.)
|
|
64
|
+
def resolve_constant_in_index(const_node)
|
|
65
|
+
segments = const_node.const_name.split('::')
|
|
66
|
+
nesting = const_node.absolute? ? [] : lexical_nesting_of(const_node)
|
|
67
|
+
|
|
68
|
+
declaration = project_index.resolve_constant(segments.first, nesting)
|
|
69
|
+
segments.drop(1).each do |segment|
|
|
70
|
+
return nil unless declaration.is_a?(Rubydex::Namespace)
|
|
71
|
+
|
|
72
|
+
declaration = declaration.find_member(segment)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
declaration
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# The lexical nesting the node's constants resolve through, outermost
|
|
79
|
+
# first. Only scopes whose *body* contains the node count: a class or
|
|
80
|
+
# module's identifier and superclass expression are evaluated before its
|
|
81
|
+
# scope exists, so ancestors reached through them are excluded.
|
|
82
|
+
def lexical_nesting_of(node)
|
|
83
|
+
nesting = []
|
|
84
|
+
child = node
|
|
85
|
+
|
|
86
|
+
node.each_ancestor do |ancestor|
|
|
87
|
+
if ancestor.type?(:class, :module) && child.equal?(ancestor.body)
|
|
88
|
+
nesting << ancestor.identifier.const_name
|
|
89
|
+
end
|
|
90
|
+
child = ancestor
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
nesting.reverse
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# The declaration of `declaration`'s singleton class, or nil when no
|
|
97
|
+
# singleton method is defined on it anywhere in the project.
|
|
98
|
+
def indexed_singleton_of(declaration)
|
|
99
|
+
project_index["#{declaration.name}::<#{declaration.name.split('::').last}>"]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# A namespace without any singleton method has no singleton-class
|
|
103
|
+
# declaration of its own, so the lookup starts from the first ancestor
|
|
104
|
+
# that has one; its `find_member` covers the rest of the chain.
|
|
105
|
+
def indexed_singleton_member(namespace, member_name)
|
|
106
|
+
namespace.ancestors.each do |ancestor|
|
|
107
|
+
singleton = indexed_singleton_of(ancestor)
|
|
108
|
+
return singleton.find_member(member_name) if singleton
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
nil
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Whether an ancestor of `scope` other than `scope` itself defines
|
|
115
|
+
# `member_name`.
|
|
116
|
+
def inherited_index_member?(scope, member_name)
|
|
117
|
+
scope.ancestors.any? do |ancestor|
|
|
118
|
+
ancestor.name != scope.name && ancestor.member(member_name)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Whether every link of the declaration's ancestry is resolved in the
|
|
123
|
+
# index: no definition of any ancestor has an unresolved superclass or
|
|
124
|
+
# mixin reference. Unresolved links silently vanish from `ancestors`,
|
|
125
|
+
# so this is the completeness check that must pass before reasoning
|
|
126
|
+
# about what an ancestry does or does not define. `extend` mixins only
|
|
127
|
+
# affect the singleton class; pass `ignore_extend: true` when reasoning
|
|
128
|
+
# about instance-side ancestry only.
|
|
129
|
+
def fully_resolved_index_ancestry?(declaration, ignore_extend: false)
|
|
130
|
+
declaration.ancestors.all? do |ancestor|
|
|
131
|
+
ancestor.definitions.all? do |definition|
|
|
132
|
+
resolved_superclass_reference?(definition) &&
|
|
133
|
+
resolved_mixin_references?(definition, ignore_extend: ignore_extend)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def resolved_superclass_reference?(definition)
|
|
139
|
+
return true unless definition.is_a?(Rubydex::ClassDefinition)
|
|
140
|
+
|
|
141
|
+
!definition.superclass.is_a?(Rubydex::UnresolvedConstantReference)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def resolved_mixin_references?(definition, ignore_extend: false)
|
|
145
|
+
return true unless definition.respond_to?(:mixins)
|
|
146
|
+
|
|
147
|
+
definition.mixins.none? do |mixin|
|
|
148
|
+
next false if ignore_extend && mixin.is_a?(Rubydex::Extend)
|
|
149
|
+
|
|
150
|
+
mixin.constant_reference.is_a?(Rubydex::UnresolvedConstantReference)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def same_file?(path, other)
|
|
155
|
+
return true if File.identical?(path, other)
|
|
156
|
+
|
|
157
|
+
normalized = [path, other].map { |p| File.expand_path(p).tr('\\', '/') }
|
|
158
|
+
normalized.uniq.one? || (Platform.windows? && normalized[0].casecmp?(normalized[1]))
|
|
159
|
+
end
|
|
160
|
+
|
|
30
161
|
def project_index_signature
|
|
162
|
+
index, signature = ProjectIndexHelp.cached_index_signature
|
|
163
|
+
return signature if index.equal?(project_index)
|
|
164
|
+
|
|
165
|
+
compute_project_index_signature.tap do |computed|
|
|
166
|
+
ProjectIndexHelp.cached_index_signature = [project_index, computed]
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def compute_project_index_signature
|
|
31
171
|
project_index.documents.filter_map do |doc|
|
|
32
172
|
uri = doc.uri
|
|
33
173
|
next if uri == BUILTIN_DOCUMENT_URI
|
|
@@ -116,14 +116,13 @@ module RuboCop
|
|
|
116
116
|
end
|
|
117
117
|
end
|
|
118
118
|
|
|
119
|
-
# rubocop:disable Metrics/ParameterLists
|
|
119
|
+
# rubocop:disable-next Metrics/ParameterLists
|
|
120
120
|
def final_pos(src, pos, increment, continuations, newlines, whitespace)
|
|
121
121
|
pos = move_pos(src, pos, increment, true, /[ \t]/)
|
|
122
122
|
pos = move_pos_str(src, pos, increment, continuations, "\\\n")
|
|
123
123
|
pos = move_pos(src, pos, increment, newlines, /\n/)
|
|
124
124
|
move_pos(src, pos, increment, whitespace, /\s/)
|
|
125
125
|
end
|
|
126
|
-
# rubocop:enable Metrics/ParameterLists
|
|
127
126
|
|
|
128
127
|
def move_pos(src, pos, step, condition, regexp)
|
|
129
128
|
offset = step == -1 ? -1 : 0
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
# Verifies that a candidate correction of the inspected source is a
|
|
6
|
+
# syntactic no-op: the corrected source must parse successfully and produce
|
|
7
|
+
# the same AST as the original.
|
|
8
|
+
#
|
|
9
|
+
# This turns "is this piece of syntax redundant?" questions into a
|
|
10
|
+
# parser-backed check instead of a hand-maintained enumeration of grammar
|
|
11
|
+
# rules: instead of modeling which constructs make (for example) a line
|
|
12
|
+
# continuation or a pair of parentheses significant, a cop can apply the
|
|
13
|
+
# correction and let the parser answer.
|
|
14
|
+
#
|
|
15
|
+
# Cops implement `#apply_reparse_correction(corrector, item)` and call
|
|
16
|
+
# `verified_by_reparse(items)`, which returns the items whose corrections
|
|
17
|
+
# are verified. Items sharing a reparse scope (the innermost method
|
|
18
|
+
# definition or class/module body, which parse standalone and cannot
|
|
19
|
+
# capture outer local variables) are verified together with a single
|
|
20
|
+
# reparse, falling back to per-item verification when the batch does not
|
|
21
|
+
# hold. Scope groups are keyed by node identity, since structurally
|
|
22
|
+
# identical definitions in different places must not share a group.
|
|
23
|
+
#
|
|
24
|
+
# Two hooks customize the comparison:
|
|
25
|
+
#
|
|
26
|
+
# * `normalize_reparsed_ast(node)` - loosen strict tree equality where a
|
|
27
|
+
# cop's correction is equivalence-preserving beyond parse identity (such
|
|
28
|
+
# laws must be justified per cop; the default compares trees as-is).
|
|
29
|
+
# * `preprocess_reparsed_source(source)` - rewrite both sides before
|
|
30
|
+
# parsing (e.g. to neutralize `__LINE__` when a correction legitimately
|
|
31
|
+
# shifts line numbers).
|
|
32
|
+
#
|
|
33
|
+
# `__FILE__` is handled by parsing with the original path, and fragments
|
|
34
|
+
# containing `__LINE__` are verified by reparsing both sides so that
|
|
35
|
+
# fragment line offsets cancel out.
|
|
36
|
+
#
|
|
37
|
+
# Note that comments are invisible to the AST, so corrections that may
|
|
38
|
+
# touch comment text must be guarded separately by the caller.
|
|
39
|
+
module ReparsedEquivalence
|
|
40
|
+
# Above this size, verification does not reparse. What happens to such
|
|
41
|
+
# items depends on `oversized`; in practice only machine-generated files
|
|
42
|
+
# come near the limit.
|
|
43
|
+
MAX_VERIFICATION_FRAGMENT_SIZE = 64 * 1024
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
# Returns the items whose corrections are verified. `oversized` controls
|
|
48
|
+
# items whose reparse scope exceeds `MAX_VERIFICATION_FRAGMENT_SIZE`:
|
|
49
|
+
# `:report` accepts them unverified (for cops whose offense logic stands
|
|
50
|
+
# on its own and uses verification as a safety gate), `:verify` reparses
|
|
51
|
+
# regardless of size (for cops where verification is the offense logic).
|
|
52
|
+
def verified_by_reparse(items, oversized: :report)
|
|
53
|
+
scope_groups(items).flat_map do |scope, group|
|
|
54
|
+
if (oversized == :report && verification_too_large?(scope)) ||
|
|
55
|
+
(group.size > 1 && corrections_verify?(scope, group))
|
|
56
|
+
group
|
|
57
|
+
else
|
|
58
|
+
group.select { |item| corrections_verify?(scope, [item]) }
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Whether the exact correction for `item` produces source that still
|
|
64
|
+
# parses, without requiring an equivalent AST. For corrections that
|
|
65
|
+
# intentionally change the tree.
|
|
66
|
+
def correction_parses?(item)
|
|
67
|
+
corrector = Corrector.new(processed_source)
|
|
68
|
+
apply_reparse_correction(corrector, item)
|
|
69
|
+
|
|
70
|
+
parse(corrector.process, processed_source.path).valid_syntax?
|
|
71
|
+
rescue ::Parser::ClobberingError
|
|
72
|
+
false
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Hook: loosen strict tree equality for corrections that are
|
|
76
|
+
# equivalence-preserving beyond parse identity.
|
|
77
|
+
def normalize_reparsed_ast(node)
|
|
78
|
+
node
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Hook: rewrite both sides before parsing.
|
|
82
|
+
def preprocess_reparsed_source(source)
|
|
83
|
+
source
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def scope_groups(items)
|
|
87
|
+
groups = {}.compare_by_identity
|
|
88
|
+
items.each { |item| (groups[reparse_scope(item_range(item))] ||= []) << item }
|
|
89
|
+
|
|
90
|
+
groups
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def item_range(item)
|
|
94
|
+
item.respond_to?(:source_range) ? item.source_range : item
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# The innermost scope that both contains `range` and parses standalone.
|
|
98
|
+
# Method definitions and class/module bodies neither capture outer local
|
|
99
|
+
# variables nor continue an outer expression; blocks and single
|
|
100
|
+
# statements do not qualify, since an outer local would reparse as a
|
|
101
|
+
# method call.
|
|
102
|
+
def reparse_scope(range)
|
|
103
|
+
node = processed_source.ast
|
|
104
|
+
scope = nil
|
|
105
|
+
|
|
106
|
+
while node.is_a?(::Parser::AST::Node)
|
|
107
|
+
scope = node if node.type?(:any_def, :class, :module, :sclass)
|
|
108
|
+
node = node.children.find do |child|
|
|
109
|
+
child.is_a?(::Parser::AST::Node) && child.source_range&.contains?(range)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
scope
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def verification_too_large?(scope)
|
|
117
|
+
(scope ? scope.source_range.size : processed_source.raw_source.size) >
|
|
118
|
+
MAX_VERIFICATION_FRAGMENT_SIZE
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def corrections_verify?(scope, items)
|
|
122
|
+
corrector = Corrector.new(processed_source)
|
|
123
|
+
items.each { |item| apply_reparse_correction(corrector, item) }
|
|
124
|
+
corrected = corrector.process
|
|
125
|
+
|
|
126
|
+
if scope
|
|
127
|
+
fragment = corrected_scope_fragment(scope, corrected)
|
|
128
|
+
parses_equivalently?(scope.source, scope, fragment)
|
|
129
|
+
else
|
|
130
|
+
parses_equivalently?(processed_source.raw_source, processed_source.ast, corrected)
|
|
131
|
+
end
|
|
132
|
+
rescue ::Parser::ClobberingError
|
|
133
|
+
false
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The corrections' edits are all contained within the scope, so the
|
|
137
|
+
# corrected fragment can be cut out of the corrected source by adjusting
|
|
138
|
+
# for the edits' length delta.
|
|
139
|
+
def corrected_scope_fragment(scope, corrected)
|
|
140
|
+
delta = corrected.length - processed_source.raw_source.length
|
|
141
|
+
scope_range = scope.source_range
|
|
142
|
+
|
|
143
|
+
corrected[scope_range.begin_pos...(scope_range.end_pos + delta)]
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Both sides are parsed with the original path so that `__FILE__`
|
|
147
|
+
# resolves identically. The original side is reparsed (instead of using
|
|
148
|
+
# the already parsed node) when preprocessing altered it or it contains
|
|
149
|
+
# `__LINE__`, whose values would otherwise differ from a fragment parsed
|
|
150
|
+
# at a different line offset.
|
|
151
|
+
def parses_equivalently?(original_source, original_ast, corrected_source)
|
|
152
|
+
original_normalized = normalized_original(original_source, original_ast)
|
|
153
|
+
return false unless original_normalized
|
|
154
|
+
|
|
155
|
+
rewritten = parse(preprocess_reparsed_source(corrected_source), processed_source.path)
|
|
156
|
+
rewritten.valid_syntax? && normalize_reparsed_ast(rewritten.ast) == original_normalized
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def normalized_original(original_source, original_ast)
|
|
160
|
+
original = preprocess_reparsed_source(original_source)
|
|
161
|
+
if original != original_source || original_source.include?('__LINE__')
|
|
162
|
+
reparsed = parse(original, processed_source.path).ast
|
|
163
|
+
reparsed && normalize_reparsed_ast(reparsed)
|
|
164
|
+
else
|
|
165
|
+
@normalized_originals ||= {}.compare_by_identity
|
|
166
|
+
@normalized_originals[original_ast] ||= normalize_reparsed_ast(original_ast)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
@@ -5,6 +5,7 @@ module RuboCop
|
|
|
5
5
|
# Common functionality for modifier cops.
|
|
6
6
|
module StatementModifier
|
|
7
7
|
include LineLengthHelp
|
|
8
|
+
include AllowedPattern
|
|
8
9
|
|
|
9
10
|
private
|
|
10
11
|
|
|
@@ -34,17 +35,58 @@ module RuboCop
|
|
|
34
35
|
condition.each_node.any?(&:lvasgn_type?)
|
|
35
36
|
end
|
|
36
37
|
|
|
38
|
+
# The rendered modifier form is compared against the bare maximum, not
|
|
39
|
+
# `acceptable_line_length?`: `Layout/LineLength`'s exemptions describe
|
|
40
|
+
# long lines the user tolerates, not permission to manufacture new ones,
|
|
41
|
+
# so collapsing to modifier form must never push a line past the maximum.
|
|
37
42
|
def modifier_fits_on_single_line?(node)
|
|
38
43
|
return true unless max_line_length
|
|
39
44
|
|
|
40
|
-
|
|
45
|
+
line_length(line_in_modifier_form(node)) <= max_line_length
|
|
41
46
|
end
|
|
42
47
|
|
|
43
|
-
def
|
|
48
|
+
def line_in_modifier_form(node)
|
|
44
49
|
keyword_element = node.loc.keyword
|
|
45
50
|
code_before = keyword_element.source_line[0...keyword_element.column]
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
|
|
52
|
+
"#{code_before}#{to_modifier_form(node)}#{code_after(node)}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Whether an existing line is acceptable to `Layout/LineLength`,
|
|
56
|
+
# exemptions (the cop disabled at that line, an allowed pattern, an
|
|
57
|
+
# allowed cop directive, an allowed URI) included. Only for judging
|
|
58
|
+
# lines already in the source as too long - see
|
|
59
|
+
# `modifier_fits_on_single_line?` for why the exemptions must not apply
|
|
60
|
+
# when deciding whether a modifier line may be created.
|
|
61
|
+
def acceptable_line_length?(line, line_number)
|
|
62
|
+
return true unless max_line_length
|
|
63
|
+
return true if line_length(line) <= max_line_length
|
|
64
|
+
return true unless line_length_enabled_at_line?(line_number)
|
|
65
|
+
return true if matches_allowed_pattern?(line)
|
|
66
|
+
|
|
67
|
+
if allow_cop_directives? && directive_on_source_line?(line_number - 1)
|
|
68
|
+
return line_length_without_directive(line) <= max_line_length
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
allowed_by_uri?(line)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def allowed_by_uri?(line)
|
|
75
|
+
return false unless allow_uri?
|
|
76
|
+
|
|
77
|
+
uri_range = find_excessive_range(line, :uri)
|
|
78
|
+
!uri_range.nil? && allowed_position?(line, uri_range)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def line_length_enabled_at_line?(line)
|
|
82
|
+
processed_source.comment_config.cop_enabled_at_line?('Layout/LineLength', line)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# `Layout/LineLength`'s allowed patterns, so a modifier line that the
|
|
86
|
+
# user has configured that cop to accept is not flagged for length here.
|
|
87
|
+
def allowed_patterns
|
|
88
|
+
line_length_config = config.for_cop('Layout/LineLength')
|
|
89
|
+
line_length_config['AllowedPatterns'] || line_length_config['IgnoredPatterns'] || []
|
|
48
90
|
end
|
|
49
91
|
|
|
50
92
|
def to_modifier_form(node)
|
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
|
4
4
|
module Cop
|
|
5
5
|
# Common methods shared by Style/TrailingCommaInArguments,
|
|
6
6
|
# Style/TrailingCommaInArrayLiteral and Style/TrailingCommaInHashLiteral
|
|
7
|
-
# rubocop:disable Metrics/ModuleLength
|
|
7
|
+
# rubocop:disable-next Metrics/ModuleLength
|
|
8
8
|
module TrailingComma
|
|
9
9
|
include ConfigurableEnforcedStyle
|
|
10
10
|
include RangeHelp
|
|
@@ -95,7 +95,7 @@ module RuboCop
|
|
|
95
95
|
node.multiline? && !allowed_multiline_argument?(node)
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
-
# rubocop:disable Metrics/AbcSize
|
|
98
|
+
# rubocop:disable-next Metrics/AbcSize
|
|
99
99
|
def method_name_and_arguments_on_same_line?(node)
|
|
100
100
|
return false if !node.call_type? || node.last_line != node.last_argument.last_line
|
|
101
101
|
return true if node.last_argument.hash_type? && node.last_argument.braces?
|
|
@@ -104,7 +104,6 @@ module RuboCop
|
|
|
104
104
|
|
|
105
105
|
line == node.last_argument.last_line
|
|
106
106
|
end
|
|
107
|
-
# rubocop:enable Metrics/AbcSize
|
|
108
107
|
|
|
109
108
|
# A single argument with the closing bracket on the same line as the end
|
|
110
109
|
# of the argument is not considered multiline, even if the argument
|
|
@@ -223,6 +222,5 @@ module RuboCop
|
|
|
223
222
|
false
|
|
224
223
|
end
|
|
225
224
|
end
|
|
226
|
-
# rubocop:enable Metrics/ModuleLength
|
|
227
225
|
end
|
|
228
226
|
end
|