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.
Files changed (191) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +5 -1
  4. data/lib/rubocop/#directive_comment.rb# +224 -0
  5. data/lib/rubocop/cache_config.rb +3 -0
  6. data/lib/rubocop/cli.rb +5 -4
  7. data/lib/rubocop/config.rb +22 -0
  8. data/lib/rubocop/config_store.rb +1 -0
  9. data/lib/rubocop/config_validator.rb +45 -1
  10. data/lib/rubocop/cop/base.rb +5 -1
  11. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  12. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +32 -18
  13. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  14. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  15. data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
  16. data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
  17. data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
  18. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  19. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  20. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  21. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  22. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  23. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  24. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  25. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +26 -6
  26. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +55 -16
  27. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  28. data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
  29. data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
  30. data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -2
  31. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  32. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  33. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  34. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  35. data/lib/rubocop/cop/layout/line_length.rb +14 -1
  36. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  37. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  38. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  39. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  40. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  41. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  42. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  43. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  44. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  45. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  46. data/lib/rubocop/cop/lint/duplicate_methods.rb +53 -15
  47. data/lib/rubocop/cop/lint/duplicate_require.rb +9 -2
  48. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  49. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
  50. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
  51. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  52. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  53. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +53 -5
  54. data/lib/rubocop/cop/lint/number_conversion.rb +14 -0
  55. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
  56. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
  57. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  58. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  59. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  60. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
  61. data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
  62. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  63. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  64. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  65. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  66. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  67. data/lib/rubocop/cop/lint/useless_assignment.rb +71 -1
  68. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
  69. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
  70. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
  71. data/lib/rubocop/cop/lint/useless_times.rb +52 -12
  72. data/lib/rubocop/cop/lint/void.rb +20 -9
  73. data/lib/rubocop/cop/message_annotator.rb +6 -8
  74. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  75. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +49 -18
  76. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  77. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  78. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  79. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  80. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  81. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  82. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  83. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  84. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  85. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
  86. data/lib/rubocop/cop/naming/block_forwarding.rb +11 -3
  87. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  88. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  89. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  90. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  91. data/lib/rubocop/cop/style/and_or.rb +8 -1
  92. data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
  93. data/lib/rubocop/cop/style/array_coercion.rb +1 -1
  94. data/lib/rubocop/cop/style/array_intersect.rb +15 -7
  95. data/lib/rubocop/cop/style/block_delimiters.rb +5 -1
  96. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
  97. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  98. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  99. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  100. data/lib/rubocop/cop/style/data_inheritance.rb +82 -9
  101. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  102. data/lib/rubocop/cop/style/dig_chain.rb +13 -2
  103. data/lib/rubocop/cop/style/each_with_object.rb +21 -5
  104. data/lib/rubocop/cop/style/empty_class_definition.rb +21 -4
  105. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  106. data/lib/rubocop/cop/style/empty_method.rb +27 -4
  107. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  108. data/lib/rubocop/cop/style/endless_method.rb +30 -10
  109. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  110. data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
  111. data/lib/rubocop/cop/style/file_read.rb +5 -1
  112. data/lib/rubocop/cop/style/file_write.rb +89 -20
  113. data/lib/rubocop/cop/style/for.rb +59 -4
  114. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  115. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  116. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  117. data/lib/rubocop/cop/style/hash_lookup_method.rb +12 -5
  118. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  119. data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
  120. data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
  121. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  122. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  123. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  124. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  125. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  126. data/lib/rubocop/cop/style/lambda.rb +13 -5
  127. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  128. data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
  129. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  130. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  131. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
  132. data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
  133. data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
  134. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
  135. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  136. data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
  137. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
  138. data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
  139. data/lib/rubocop/cop/style/nil_lambda.rb +22 -17
  140. data/lib/rubocop/cop/style/non_nil_check.rb +14 -1
  141. data/lib/rubocop/cop/style/operator_method_call.rb +37 -24
  142. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
  143. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  144. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  145. data/lib/rubocop/cop/style/raise_args.rb +15 -0
  146. data/lib/rubocop/cop/style/redundant_array_constructor.rb +18 -1
  147. data/lib/rubocop/cop/style/redundant_begin.rb +117 -17
  148. data/lib/rubocop/cop/style/redundant_each.rb +26 -1
  149. data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
  150. data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
  151. data/lib/rubocop/cop/style/redundant_interpolation.rb +37 -16
  152. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
  153. data/lib/rubocop/cop/style/redundant_percent_q.rb +15 -2
  154. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
  155. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  156. data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
  157. data/lib/rubocop/cop/style/redundant_sort.rb +6 -2
  158. data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
  159. data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
  160. data/lib/rubocop/cop/style/select_by_range.rb +1 -6
  161. data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
  162. data/lib/rubocop/cop/style/self_assignment.rb +0 -4
  163. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  164. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  165. data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
  166. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  167. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  168. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  169. data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
  170. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  171. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  172. data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
  173. data/lib/rubocop/cop/style/struct_inheritance.rb +82 -16
  174. data/lib/rubocop/cop/style/swap_values.rb +10 -13
  175. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  176. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  177. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
  178. data/lib/rubocop/cop/style/word_array.rb +1 -3
  179. data/lib/rubocop/cop/util.rb +2 -0
  180. data/lib/rubocop/cop/variable_force.rb +14 -1
  181. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  182. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  183. data/lib/rubocop/result_cache.rb +9 -3
  184. data/lib/rubocop/rspec/cop_helper.rb +8 -2
  185. data/lib/rubocop/rspec/shared_contexts.rb +11 -11
  186. data/lib/rubocop/runner.rb +21 -7
  187. data/lib/rubocop/server/cache.rb +24 -5
  188. data/lib/rubocop/target_ruby.rb +6 -8
  189. data/lib/rubocop/todo_audit.rb +61 -15
  190. data/lib/rubocop/version.rb +18 -8
  191. metadata +9 -5
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Checks node patterns for parts that can never match: node types that
7
+ # do not exist, sequences with a number of children that a node of that
8
+ # type never has, a node where a node of that type has a value like a
9
+ # symbol (and the other way around), and predicates that nothing responds
10
+ # to. Alternatives that are in a union more than once are reported too.
11
+ #
12
+ # Patterns that are built with interpolation are not checked.
13
+ #
14
+ # @example
15
+ # # bad
16
+ # def_node_matcher :my_matcher, <<~PATTERN
17
+ # (def _name _args)
18
+ # PATTERN
19
+ #
20
+ # # good
21
+ # def_node_matcher :my_matcher, <<~PATTERN
22
+ # (def _name _args _body)
23
+ # PATTERN
24
+ #
25
+ # # bad
26
+ # def_node_matcher :my_matcher, <<~PATTERN
27
+ # (send nil? "puts" ...)
28
+ # PATTERN
29
+ #
30
+ # # good
31
+ # def_node_matcher :my_matcher, <<~PATTERN
32
+ # (send nil? :puts ...)
33
+ # PATTERN
34
+ #
35
+ # # bad
36
+ # def_node_matcher :my_matcher, <<~PATTERN
37
+ # (send _ {:first :last :first})
38
+ # PATTERN
39
+ #
40
+ # # good
41
+ # def_node_matcher :my_matcher, <<~PATTERN
42
+ # (send _ {:first :last})
43
+ # PATTERN
44
+ #
45
+ class InvalidNodePattern < Base
46
+ RESTRICT_ON_SEND = %i[def_node_matcher def_node_search].freeze
47
+
48
+ def on_send(node)
49
+ pattern_node = node.arguments[1]
50
+ return unless pattern_node&.any_str_type? && (start = pattern_start(pattern_node))
51
+
52
+ problems(pattern_node).each do |problem|
53
+ add_offense(offense_range(start, problem.range), message: problem.message)
54
+ end
55
+ end
56
+ alias on_csend on_send
57
+
58
+ private
59
+
60
+ # @return [Parser::Source::Range, nil] where the pattern begins, if
61
+ # the positions in its value are the positions in the source
62
+ def pattern_start(pattern_node)
63
+ if pattern_node.heredoc?
64
+ pattern_node.loc.heredoc_body unless pattern_node.each_child_node(:begin).any?
65
+ elsif pattern_node.str_type? && pattern_node.loc?(:begin) && verbatim?(pattern_node)
66
+ pattern_node.loc.begin.end
67
+ end
68
+ end
69
+
70
+ def offense_range(start, range)
71
+ begin_pos = start.begin_pos + range.begin_pos
72
+
73
+ start.with(begin_pos: begin_pos, end_pos: begin_pos + range.size)
74
+ end
75
+
76
+ def verbatim?(pattern_node)
77
+ pattern_node.source == "#{pattern_node.loc.begin.source}#{pattern_node.value}" \
78
+ "#{pattern_node.loc.end.source}"
79
+ end
80
+
81
+ def problems(pattern_node)
82
+ value = pattern_node.heredoc? ? pattern_node.loc.heredoc_body.source : pattern_node.value
83
+
84
+ RuboCop::AST::NodePattern::Checker.new.check(value)
85
+ rescue RuboCop::AST::NodePattern::Invalid
86
+ []
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Checks for node searches that are used as a condition. A search that
7
+ # is defined with `def_node_search` and a name that does not end in `?`
8
+ # returns an enumerator when it is called without a block, and an
9
+ # enumerator is truthy even if nothing matches.
10
+ #
11
+ # Only the searches that are defined in the same file are known.
12
+ #
13
+ # @example
14
+ # def_node_search :assignments, '(lvasgn ...)'
15
+ #
16
+ # # bad
17
+ # return unless assignments(node)
18
+ #
19
+ # # bad
20
+ # assignments(node) && something
21
+ #
22
+ # # good
23
+ # return unless assignments(node).any?
24
+ #
25
+ # # good
26
+ # assignments(node) { |assignment| do_something(assignment) }
27
+ #
28
+ # # good
29
+ # def_node_search :assignments?, '(lvasgn ...)'
30
+ #
31
+ # return unless assignments?(node)
32
+ #
33
+ class NodeSearchAsCondition < Base
34
+ MSG = '`%<name>s` returns an enumerator, which is always truthy. ' \
35
+ 'Use `%<name>s(...).any?`, or define the search with a name that ends in `?`.'
36
+
37
+ # @!method node_search_name(node)
38
+ def_node_matcher :node_search_name, <<~PATTERN
39
+ (send nil? :def_node_search (sym $_) ...)
40
+ PATTERN
41
+
42
+ def on_new_investigation
43
+ @node_searches = nil
44
+ end
45
+
46
+ def on_send(node) # rubocop:disable InternalAffairs/OnSendWithoutOnCSend -- a search is called without a receiver
47
+ return unless node_searches.include?(node.method_name)
48
+ return if node.receiver || node.block_literal? || node.block_argument?
49
+ return unless condition?(node)
50
+
51
+ add_offense(node, message: format(MSG, name: node.method_name))
52
+ end
53
+
54
+ private
55
+
56
+ def node_searches
57
+ @node_searches ||= processed_source.ast.each_node(:send).filter_map do |node|
58
+ name = node_search_name(node)
59
+
60
+ name unless name.nil? || name.end_with?('?')
61
+ end.to_set
62
+ end
63
+
64
+ def condition?(node)
65
+ parent = node.parent
66
+ return false unless parent
67
+
68
+ if parent.type?(:and, :or)
69
+ parent.lhs.equal?(node)
70
+ elsif parent.type?(:if, :while, :until)
71
+ parent.condition.equal?(node)
72
+ else
73
+ parent.send_type? && parent.method?(:!)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Checks for cops that are declared as incompatible for autocorrection by
7
+ # both cops of the pair. One declaration is enough: when either cop has
8
+ # been applied, the corrections of the other are held back until the next
9
+ # pass.
10
+ #
11
+ # The declaration that is reported is the one in the cop whose name comes
12
+ # second in alphabetical order.
13
+ #
14
+ # @example
15
+ # # bad
16
+ # class Bar < Base
17
+ # def self.autocorrect_incompatible_with
18
+ # [Style::Foo]
19
+ # end
20
+ # end
21
+ #
22
+ # class Foo < Base
23
+ # def self.autocorrect_incompatible_with
24
+ # [Style::Bar]
25
+ # end
26
+ # end
27
+ #
28
+ # # good
29
+ # class Bar < Base
30
+ # def self.autocorrect_incompatible_with
31
+ # [Style::Foo]
32
+ # end
33
+ # end
34
+ #
35
+ # class Foo < Base
36
+ # end
37
+ #
38
+ class RedundantAutocorrectIncompatibleWith < Base
39
+ MSG = '`%<other>s` already declares that it is incompatible with this cop.'
40
+
41
+ def on_defs(node)
42
+ return unless declaration?(node) && (own = defining_cop_name(node))
43
+
44
+ node.body.each_child_node(:const) do |const|
45
+ other = qualified_name(const.const_name, own)
46
+ next unless own > other && declared_by?(other, own)
47
+
48
+ add_offense(const, message: format(MSG, other: other))
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def declaration?(node)
55
+ node.method?(:autocorrect_incompatible_with) && node.receiver.self_type? &&
56
+ node.body&.array_type?
57
+ end
58
+
59
+ # @return [String, nil] the name of the cop that the method is defined in
60
+ def defining_cop_name(node)
61
+ department, name = node.parent_module_name&.split('::')&.last(2)
62
+
63
+ "#{department}/#{name}" if name
64
+ end
65
+
66
+ # A cop of the same department can be given without the department.
67
+ def qualified_name(const_name, cop_name)
68
+ department, name = const_name.split('::').last(2)
69
+ return "#{department}/#{name}" if name
70
+
71
+ "#{cop_name.split('/').first}/#{department}"
72
+ end
73
+
74
+ def declared_by?(other, cop_name)
75
+ cop_class = Registry.global.find_by_cop_name(other)
76
+ return false unless cop_class
77
+
78
+ cop_class.autocorrect_incompatible_with.any? { |cop| cop.cop_name == cop_name }
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -7,6 +7,7 @@ require_relative 'internal_affairs/empty_line_between_expect_offense_and_correct
7
7
  require_relative 'internal_affairs/example_description'
8
8
  require_relative 'internal_affairs/example_heredoc_delimiter'
9
9
  require_relative 'internal_affairs/inherit_deprecated_cop_class'
10
+ require_relative 'internal_affairs/invalid_node_pattern'
10
11
  require_relative 'internal_affairs/itblock_handler'
11
12
  require_relative 'internal_affairs/lambda_or_proc'
12
13
  require_relative 'internal_affairs/location_exists'
@@ -18,6 +19,7 @@ require_relative 'internal_affairs/node_destructuring'
18
19
  require_relative 'internal_affairs/node_first_or_last_argument'
19
20
  require_relative 'internal_affairs/node_matcher_directive'
20
21
  require_relative 'internal_affairs/node_pattern_groups'
22
+ require_relative 'internal_affairs/node_search_as_condition'
21
23
  require_relative 'internal_affairs/node_type_group'
22
24
  require_relative 'internal_affairs/node_type_multiple_predicates'
23
25
  require_relative 'internal_affairs/node_type_predicate'
@@ -26,6 +28,7 @@ require_relative 'internal_affairs/offense_location_keyword'
26
28
  require_relative 'internal_affairs/on_send_without_on_csend'
27
29
  require_relative 'internal_affairs/operator_keyword'
28
30
  require_relative 'internal_affairs/processed_source_buffer_name'
31
+ require_relative 'internal_affairs/redundant_autocorrect_incompatible_with'
29
32
  require_relative 'internal_affairs/redundant_context_config_parameter'
30
33
  require_relative 'internal_affairs/redundant_described_class_as_subject'
31
34
  require_relative 'internal_affairs/redundant_expect_offense_arguments'
@@ -57,17 +57,21 @@ module RuboCop
57
57
 
58
58
  def check_body(body, node)
59
59
  modifiers = body.each_child_node(:send).select(&:bare_access_modifier?)
60
- end_range = node.loc.end
60
+ base_range = indentation_base_range(node)
61
61
 
62
62
  modifiers.each do |modifier|
63
63
  next if same_line?(node, modifier)
64
64
 
65
- check_modifier(modifier, end_range)
65
+ check_modifier(modifier, base_range)
66
66
  end
67
67
  end
68
68
 
69
- def check_modifier(send_node, end_range)
70
- offset = column_offset_between(send_node.source_range, end_range)
69
+ def indentation_base_range(node)
70
+ node.any_block_type? ? node.loc.end : node.loc.keyword
71
+ end
72
+
73
+ def check_modifier(send_node, base_range)
74
+ offset = column_offset_between(send_node.source_range, base_range)
71
75
 
72
76
  @column_delta = expected_indent_offset - offset
73
77
  if @column_delta.zero?
@@ -30,6 +30,19 @@ module RuboCop
30
30
 
31
31
  MSG = 'Expression at %<line>d, %<column>d should be on its own line.'
32
32
 
33
+ # Moving the closing brace below a heredoc leaves behind text that another cop
34
+ # inserts right after the brace in the same pass.
35
+ def self.autocorrect_incompatible_with
36
+ [
37
+ Lint::AmbiguousBlockAssociation,
38
+ Style::MethodCallWithArgsParentheses,
39
+ Style::NestedParenthesizedCalls,
40
+ Style::TrailingCommaInArguments,
41
+ Style::TrailingCommaInArrayLiteral,
42
+ Style::TrailingCommaInHashLiteral
43
+ ]
44
+ end
45
+
33
46
  def on_block(node)
34
47
  return if node.single_line?
35
48
 
@@ -49,13 +62,12 @@ module RuboCop
49
62
 
50
63
  def register_offense(node, offense_range)
51
64
  add_offense(node.loc.end, message: message(node)) do |corrector|
52
- replacement = "\n#{offense_range.source.lstrip}"
65
+ next if overlaps_inner_heredoc_correction?(node)
53
66
 
54
- if (heredoc = last_heredoc_argument(node.body))
55
- corrector.remove(offense_range)
56
- corrector.insert_after(heredoc.loc.heredoc_end, replacement)
67
+ if (heredoc = heredoc_argument_after_block_end(node))
68
+ correct_with_heredoc(corrector, offense_range, heredoc)
57
69
  else
58
- corrector.replace(offense_range, replacement)
70
+ corrector.replace(offense_range, "\n#{offense_range.source.lstrip}")
59
71
  end
60
72
  end
61
73
  end
@@ -64,6 +76,25 @@ module RuboCop
64
76
  format(MSG, line: node.loc.end.line, column: node.loc.end.column + 1)
65
77
  end
66
78
 
79
+ def overlaps_inner_heredoc_correction?(node)
80
+ node.each_descendant(:any_block).any? do |block|
81
+ same_line?(block.loc.end, node.loc.end) && heredoc_correction?(block)
82
+ end
83
+ end
84
+
85
+ def heredoc_correction?(node)
86
+ return false if node.single_line?
87
+ return false if begins_its_line?(node.loc.end)
88
+
89
+ !heredoc_argument_after_block_end(node).nil?
90
+ end
91
+
92
+ def heredoc_argument_after_block_end(node)
93
+ return unless (heredoc = last_heredoc_argument(node.body))
94
+
95
+ heredoc if heredoc.loc.heredoc_end.begin_pos > node.loc.end.begin_pos
96
+ end
97
+
67
98
  def last_heredoc_argument(node)
68
99
  return unless node&.call_type?
69
100
  return unless (arguments = node&.arguments)
@@ -74,6 +105,21 @@ module RuboCop
74
105
  last_heredoc_argument(node.children.first)
75
106
  end
76
107
 
108
+ def correct_with_heredoc(corrector, offense_range, heredoc)
109
+ range = offense_range.with(end_pos: code_end_pos(offense_range))
110
+
111
+ corrector.remove(range)
112
+ corrector.insert_after(heredoc.loc.heredoc_end, "\n#{range.source.strip}")
113
+ end
114
+
115
+ def code_end_pos(range)
116
+ comment = processed_source.comment_at_line(range.last_line)
117
+ return range.source_buffer.line_range(range.last_line).end_pos if comment.nil?
118
+
119
+ code_range = range.with(end_pos: comment.source_range.begin_pos)
120
+ code_range.begin_pos + code_range.source.rstrip.length
121
+ end
122
+
77
123
  def offense_range(node)
78
124
  node.children.compact.last.source_range.end.join(node.loc.end)
79
125
  end
@@ -174,7 +174,7 @@ module RuboCop
174
174
  # end
175
175
  # end
176
176
  #
177
- class ClassStructure < Base
177
+ class ClassStructure < Base # rubocop:disable Metrics/ClassLength
178
178
  include VisibilityHelp
179
179
  include CommentsHelp
180
180
  extend AutoCorrector
@@ -191,9 +191,19 @@ module RuboCop
191
191
  # A moved element is inserted before the line break that precedes its anchor.
192
192
  # When the anchor follows a blank line, `Layout/EmptyLines`,
193
193
  # `Layout/EmptyLinesAroundAccessModifier` or `Layout/EmptyLinesAroundClassBody`
194
- # may remove that line break in the same pass.
194
+ # may remove that line break in the same pass. A moved element is removed with the
195
+ # line break before it, and `Style/AccessModifierDeclarations`,
196
+ # `Style/BisectedAttrAccessor` or `Style/RedundantInitialize` may remove the same
197
+ # element with the line break after it, joining the lines around it.
195
198
  def self.autocorrect_incompatible_with
196
- [Layout::EmptyLines, Layout::EmptyLinesAroundAccessModifier, Layout::EmptyLinesAroundClassBody]
199
+ [
200
+ Layout::EmptyLines,
201
+ Layout::EmptyLinesAroundAccessModifier,
202
+ Layout::EmptyLinesAroundClassBody,
203
+ Style::AccessModifierDeclarations,
204
+ Style::BisectedAttrAccessor,
205
+ Style::RedundantInitialize
206
+ ]
197
207
  end
198
208
 
199
209
  # Validates code style on class declaration.
@@ -204,14 +214,17 @@ module RuboCop
204
214
  # Consecutive elements of the same category are reported only once,
205
215
  # on the first element of the group.
206
216
  def on_class(class_node)
217
+ out_of_order = out_of_order_elements(class_node)
218
+ collapsing_nodes = collapsing_nodes(out_of_order.map(&:first))
219
+
207
220
  # Corrections are registered in reverse source order because an insertion at
208
221
  # a given position lands before any insertion already made there;
209
222
  # this keeps the source order of nodes moved before the same anchor.
210
- out_of_order_elements(class_node).reverse_each do |node, category, previous|
223
+ out_of_order.reverse_each do |node, category, previous|
211
224
  message = format(MSG, category: category, previous: previous)
212
225
 
213
226
  add_offense(node, message: message) do |corrector|
214
- autocorrect(corrector, node)
227
+ autocorrect(corrector, node, collapsing_nodes)
215
228
  end
216
229
  end
217
230
  end
@@ -234,24 +247,89 @@ module RuboCop
234
247
  out_of_order
235
248
  end
236
249
 
250
+ # The moved elements whose removal takes the blank lines above them. An offense disabled
251
+ # by a directive moves nothing. An element that is another's insertion anchor keeps its
252
+ # blanks, as the insertion would otherwise land inside the removal.
253
+ def collapsing_nodes(nodes)
254
+ moving = nodes.select do |node|
255
+ enabled_lines?(node.source_range) && moved_group(node).any?
256
+ end
257
+ moved = moving.flat_map { |node| moved_group(node) }.to_set
258
+ anchors = moving.map { |node| insertion_anchor(node) }
259
+
260
+ moved - anchors - separators(moved)
261
+ end
262
+
237
263
  # Autocorrect by moving the node, together with the contiguous group of
238
264
  # same-category elements that follows it, to its expected position.
239
- def autocorrect(corrector, node)
240
- return if dynamic_constant?(node)
241
-
242
- anchor = insertion_anchor(node)
243
- return unless anchor
265
+ def autocorrect(corrector, node, collapsing_nodes)
266
+ group = moved_group(node)
267
+ return if group.empty?
244
268
 
245
- anchor_range = source_range_with_comment(anchor)
269
+ anchor_range = source_range_with_comment(insertion_anchor(node))
246
270
  # Reversed for the same reason offenses are registered in reverse source order:
247
271
  # the last insertion at a position comes first.
248
- movable_group(node).reverse_each do |group_node|
272
+ group.reverse_each do |group_node|
249
273
  current_range = source_range_with_comment(group_node)
250
274
  corrector.insert_before(anchor_range, current_range.source)
251
- corrector.remove(current_range)
275
+ corrector.remove(vacated_range(group_node, current_range, collapsing_nodes))
252
276
  end
253
277
  end
254
278
 
279
+ # The node's movable group, or none when the node itself cannot be moved.
280
+ def moved_group(node)
281
+ return [] if dynamic_constant?(node) || !insertion_anchor(node)
282
+
283
+ movable_group(node)
284
+ end
285
+
286
+ # The node's own lines together with the blank lines above it, which would otherwise
287
+ # join whatever follows.
288
+ def vacated_range(node, range, collapsing_nodes)
289
+ return range unless collapsing_nodes.include?(node)
290
+
291
+ # The range starts on the line break that ends the line above the node.
292
+ line = range.first_line
293
+ line -= 1 while buffer.source_line(line).blank?
294
+ range.with(begin_pos: buffer.line_range(line).end_pos)
295
+ end
296
+
297
+ # The moved elements whose blank lines above them are all that would separate the code
298
+ # around their run: in each run that a remaining element or a comment directly follows,
299
+ # the first one with blank lines above it.
300
+ def separators(moved_nodes)
301
+ moved_runs(moved_nodes).filter_map do |run|
302
+ run.find { |node| blank_line_above?(node) } if directly_followed?(run.last)
303
+ end
304
+ end
305
+
306
+ # The moved elements in runs of siblings with only blank lines between them, since a
307
+ # comment between two of them stays behind.
308
+ def moved_runs(moved_nodes)
309
+ moved_nodes.sort_by { |node| node.source_range.begin_pos }.chunk_while do |first, second|
310
+ first.right_sibling.equal?(second) && blank_between?(first, second)
311
+ end
312
+ end
313
+
314
+ def blank_between?(first, second)
315
+ from = buffer.line_for_position(end_position_for(first)) + 1
316
+ to = buffer.line_for_position(begin_pos_with_comment(second) + 1) - 1
317
+ (from..to).all? { |line| buffer.source_line(line).blank? }
318
+ end
319
+
320
+ # Whether the line after the node holds a remaining element or a comment.
321
+ def directly_followed?(node)
322
+ line = buffer.line_for_position(end_position_for(node)) + 1
323
+ return false if buffer.source_line(line).blank?
324
+
325
+ !node.right_sibling.nil? || whole_line_comment_at_line?(line)
326
+ end
327
+
328
+ # Whether the line above the node, or above the comment attached to it, is blank.
329
+ def blank_line_above?(node)
330
+ buffer.source_line(source_range_with_comment(node).first_line).blank?
331
+ end
332
+
255
333
  # Classifies a node to match with something in the {expected_order}
256
334
  # @param node to be analysed
257
335
  # @return String when the node type is a `:block` then
@@ -65,11 +65,15 @@ module RuboCop
65
65
  private
66
66
 
67
67
  def autocorrect(corrector, node)
68
- if style == :start_of_line && node.parent&.send_type?
69
- AlignmentCorrector.align_end(corrector, processed_source, node, node.parent)
70
- else
71
- AlignmentCorrector.align_end(corrector, processed_source, node, node)
72
- end
68
+ align_to = style == :start_of_line ? outermost_def_modifier(node) : node
69
+
70
+ AlignmentCorrector.align_end(corrector, processed_source, node, align_to)
71
+ end
72
+
73
+ def outermost_def_modifier(node)
74
+ modifiers = node.each_ancestor(:send).take_while { |send| send.def_modifier.equal?(node) }
75
+
76
+ modifiers.last || node
73
77
  end
74
78
  end
75
79
  end
@@ -37,6 +37,8 @@ module RuboCop
37
37
 
38
38
  MSG = 'Align `%<else_range>s` with `%<base_range>s`.'
39
39
 
40
+ alias on_csend on_send
41
+
40
42
  def on_if(node, base = nil)
41
43
  return if ignored_node?(node)
42
44
  return unless node.else? && begins_its_line?(node.loc.else)
@@ -111,10 +111,10 @@ module RuboCop
111
111
  end
112
112
 
113
113
  def correct_style?(node)
114
- !node.if_branch&.guard_clause? ||
115
- next_line_rescue_or_ensure?(node) ||
116
- next_sibling_parent_empty_or_else?(node) ||
117
- next_sibling_empty_or_guard_clause?(node)
114
+ return true unless node.if_branch&.guard_clause?
115
+ return true unless node.parent&.type?(:begin, :kwbegin)
116
+
117
+ next_sibling_empty_or_guard_clause?(node)
118
118
  end
119
119
 
120
120
  def contains_guard_clause?(node)
@@ -140,20 +140,6 @@ module RuboCop
140
140
  DirectiveComment.new(comment).enabled? || simplecov_directive_comment?(comment)
141
141
  end
142
142
 
143
- def next_line_rescue_or_ensure?(node)
144
- parent = node.parent
145
- parent.nil? || parent.rescue_type? || parent.ensure_type?
146
- end
147
-
148
- def next_sibling_parent_empty_or_else?(node)
149
- next_sibling = node.right_sibling
150
- return true unless next_sibling.is_a?(AST::Node)
151
-
152
- parent = next_sibling.parent
153
-
154
- parent&.if_type? && parent.else?
155
- end
156
-
157
143
  def next_sibling_empty_or_guard_clause?(node)
158
144
  next_sibling = node.right_sibling
159
145
  return true if next_sibling.nil?
@@ -218,7 +204,7 @@ module RuboCop
218
204
  parent = node.parent
219
205
  return false unless parent
220
206
 
221
- parent.begin_type? && same_line?(node, node.right_sibling)
207
+ parent.type?(:begin, :kwbegin) && node.last_line == node.right_sibling.first_line
222
208
  end
223
209
 
224
210
  # SimpleCov excludes code from the coverage report by wrapping it in directive comments:
@@ -28,7 +28,6 @@ module RuboCop
28
28
  # end
29
29
  #
30
30
  class EmptyLinesAfterModuleInclusion < Base
31
- include RangeHelp
32
31
  extend AutoCorrector
33
32
 
34
33
  MSG = 'Add an empty line after module inclusion.'
@@ -37,24 +36,45 @@ module RuboCop
37
36
 
38
37
  RESTRICT_ON_SEND = MODULE_INCLUSION_METHODS
39
38
 
39
+ def on_new_investigation
40
+ @heredoc_end_lines = nil
41
+ end
42
+
40
43
  def on_send(node)
41
44
  return if node.receiver || node.arguments.empty?
42
45
  return if node.parent&.type?(:send, :any_block, :array)
43
46
 
44
- return if next_line_empty_or_enable_directive_comment?(node.last_line)
47
+ last_line = last_line_with_heredocs(node)
48
+ return if next_line_empty_or_enable_directive_comment?(last_line)
45
49
 
46
50
  next_line_node = next_line_node(node)
47
51
  return unless require_empty_line?(next_line_node)
48
52
 
49
- add_offense(node) { |corrector| autocorrect(corrector, node) }
53
+ add_offense(node) { |corrector| autocorrect(corrector, last_line) }
50
54
  end
51
55
 
52
56
  private
53
57
 
54
- def autocorrect(corrector, node)
55
- node_range = range_by_whole_lines(node.source_range)
58
+ def last_line_with_heredocs(node)
59
+ [node.last_line, heredoc_end_lines[node.last_line]].compact.max
60
+ end
61
+
62
+ # @return [Hash{Integer => Integer}] the last line of the heredocs
63
+ # that begin on a line
64
+ def heredoc_end_lines
65
+ @heredoc_end_lines ||=
66
+ processed_source.ast.each_node(:any_str).with_object({}) do |str, lines|
67
+ next unless str.heredoc?
68
+
69
+ end_line = str.loc.heredoc_end.line
70
+ lines[str.first_line] = [lines[str.first_line], end_line].compact.max
71
+ end
72
+ end
73
+
74
+ def autocorrect(corrector, last_line)
75
+ node_range = processed_source.buffer.line_range(last_line)
56
76
 
57
- next_line = node_range.last_line + 1
77
+ next_line = last_line + 1
58
78
  if enable_directive_comment?(next_line)
59
79
  node_range = processed_source.comment_at_line(next_line)
60
80
  end