rubocop 1.69.2 → 1.71.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -2
  4. data/config/default.yml +36 -2
  5. data/lib/rubocop/cli/command/execute_runner.rb +3 -3
  6. data/lib/rubocop/cli/command/show_cops.rb +24 -2
  7. data/lib/rubocop/comment_config.rb +1 -1
  8. data/lib/rubocop/config.rb +13 -4
  9. data/lib/rubocop/config_loader.rb +4 -0
  10. data/lib/rubocop/config_loader_resolver.rb +14 -3
  11. data/lib/rubocop/config_validator.rb +18 -8
  12. data/lib/rubocop/cop/base.rb +6 -0
  13. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  14. data/lib/rubocop/cop/internal_affairs/cop_enabled.rb +85 -0
  15. data/lib/rubocop/cop/internal_affairs/location_expression.rb +2 -1
  16. data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +3 -2
  17. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +4 -3
  18. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +90 -0
  19. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +2 -1
  20. data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +5 -4
  21. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  22. data/lib/rubocop/cop/layout/argument_alignment.rb +2 -8
  23. data/lib/rubocop/cop/layout/class_structure.rb +7 -7
  24. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
  25. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -1
  26. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  27. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -7
  28. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -7
  29. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -7
  30. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
  31. data/lib/rubocop/cop/layout/hash_alignment.rb +6 -4
  32. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -0
  33. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +7 -1
  34. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +1 -1
  35. data/lib/rubocop/cop/layout/line_length.rb +1 -0
  36. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
  37. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +25 -0
  38. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -0
  39. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
  40. data/lib/rubocop/cop/layout/space_after_method_name.rb +1 -1
  41. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -0
  42. data/lib/rubocop/cop/layout/space_around_operators.rb +3 -3
  43. data/lib/rubocop/cop/layout/trailing_whitespace.rb +5 -3
  44. data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +119 -0
  45. data/lib/rubocop/cop/lint/constant_reassignment.rb +152 -0
  46. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -1
  47. data/lib/rubocop/cop/lint/duplicate_set_element.rb +20 -7
  48. data/lib/rubocop/cop/lint/float_comparison.rb +5 -2
  49. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  50. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +23 -5
  51. data/lib/rubocop/cop/lint/mixed_case_range.rb +1 -1
  52. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -1
  53. data/lib/rubocop/cop/lint/nested_method_definition.rb +5 -1
  54. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +4 -3
  55. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +17 -30
  56. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -1
  57. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +1 -1
  58. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +2 -2
  59. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  60. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +7 -0
  61. data/lib/rubocop/cop/lint/shared_mutable_default.rb +65 -0
  62. data/lib/rubocop/cop/lint/syntax.rb +4 -1
  63. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +1 -4
  64. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  65. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +2 -1
  66. data/lib/rubocop/cop/lint/void.rb +3 -2
  67. data/lib/rubocop/cop/metrics/collection_literal_length.rb +7 -0
  68. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  69. data/lib/rubocop/cop/metrics/method_length.rb +8 -1
  70. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  71. data/lib/rubocop/cop/mixin/check_line_breakable.rb +7 -7
  72. data/lib/rubocop/cop/mixin/comments_help.rb +2 -0
  73. data/lib/rubocop/cop/mixin/dig_help.rb +1 -1
  74. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  75. data/lib/rubocop/cop/mixin/hash_subset.rb +170 -0
  76. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +26 -16
  77. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  78. data/lib/rubocop/cop/mixin/statement_modifier.rb +8 -3
  79. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  80. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -2
  81. data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
  82. data/lib/rubocop/cop/security/compound_hash.rb +1 -0
  83. data/lib/rubocop/cop/style/access_modifier_declarations.rb +32 -1
  84. data/lib/rubocop/cop/style/and_or.rb +1 -1
  85. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -4
  86. data/lib/rubocop/cop/style/array_first_last.rb +18 -2
  87. data/lib/rubocop/cop/style/block_delimiters.rb +6 -19
  88. data/lib/rubocop/cop/style/class_and_module_children.rb +5 -2
  89. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  90. data/lib/rubocop/cop/style/conditional_assignment.rb +3 -1
  91. data/lib/rubocop/cop/style/each_for_simple_loop.rb +4 -7
  92. data/lib/rubocop/cop/style/empty_else.rb +4 -2
  93. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  94. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  95. data/lib/rubocop/cop/style/exact_regexp_match.rb +3 -10
  96. data/lib/rubocop/cop/style/exponential_notation.rb +1 -1
  97. data/lib/rubocop/cop/style/fetch_env_var.rb +1 -1
  98. data/lib/rubocop/cop/style/float_division.rb +8 -4
  99. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  100. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -1
  101. data/lib/rubocop/cop/style/hash_except.rb +9 -148
  102. data/lib/rubocop/cop/style/hash_slice.rb +65 -0
  103. data/lib/rubocop/cop/style/hash_syntax.rb +5 -2
  104. data/lib/rubocop/cop/style/it_assignment.rb +36 -0
  105. data/lib/rubocop/cop/style/map_to_set.rb +3 -2
  106. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +11 -1
  107. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -0
  108. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -1
  109. data/lib/rubocop/cop/style/missing_else.rb +2 -0
  110. data/lib/rubocop/cop/style/multiple_comparison.rb +26 -20
  111. data/lib/rubocop/cop/style/mutable_constant.rb +2 -2
  112. data/lib/rubocop/cop/style/object_then.rb +13 -15
  113. data/lib/rubocop/cop/style/open_struct_use.rb +4 -4
  114. data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
  115. data/lib/rubocop/cop/style/raise_args.rb +6 -4
  116. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  117. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +2 -1
  118. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  119. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  120. data/lib/rubocop/cop/style/redundant_initialize.rb +12 -3
  121. data/lib/rubocop/cop/style/redundant_line_continuation.rb +34 -13
  122. data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -4
  123. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +3 -0
  124. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
  125. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  126. data/lib/rubocop/cop/style/redundant_self_assignment.rb +6 -5
  127. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  128. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +2 -1
  129. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  130. data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -2
  131. data/lib/rubocop/cop/style/single_line_methods.rb +2 -3
  132. data/lib/rubocop/cop/style/slicing_with_range.rb +40 -11
  133. data/lib/rubocop/cop/style/sole_nested_conditional.rb +1 -1
  134. data/lib/rubocop/cop/style/string_methods.rb +1 -1
  135. data/lib/rubocop/cop/style/super_arguments.rb +63 -15
  136. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -1
  137. data/lib/rubocop/cop/style/yoda_condition.rb +8 -4
  138. data/lib/rubocop/cop/style/yoda_expression.rb +1 -0
  139. data/lib/rubocop/cop/util.rb +9 -2
  140. data/lib/rubocop/cops_documentation_generator.rb +13 -13
  141. data/lib/rubocop/directive_comment.rb +9 -8
  142. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  143. data/lib/rubocop/lsp/diagnostic.rb +189 -0
  144. data/lib/rubocop/lsp/logger.rb +2 -2
  145. data/lib/rubocop/lsp/routes.rb +7 -23
  146. data/lib/rubocop/lsp/runtime.rb +15 -49
  147. data/lib/rubocop/lsp/stdin_runner.rb +83 -0
  148. data/lib/rubocop/options.rb +2 -1
  149. data/lib/rubocop/path_util.rb +11 -8
  150. data/lib/rubocop/result_cache.rb +13 -13
  151. data/lib/rubocop/rspec/expect_offense.rb +6 -2
  152. data/lib/rubocop/rspec/shared_contexts.rb +4 -1
  153. data/lib/rubocop/runner.rb +5 -6
  154. data/lib/rubocop/target_finder.rb +1 -0
  155. data/lib/rubocop/target_ruby.rb +15 -0
  156. data/lib/rubocop/version.rb +1 -1
  157. data/lib/rubocop.rb +6 -0
  158. data/lib/ruby_lsp/rubocop/addon.rb +78 -0
  159. data/lib/ruby_lsp/rubocop/wraps_built_in_lsp_runtime.rb +50 -0
  160. metadata +17 -8
@@ -43,7 +43,7 @@ module RuboCop
43
43
  # (Note: Passes may not happen exactly in this sequence.)
44
44
  module CheckLineBreakable
45
45
  def extract_breakable_node(node, max)
46
- if node.send_type?
46
+ if node.call_type?
47
47
  return if chained_to_heredoc?(node)
48
48
 
49
49
  args = process_args(node.arguments)
@@ -74,9 +74,9 @@ module RuboCop
74
74
  def extract_first_element_over_column_limit(node, elements, max)
75
75
  line = node.first_line
76
76
 
77
- # If a `send` node is not parenthesized, don't move the first element, because it
77
+ # If a `send` or `csend` node is not parenthesized, don't move the first element, because it
78
78
  # can result in changed behavior or a syntax error.
79
- if node.send_type? && !node.parenthesized? && !first_argument_is_heredoc?(node)
79
+ if node.call_type? && !node.parenthesized? && !first_argument_is_heredoc?(node)
80
80
  elements = elements.drop(1)
81
81
  end
82
82
 
@@ -98,10 +98,10 @@ module RuboCop
98
98
  end
99
99
 
100
100
  # @api private
101
- # If a send node contains a heredoc argument, splitting cannot happen
101
+ # If a `send` or `csend` node contains a heredoc argument, splitting cannot happen
102
102
  # after the heredoc or else it will cause a syntax error.
103
103
  def shift_elements_for_heredoc_arg(node, elements, index)
104
- return index unless node.send_type? || node.array_type?
104
+ return index unless node.call_type? || node.array_type?
105
105
 
106
106
  heredoc_index = elements.index { |arg| arg.respond_to?(:heredoc?) && arg.heredoc? }
107
107
  return index unless heredoc_index
@@ -156,7 +156,7 @@ module RuboCop
156
156
 
157
157
  if ancestor.hash_type? || ancestor.array_type?
158
158
  elements = ancestor.children
159
- elsif ancestor.send_type?
159
+ elsif ancestor.call_type?
160
160
  elements = process_args(ancestor.arguments)
161
161
  else
162
162
  next
@@ -176,7 +176,7 @@ module RuboCop
176
176
  return children_could_be_broken_up?(ancestor.children)
177
177
  end
178
178
 
179
- next unless ancestor.send_type?
179
+ next unless ancestor.call_type?
180
180
 
181
181
  args = process_args(ancestor.arguments)
182
182
  return children_could_be_broken_up?(args) if breakable_collection?(ancestor, args)
@@ -73,6 +73,8 @@ module RuboCop
73
73
  node.else_branch.loc.line
74
74
  elsif node.elsif?
75
75
  node.each_ancestor(:if).find(&:if?).loc.end.line
76
+ elsif node.if? && node.parent && parentheses?(node.parent)
77
+ node.parent.loc.end.line
76
78
  end
77
79
  elsif node.block_type? || node.numblock_type?
78
80
  node.loc.end.line
@@ -20,7 +20,7 @@ module RuboCop
20
20
  private
21
21
 
22
22
  def dig_chain_enabled?
23
- @config.for_cop('Style/DigChain')['Enabled']
23
+ @config.cop_enabled?('Style/DigChain')
24
24
  end
25
25
  end
26
26
  end
@@ -6,7 +6,7 @@ module RuboCop
6
6
  module FrozenStringLiteral
7
7
  module_function
8
8
 
9
- FROZEN_STRING_LITERAL = '# frozen_string_literal:'
9
+ FROZEN_STRING_LITERAL_REGEXP = /#\s*frozen[-_]?string[-_]?literal:/i.freeze
10
10
  FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'
11
11
  FROZEN_STRING_LITERAL_TYPES_RUBY27 = %i[str dstr].freeze
12
12
 
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Common functionality for Style/HashExcept and Style/HashSlice cops.
6
+ # It registers an offense on methods with blocks that are equivalent
7
+ # to Hash#except or Hash#slice.
8
+ module HashSubset
9
+ include RangeHelp
10
+ extend NodePattern::Macros
11
+
12
+ RESTRICT_ON_SEND = %i[reject select filter].freeze
13
+
14
+ SUBSET_METHODS = %i[== != eql? include?].freeze
15
+ ACTIVE_SUPPORT_SUBSET_METHODS = (SUBSET_METHODS + %i[in? exclude?]).freeze
16
+
17
+ MSG = 'Use `%<prefer>s` instead.'
18
+
19
+ # @!method block_with_first_arg_check?(node)
20
+ def_node_matcher :block_with_first_arg_check?, <<~PATTERN
21
+ (block
22
+ (call _ _)
23
+ (args
24
+ $(arg _key)
25
+ (arg _))
26
+ {
27
+ $(send
28
+ {(lvar _key) $_ _ | _ $_ (lvar _key)})
29
+ (send
30
+ $(send
31
+ {(lvar _key) $_ _ | _ $_ (lvar _key)}) :!)
32
+ })
33
+ PATTERN
34
+
35
+ def on_send(node)
36
+ offense_range, key_source = extract_offense(node)
37
+
38
+ return unless offense_range
39
+ return unless semantically_subset_method?(node)
40
+
41
+ preferred_method = "#{preferred_method_name}(#{key_source})"
42
+ add_offense(offense_range, message: format(MSG, prefer: preferred_method)) do |corrector|
43
+ corrector.replace(offense_range, preferred_method)
44
+ end
45
+ end
46
+ alias on_csend on_send
47
+
48
+ private
49
+
50
+ def semantically_subset_method?(node)
51
+ raise NotImplementedError
52
+ end
53
+
54
+ def preferred_method_name
55
+ raise NotImplementedError
56
+ end
57
+
58
+ def extract_offense(node)
59
+ block = node.parent
60
+ return unless extracts_hash_subset?(block)
61
+
62
+ except_key = except_key(block)
63
+ return if except_key.nil? || !safe_to_register_offense?(block, except_key)
64
+
65
+ [offense_range(node), except_key_source(except_key)]
66
+ end
67
+
68
+ def extracts_hash_subset?(block)
69
+ block_with_first_arg_check?(block) do |key_arg, send_node, method|
70
+ return false unless supported_subset_method?(method)
71
+
72
+ case method
73
+ when :include?, :exclude?
74
+ send_node.first_argument.source == key_arg.source
75
+ when :in?
76
+ send_node.receiver.source == key_arg.source
77
+ else
78
+ true
79
+ end
80
+ end
81
+ end
82
+
83
+ def supported_subset_method?(method)
84
+ if active_support_extensions_enabled?
85
+ ACTIVE_SUPPORT_SUBSET_METHODS.include?(method)
86
+ else
87
+ SUBSET_METHODS.include?(method)
88
+ end
89
+ end
90
+
91
+ def semantically_except_method?(node)
92
+ block = node.parent
93
+ body, negated = extract_body_if_negated(block.body)
94
+
95
+ if node.method?('reject')
96
+ body.method?('==') || body.method?('eql?') || included?(body, negated)
97
+ else
98
+ body.method?('!=') || not_included?(body, negated)
99
+ end
100
+ end
101
+
102
+ def semantically_slice_method?(node)
103
+ !semantically_except_method?(node)
104
+ end
105
+
106
+ def included?(body, negated)
107
+ if negated
108
+ body.method?('exclude?')
109
+ else
110
+ body.method?('include?') || body.method?('in?')
111
+ end
112
+ end
113
+
114
+ def not_included?(body, negated)
115
+ included?(body, !negated)
116
+ end
117
+
118
+ def safe_to_register_offense?(block, except_key)
119
+ body = block.body
120
+
121
+ if body.method?('==') || body.method?('!=')
122
+ except_key.sym_type? || except_key.str_type?
123
+ else
124
+ true
125
+ end
126
+ end
127
+
128
+ def extract_body_if_negated(body)
129
+ if body.method?('!')
130
+ [body.receiver, true]
131
+ else
132
+ [body, false]
133
+ end
134
+ end
135
+
136
+ def except_key_source(key)
137
+ if key.array_type?
138
+ key = if key.percent_literal?
139
+ key.each_value.map { |v| decorate_source(v) }
140
+ else
141
+ key.each_value.map(&:source)
142
+ end
143
+ return key.join(', ')
144
+ end
145
+
146
+ key.literal? ? key.source : "*#{key.source}"
147
+ end
148
+
149
+ def decorate_source(value)
150
+ return ":\"#{value.source}\"" if value.dsym_type?
151
+ return "\"#{value.source}\"" if value.dstr_type?
152
+ return ":#{value.source}" if value.sym_type?
153
+
154
+ "'#{value.source}'"
155
+ end
156
+
157
+ def except_key(node)
158
+ key_arg = node.argument_list.first.source
159
+ body, = extract_body_if_negated(node.body)
160
+ lhs, _method_name, rhs = *body
161
+
162
+ lhs.source == key_arg ? rhs : lhs
163
+ end
164
+
165
+ def offense_range(node)
166
+ range_between(node.loc.selector.begin_pos, node.parent.loc.end.end_pos)
167
+ end
168
+ end
169
+ end
170
+ end
@@ -5,6 +5,10 @@ module RuboCop
5
5
  # Common functionality for checking whether an AST node/token is aligned
6
6
  # with something on a preceding or following line
7
7
  module PrecedingFollowingAlignment
8
+ # Tokens that end with an `=`, as well as `<<`, that can be aligned together:
9
+ # `=`, `==`, `===`, `!=`, `<=`, `>=`, `<<` and operator assignment (`+=`, etc).
10
+ ASSIGNMENT_OR_COMPARISON_TOKENS = %i[tEQL tEQ tEQQ tNEQ tLEQ tGEQ tOP_ASGN tLSHFT].freeze
11
+
8
12
  private
9
13
 
10
14
  def allow_for_alignment?
@@ -19,16 +23,20 @@ module RuboCop
19
23
  aligned_with_adjacent_line?(range, method(:aligned_operator?))
20
24
  end
21
25
 
22
- def aligned_with_preceding_assignment(token)
26
+ # Allows alignment with a preceding operator that ends with an `=`,
27
+ # including assignment and comparison operators.
28
+ def aligned_with_preceding_equals_operator(token)
23
29
  preceding_line_range = token.line.downto(1)
24
30
 
25
- aligned_with_assignment(token, preceding_line_range)
31
+ aligned_with_equals_sign(token, preceding_line_range)
26
32
  end
27
33
 
28
- def aligned_with_subsequent_assignment(token)
34
+ # Allows alignment with a subsequent operator that ends with an `=`,
35
+ # including assignment and comparison operators.
36
+ def aligned_with_subsequent_equals_operator(token)
29
37
  subsequent_line_range = token.line.upto(processed_source.lines.length)
30
38
 
31
- aligned_with_assignment(token, subsequent_line_range)
39
+ aligned_with_equals_sign(token, subsequent_line_range)
32
40
  end
33
41
 
34
42
  def aligned_with_adjacent_line?(range, predicate)
@@ -75,11 +83,11 @@ module RuboCop
75
83
  end
76
84
 
77
85
  def aligned_token?(range, line, lineno)
78
- aligned_words?(range, line) || aligned_assignment?(range, lineno)
86
+ aligned_words?(range, line) || aligned_equals_operator?(range, lineno)
79
87
  end
80
88
 
81
89
  def aligned_operator?(range, line, lineno)
82
- aligned_identical?(range, line) || aligned_assignment?(range, lineno)
90
+ aligned_identical?(range, line) || aligned_equals_operator?(range, lineno)
83
91
  end
84
92
 
85
93
  def aligned_words?(range, line)
@@ -90,22 +98,24 @@ module RuboCop
90
98
  token == line[left_edge, token.length]
91
99
  end
92
100
 
93
- def aligned_assignment?(range, lineno)
94
- # Check that assignment is aligned with a previous assignment operator
95
- # ie. an equals sign, an operator assignment, or an append operator (`<<`)
101
+ def aligned_equals_operator?(range, lineno)
102
+ # Check that the operator is aligned with a previous assignment or comparison operator
103
+ # ie. an equals sign, an operator assignment (eg. `+=`), a comparison (`==`, `<=`, etc.).
104
+ # Since append operators (`<<`) are a type of assignment, they are allowed as well,
105
+ # despite not ending with a literal equals character.
96
106
  line_range = processed_source.buffer.line_range(lineno)
97
107
  return false unless line_range
98
108
 
99
109
  # Find the specific token to avoid matching up to operators inside strings
100
- assignment_token = processed_source.tokens_within(line_range).detect do |token|
101
- token.equal_sign? || token.type == :tLSHFT
110
+ operator_token = processed_source.tokens_within(line_range).detect do |token|
111
+ ASSIGNMENT_OR_COMPARISON_TOKENS.include?(token.type)
102
112
  end
103
113
 
104
- aligned_with_preceding_assignment?(range, assignment_token) ||
105
- aligned_with_append_operator?(range, assignment_token)
114
+ aligned_with_preceding_equals?(range, operator_token) ||
115
+ aligned_with_append_operator?(range, operator_token)
106
116
  end
107
117
 
108
- def aligned_with_preceding_assignment?(range, token)
118
+ def aligned_with_preceding_equals?(range, token)
109
119
  return false unless token
110
120
 
111
121
  range.source[-1] == '=' && range.last_column == token.pos.last_column
@@ -123,7 +133,7 @@ module RuboCop
123
133
  range.source == line[range.column, range.size]
124
134
  end
125
135
 
126
- def aligned_with_assignment(token, line_range)
136
+ def aligned_with_equals_sign(token, line_range)
127
137
  token_line_indent = processed_source.line_indentation(token.line)
128
138
  assignment_lines = relevant_assignment_lines(line_range)
129
139
  relevant_line_number = assignment_lines[1]
@@ -135,7 +145,7 @@ module RuboCop
135
145
  return :none if relevant_indent < token_line_indent
136
146
  return :none unless processed_source.lines[relevant_line_number - 1]
137
147
 
138
- aligned_assignment?(token.pos, relevant_line_number) ? :yes : :no
148
+ aligned_equals_operator?(token.pos, relevant_line_number) ? :yes : :no
139
149
  end
140
150
 
141
151
  def assignment_lines
@@ -36,7 +36,7 @@ module RuboCop
36
36
  end
37
37
 
38
38
  def space_required_after?(token)
39
- (token.left_curly_brace? || token.type == :tLAMBEG) && space_required_after_lcurly?
39
+ token.left_curly_brace? && space_required_after_lcurly?
40
40
  end
41
41
 
42
42
  def space_required_after_lcurly?
@@ -55,14 +55,19 @@ module RuboCop
55
55
  end
56
56
 
57
57
  def if_body_source(if_body)
58
- if if_body.call_type? &&
59
- if_body.last_argument&.hash_type? && if_body.last_argument.pairs.last&.value_omission?
58
+ if if_body.call_type? && !if_body.method?(:[]=) && omitted_value_in_last_hash_arg?(if_body)
60
59
  "#{method_source(if_body)}(#{if_body.arguments.map(&:source).join(', ')})"
61
60
  else
62
61
  if_body.source
63
62
  end
64
63
  end
65
64
 
65
+ def omitted_value_in_last_hash_arg?(if_body)
66
+ return false unless (last_argument = if_body.last_argument)
67
+
68
+ last_argument.hash_type? && last_argument.pairs.last&.value_omission?
69
+ end
70
+
66
71
  def method_source(if_body)
67
72
  end_range = if_body.implicit_call? ? if_body.loc.dot.end : if_body.loc.selector
68
73
 
@@ -96,7 +101,7 @@ module RuboCop
96
101
  end
97
102
 
98
103
  def max_line_length
99
- return unless config.for_cop('Layout/LineLength')['Enabled']
104
+ return unless config.cop_enabled?('Layout/LineLength')
100
105
 
101
106
  config.for_cop('Layout/LineLength')['Max']
102
107
  end
@@ -26,7 +26,7 @@ module RuboCop
26
26
  end
27
27
 
28
28
  def string_literals_config
29
- config.for_cop('Style/StringLiterals')
29
+ config.for_enabled_cop('Style/StringLiterals')
30
30
  end
31
31
  end
32
32
  end
@@ -2,8 +2,8 @@
2
2
 
3
3
  module RuboCop
4
4
  module Cop
5
- # Common methods shared by Style/TrailingCommaInArguments and
6
- # Style/TrailingCommaInLiteral
5
+ # Common methods shared by Style/TrailingCommaInArguments,
6
+ # Style/TrailingCommaInArrayLiteral and Style/TrailingCommaInHashLiteral
7
7
  module TrailingComma
8
8
  include ConfigurableEnforcedStyle
9
9
  include RangeHelp
@@ -138,7 +138,7 @@ module RuboCop
138
138
  def use_block_argument_as_local_variable?(node, last_argument)
139
139
  return false if node.body.nil?
140
140
 
141
- node.body.each_descendant(:lvar, :lvasgn).any? do |lvar|
141
+ node.body.each_node(:lvar, :lvasgn).any? do |lvar|
142
142
  !lvar.parent.block_pass_type? && lvar.node_parts[0].to_s == last_argument
143
143
  end
144
144
  end
@@ -100,6 +100,7 @@ module RuboCop
100
100
  add_offense(node, message: REDUNDANT_HASH_MSG)
101
101
  end
102
102
  end
103
+ alias on_csend on_send
103
104
  alias on_op_asgn on_send
104
105
  end
105
106
  end
@@ -112,6 +112,26 @@ module RuboCop
112
112
  # private attr :quux
113
113
  #
114
114
  # end
115
+ #
116
+ # @example AllowModifiersOnAliasMethod: true (default)
117
+ # # good
118
+ # class Foo
119
+ #
120
+ # public alias_method :bar, :foo
121
+ # protected alias_method :baz, :foo
122
+ # private alias_method :qux, :foo
123
+ #
124
+ # end
125
+ #
126
+ # @example AllowModifiersOnAliasMethod: false
127
+ # # bad
128
+ # class Foo
129
+ #
130
+ # public alias_method :bar, :foo
131
+ # protected alias_method :baz, :foo
132
+ # private alias_method :qux, :foo
133
+ #
134
+ # end
115
135
  class AccessModifierDeclarations < Base
116
136
  extend AutoCorrector
117
137
 
@@ -145,6 +165,12 @@ module RuboCop
145
165
  (send nil? {:attr :attr_reader :attr_writer :attr_accessor} _+))
146
166
  PATTERN
147
167
 
168
+ # @!method access_modifier_with_alias_method?, <<~PATTERN
169
+ def_node_matcher :access_modifier_with_alias_method?, <<~PATTERN
170
+ (send nil? {:private :protected :public :module_function}
171
+ (send nil? :alias_method _ _))
172
+ PATTERN
173
+
148
174
  def on_send(node)
149
175
  return if allowed?(node)
150
176
 
@@ -164,7 +190,8 @@ module RuboCop
164
190
  !node.access_modifier? ||
165
191
  ALLOWED_NODE_TYPES.include?(node.parent&.type) ||
166
192
  allow_modifiers_on_symbols?(node) ||
167
- allow_modifiers_on_attrs?(node)
193
+ allow_modifiers_on_attrs?(node) ||
194
+ allow_modifiers_on_alias_method?(node)
168
195
  end
169
196
 
170
197
  def autocorrect(corrector, node)
@@ -194,6 +221,10 @@ module RuboCop
194
221
  cop_config['AllowModifiersOnAttrs'] && access_modifier_with_attr?(node)
195
222
  end
196
223
 
224
+ def allow_modifiers_on_alias_method?(node)
225
+ cop_config['AllowModifiersOnAliasMethod'] && access_modifier_with_alias_method?(node)
226
+ end
227
+
197
228
  def offense?(node)
198
229
  (group_style? && access_modifier_is_inlined?(node) &&
199
230
  !node.parent&.if_type? && !right_siblings_same_inline_method?(node)) ||
@@ -127,7 +127,7 @@ module RuboCop
127
127
  end
128
128
 
129
129
  def correct_other(node, corrector)
130
- return if node.source_range.begin.is?('(')
130
+ return if node.parenthesized_call?
131
131
 
132
132
  corrector.wrap(node, '(', ')')
133
133
  end
@@ -540,10 +540,7 @@ module RuboCop
540
540
  end
541
541
 
542
542
  def explicit_block_name?
543
- block_forwarding_config = config.for_cop('Naming/BlockForwarding')
544
- return false unless block_forwarding_config['Enabled']
545
-
546
- block_forwarding_config['EnforcedStyle'] == 'explicit'
543
+ config.for_enabled_cop('Naming/BlockForwarding')['EnforcedStyle'] == 'explicit'
547
544
  end
548
545
  end
549
546
  end
@@ -42,14 +42,30 @@ module RuboCop
42
42
  return if node.parent && brace_method?(node.parent)
43
43
 
44
44
  preferred = (value.zero? ? 'first' : 'last')
45
- add_offense(node.loc.selector, message: format(MSG, preferred: preferred)) do |corrector|
46
- corrector.replace(node.loc.selector, ".#{preferred}")
45
+ offense_range = find_offense_range(node)
46
+
47
+ add_offense(offense_range, message: format(MSG, preferred: preferred)) do |corrector|
48
+ corrector.replace(offense_range, preferred_value(node, preferred))
47
49
  end
48
50
  end
49
51
  # rubocop:enable Metrics/AbcSize
52
+ alias on_csend on_send
50
53
 
51
54
  private
52
55
 
56
+ def preferred_value(node, value)
57
+ value = ".#{value}" unless node.loc.dot
58
+ value
59
+ end
60
+
61
+ def find_offense_range(node)
62
+ if node.loc.dot
63
+ node.loc.selector.join(node.source_range.end)
64
+ else
65
+ node.loc.selector
66
+ end
67
+ end
68
+
53
69
  def innermost_braces_node(node)
54
70
  node = node.receiver while node.receiver.send_type? && node.receiver.method?(:[])
55
71
  node
@@ -195,6 +195,7 @@ module RuboCop
195
195
  end
196
196
  end
197
197
  end
198
+ alias on_csend on_send
198
199
 
199
200
  def on_block(node)
200
201
  return if ignored_node?(node)
@@ -348,7 +349,7 @@ module RuboCop
348
349
  case node.type
349
350
  when :block, :numblock
350
351
  yield node
351
- when :send
352
+ when :send, :csend
352
353
  # When a method has an argument which is another method with a block,
353
354
  # that block needs braces, otherwise a syntax error will be introduced
354
355
  # for subsequent arguments.
@@ -369,9 +370,8 @@ module RuboCop
369
370
  end
370
371
  # rubocop:enable Metrics/CyclomaticComplexity
371
372
 
372
- # rubocop:disable Metrics/CyclomaticComplexity
373
373
  def proper_block_style?(node)
374
- return true if require_braces?(node) || require_do_end?(node)
374
+ return true if require_do_end?(node)
375
375
  return special_method_proper_block_style?(node) if special_method?(node.method_name)
376
376
 
377
377
  case style
@@ -381,15 +381,6 @@ module RuboCop
381
381
  when :always_braces then braces_style?(node)
382
382
  end
383
383
  end
384
- # rubocop:enable Metrics/CyclomaticComplexity
385
-
386
- def require_braces?(node)
387
- return false unless node.braces?
388
-
389
- node.each_ancestor(:send).any? do |send|
390
- send.arithmetic_operation? && node.source_range.end_pos < send.loc.selector.begin_pos
391
- end
392
- end
393
384
 
394
385
  def require_do_end?(node)
395
386
  return false if node.braces? || node.multiline?
@@ -483,14 +474,10 @@ module RuboCop
483
474
  end
484
475
 
485
476
  def return_value_of_scope?(node)
486
- return false unless node.parent
487
-
488
- conditional?(node.parent) || array_or_range?(node.parent) ||
489
- node.parent.children.last == node
490
- end
477
+ return false unless (parent = node.parent)
491
478
 
492
- def conditional?(node)
493
- node.if_type? || node.operator_keyword?
479
+ parent.conditional? || parent.operator_keyword? || array_or_range?(parent) ||
480
+ parent.children.last == node
494
481
  end
495
482
 
496
483
  def array_or_range?(node)
@@ -126,9 +126,12 @@ module RuboCop
126
126
  end
127
127
 
128
128
  def unindent(corrector, node)
129
- return if node.body.children.last.nil?
129
+ return unless node.body.children.last
130
130
 
131
- column_delta = configured_indentation_width - leading_spaces(node.body.children.last).size
131
+ last_child_leading_spaces = leading_spaces(node.body.children.last)
132
+ return if leading_spaces(node).size == last_child_leading_spaces.size
133
+
134
+ column_delta = configured_indentation_width - last_child_leading_spaces.size
132
135
  return if column_delta.zero?
133
136
 
134
137
  AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
@@ -49,7 +49,6 @@ module RuboCop
49
49
  def on_block(node)
50
50
  check_method_node(node.send_node)
51
51
  end
52
-
53
52
  alias on_numblock on_block
54
53
 
55
54
  def on_send(node)
@@ -57,6 +56,7 @@ module RuboCop
57
56
 
58
57
  check_method_node(node)
59
58
  end
59
+ alias on_csend on_send
60
60
 
61
61
  private
62
62
 
@@ -309,7 +309,7 @@ module RuboCop
309
309
  end
310
310
 
311
311
  def allowed_single_line?(branches)
312
- single_line_conditions_only? && branches.any?(&:begin_type?)
312
+ single_line_conditions_only? && branches.compact.any?(&:begin_type?)
313
313
  end
314
314
 
315
315
  def assignment_node(node)
@@ -445,6 +445,8 @@ module RuboCop
445
445
  end
446
446
 
447
447
  [condition.loc.else, condition.loc.end].each do |loc|
448
+ next unless loc
449
+
448
450
  corrector.remove_preceding(loc, loc.column - column)
449
451
  end
450
452
  end