rubocop 1.8.0 → 1.11.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 (246) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -13
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +89 -22
  5. data/config/obsoletion.yml +4 -0
  6. data/lib/rubocop.rb +9 -0
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +5 -4
  8. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  9. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  10. data/lib/rubocop/config.rb +5 -2
  11. data/lib/rubocop/config_loader.rb +7 -14
  12. data/lib/rubocop/config_store.rb +12 -1
  13. data/lib/rubocop/cop/base.rb +2 -1
  14. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
  15. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  16. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
  17. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
  18. data/lib/rubocop/cop/exclude_limit.rb +26 -0
  19. data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
  20. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
  21. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  22. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  23. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
  24. data/lib/rubocop/cop/generator.rb +3 -5
  25. data/lib/rubocop/cop/internal_affairs.rb +6 -1
  26. data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +68 -0
  27. data/lib/rubocop/cop/internal_affairs/example_description.rb +90 -0
  28. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  29. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
  30. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
  31. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  32. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
  33. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +62 -0
  34. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +65 -0
  35. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  36. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
  37. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
  38. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  39. data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
  40. data/lib/rubocop/cop/layout/class_structure.rb +8 -2
  41. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
  42. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  43. data/lib/rubocop/cop/layout/first_argument_indentation.rb +22 -3
  44. data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
  45. data/lib/rubocop/cop/layout/line_length.rb +2 -1
  46. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
  47. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  48. data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
  49. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  50. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
  51. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
  52. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
  53. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
  54. data/lib/rubocop/cop/lint/debugger.rb +60 -14
  55. data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
  56. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
  57. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
  58. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
  59. data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
  60. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  61. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  62. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
  63. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  64. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
  65. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  66. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
  67. data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
  68. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
  69. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
  70. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
  71. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
  72. data/lib/rubocop/cop/lint/number_conversion.rb +43 -6
  73. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +47 -0
  74. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +39 -0
  75. data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
  76. data/lib/rubocop/cop/lint/rand_one.rb +1 -0
  77. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +5 -3
  78. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  79. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
  80. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
  81. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
  82. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
  83. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
  84. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  85. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
  86. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
  87. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  88. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  89. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  90. data/lib/rubocop/cop/lint/symbol_conversion.rb +103 -0
  91. data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
  92. data/lib/rubocop/cop/lint/triple_quotes.rb +71 -0
  93. data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
  94. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
  95. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  96. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
  97. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
  98. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
  99. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
  100. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
  101. data/lib/rubocop/cop/lint/useless_times.rb +3 -0
  102. data/lib/rubocop/cop/message_annotator.rb +4 -1
  103. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  104. data/lib/rubocop/cop/metrics/module_length.rb +1 -0
  105. data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
  106. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
  107. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
  108. data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -0
  109. data/lib/rubocop/cop/mixin/code_length.rb +3 -1
  110. data/lib/rubocop/cop/mixin/comments_help.rb +0 -1
  111. data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
  112. data/lib/rubocop/cop/mixin/def_node.rb +1 -0
  113. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
  114. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  115. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
  116. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
  117. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
  118. data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
  119. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
  120. data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
  121. data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
  122. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +5 -1
  123. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
  124. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  125. data/lib/rubocop/cop/naming/constant_name.rb +2 -0
  126. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -0
  127. data/lib/rubocop/cop/naming/method_name.rb +3 -0
  128. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  129. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +38 -5
  130. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  131. data/lib/rubocop/cop/registry.rb +1 -1
  132. data/lib/rubocop/cop/security/eval.rb +1 -0
  133. data/lib/rubocop/cop/security/json_load.rb +1 -0
  134. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  135. data/lib/rubocop/cop/security/open.rb +1 -0
  136. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  137. data/lib/rubocop/cop/severity.rb +3 -3
  138. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
  139. data/lib/rubocop/cop/style/alias.rb +1 -0
  140. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  141. data/lib/rubocop/cop/style/array_coercion.rb +2 -0
  142. data/lib/rubocop/cop/style/array_join.rb +1 -0
  143. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  144. data/lib/rubocop/cop/style/attr.rb +1 -0
  145. data/lib/rubocop/cop/style/case_equality.rb +2 -1
  146. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -0
  147. data/lib/rubocop/cop/style/collection_compact.rb +2 -0
  148. data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
  149. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  150. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  151. data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
  152. data/lib/rubocop/cop/style/date_time.rb +3 -0
  153. data/lib/rubocop/cop/style/dir.rb +1 -0
  154. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -9
  155. data/lib/rubocop/cop/style/documentation.rb +5 -0
  156. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  157. data/lib/rubocop/cop/style/double_negation.rb +3 -2
  158. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
  159. data/lib/rubocop/cop/style/each_with_object.rb +1 -0
  160. data/lib/rubocop/cop/style/empty_literal.rb +9 -0
  161. data/lib/rubocop/cop/style/endless_method.rb +1 -0
  162. data/lib/rubocop/cop/style/eval_with_location.rb +140 -49
  163. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  164. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
  165. data/lib/rubocop/cop/style/explicit_block_argument.rb +12 -1
  166. data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
  167. data/lib/rubocop/cop/style/float_division.rb +7 -0
  168. data/lib/rubocop/cop/style/format_string.rb +2 -0
  169. data/lib/rubocop/cop/style/format_string_token.rb +19 -2
  170. data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
  171. data/lib/rubocop/cop/style/hash_conversion.rb +105 -0
  172. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
  173. data/lib/rubocop/cop/style/hash_except.rb +1 -0
  174. data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
  175. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
  176. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
  177. data/lib/rubocop/cop/style/if_inside_else.rb +14 -7
  178. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +122 -0
  179. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  180. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  181. data/lib/rubocop/cop/style/min_max.rb +1 -0
  182. data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
  183. data/lib/rubocop/cop/style/module_function.rb +5 -0
  184. data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
  185. data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
  186. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -0
  187. data/lib/rubocop/cop/style/nil_comparison.rb +6 -0
  188. data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
  189. data/lib/rubocop/cop/style/non_nil_check.rb +30 -13
  190. data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
  191. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
  192. data/lib/rubocop/cop/style/option_hash.rb +1 -0
  193. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  194. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
  195. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  196. data/lib/rubocop/cop/style/proc.rb +1 -0
  197. data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
  198. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
  199. data/lib/rubocop/cop/style/redundant_begin.rb +7 -1
  200. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
  201. data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
  202. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
  203. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  204. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
  205. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
  206. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
  207. data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
  208. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
  209. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  210. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
  211. data/lib/rubocop/cop/style/return_nil.rb +6 -0
  212. data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
  213. data/lib/rubocop/cop/style/sample.rb +1 -0
  214. data/lib/rubocop/cop/style/signal_exception.rb +3 -0
  215. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  216. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  217. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
  218. data/lib/rubocop/cop/style/sole_nested_conditional.rb +28 -4
  219. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  220. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  221. data/lib/rubocop/cop/style/string_concatenation.rb +2 -1
  222. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
  223. data/lib/rubocop/cop/style/strip.rb +1 -0
  224. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -0
  225. data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
  226. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -1
  227. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -0
  228. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  229. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
  230. data/lib/rubocop/cop/style/unless_logical_operators.rb +99 -0
  231. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  232. data/lib/rubocop/cop/style/while_until_modifier.rb +2 -4
  233. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  234. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
  235. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -0
  236. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  237. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
  238. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  239. data/lib/rubocop/magic_comment.rb +30 -1
  240. data/lib/rubocop/name_similarity.rb +1 -1
  241. data/lib/rubocop/options.rb +1 -1
  242. data/lib/rubocop/rspec/expect_offense.rb +5 -2
  243. data/lib/rubocop/runner.rb +1 -0
  244. data/lib/rubocop/target_ruby.rb +21 -13
  245. data/lib/rubocop/version.rb +2 -2
  246. metadata +21 -7
@@ -11,12 +11,12 @@ module RuboCop
11
11
  # name = "RuboCop"
12
12
  # # Some comment and an empty line
13
13
  #
14
- # website += "/rubocop-hq/rubocop" unless cond
14
+ # website += "/rubocop/rubocop" unless cond
15
15
  # puts "rubocop" if debug
16
16
  #
17
17
  # # bad for any configuration
18
18
  # set_app("RuboCop")
19
- # website = "https://github.com/rubocop-hq/rubocop"
19
+ # website = "https://github.com/rubocop/rubocop"
20
20
  #
21
21
  # # good only if AllowBeforeTrailingComments is true
22
22
  # object.method(arg) # this is a comment
@@ -4,11 +4,14 @@ module RuboCop
4
4
  module Cop
5
5
  module Layout
6
6
  # This cop checks the indentation of the first argument in a method call.
7
- # Arguments after the first one are checked by Layout/ArgumentAlignment,
7
+ # Arguments after the first one are checked by `Layout/ArgumentAlignment`,
8
8
  # not by this cop.
9
9
  #
10
10
  # For indenting the first parameter of method _definitions_, check out
11
- # Layout/FirstParameterIndentation.
11
+ # `Layout/FirstParameterIndentation`.
12
+ #
13
+ # This cop will respect `Layout/ArgumentAlignment` and will not work when
14
+ # `EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArgumentAlignment`.
12
15
  #
13
16
  # @example
14
17
  #
@@ -149,7 +152,8 @@ module RuboCop
149
152
  MSG = 'Indent the first argument one step more than %<base>s.'
150
153
 
151
154
  def on_send(node)
152
- return if !node.arguments? || node.operator_method?
155
+ return if style != :consistent && enforce_first_argument_with_fixed_indentation?
156
+ return if !node.arguments? || bare_operator?(node)
153
157
 
154
158
  indent = base_indentation(node) + configured_indentation_width
155
159
 
@@ -163,6 +167,10 @@ module RuboCop
163
167
 
164
168
  private
165
169
 
170
+ def bare_operator?(node)
171
+ node.operator_method? && !node.dot?
172
+ end
173
+
166
174
  def message(arg_node)
167
175
  return 'Bad indentation of the first argument.' unless arg_node
168
176
 
@@ -202,6 +210,7 @@ module RuboCop
202
210
  node.source_range.begin_pos > parent.source_range.begin_pos
203
211
  end
204
212
 
213
+ # @!method eligible_method_call?(node)
205
214
  def_node_matcher :eligible_method_call?, <<~PATTERN
206
215
  (send _ !:[]= ...)
207
216
  PATTERN
@@ -250,6 +259,16 @@ module RuboCop
250
259
  def on_new_investigation
251
260
  @comment_lines = nil
252
261
  end
262
+
263
+ def enforce_first_argument_with_fixed_indentation?
264
+ return false unless argument_alignment_config['Enabled']
265
+
266
+ argument_alignment_config['EnforcedStyle'] == 'with_fixed_indentation'
267
+ end
268
+
269
+ def argument_alignment_config
270
+ config.for_cop('Layout/ArgumentAlignment')
271
+ end
253
272
  end
254
273
  end
255
274
  end
@@ -52,6 +52,7 @@ module RuboCop
52
52
  MSG = 'Use %<configured_indentation_width>d (not %<indentation>d) ' \
53
53
  'spaces for%<name>s indentation.'
54
54
 
55
+ # @!method access_modifier?(node)
55
56
  def_node_matcher :access_modifier?, <<~PATTERN
56
57
  [(send ...) access_modifier?]
57
58
  PATTERN
@@ -60,12 +60,13 @@ module RuboCop
60
60
  # }
61
61
  class LineLength < Base
62
62
  include CheckLineBreakable
63
- include ConfigurableMax
64
63
  include IgnoredPattern
65
64
  include RangeHelp
66
65
  include LineLengthHelp
67
66
  extend AutoCorrector
68
67
 
68
+ exclude_limit 'Max'
69
+
69
70
  MSG = 'Line is too long. [%<length>d/%<max>d]'
70
71
 
71
72
  def on_block(node)
@@ -31,6 +31,32 @@ module RuboCop
31
31
  # foo = if expression
32
32
  # 'bar'
33
33
  # end
34
+ #
35
+ # @example SupportedTypes: ['block', 'case', 'class', 'if', 'kwbegin', 'module'] (default)
36
+ # # good
37
+ # foo =
38
+ # if expression
39
+ # 'bar'
40
+ # end
41
+ #
42
+ # # good
43
+ # foo =
44
+ # [1].map do |i|
45
+ # i + 1
46
+ # end
47
+ #
48
+ # @example SupportedTypes: ['block']
49
+ # # good
50
+ # foo = if expression
51
+ # 'bar'
52
+ # end
53
+ #
54
+ # # good
55
+ # foo =
56
+ # [1].map do |i|
57
+ # 'bar' * i
58
+ # end
59
+ #
34
60
  class MultilineAssignmentLayout < Base
35
61
  include CheckAssignment
36
62
  include ConfigurableEnforcedStyle
@@ -61,7 +61,7 @@ module RuboCop
61
61
  # by `EnforcedStyle: line_count_based` of `Style/BlockDelimiters` cop.
62
62
  # That means preventing auto-correction to incorrect auto-corrected
63
63
  # code.
64
- # See: https://github.com/rubocop-hq/rubocop/issues/7534
64
+ # See: https://github.com/rubocop/rubocop/issues/7534
65
65
  return if conflict_with_block_delimiters?(node)
66
66
 
67
67
  left_brace = node.loc.begin
@@ -39,12 +39,17 @@ module RuboCop
39
39
 
40
40
  range_between(receiver_end_pos, selector_begin_pos)
41
41
  elsif node.method?(:[]=)
42
- end_pos = node.receiver.source_range.end_pos
42
+ offense_range_for_assignment(node, begin_pos)
43
+ end
44
+ end
43
45
 
44
- return if begin_pos - end_pos == 1
46
+ def offense_range_for_assignment(node, begin_pos)
47
+ end_pos = node.receiver.source_range.end_pos
45
48
 
46
- range_between(end_pos, begin_pos - 1)
47
- end
49
+ return if begin_pos - end_pos == 1 ||
50
+ (range = range_between(end_pos, begin_pos - 1)).source.start_with?('[')
51
+
52
+ range
48
53
  end
49
54
 
50
55
  def register_offense(range)
@@ -89,7 +89,7 @@ module RuboCop
89
89
  # preventing auto-correction to single-line empty braces. It will
90
90
  # conflict with auto-correction by `Layout/SpaceInsideBlockBraces` cop
91
91
  # if auto-corrected to a single-line empty braces.
92
- # See: https://github.com/rubocop-hq/rubocop/issues/7363
92
+ # See: https://github.com/rubocop/rubocop/issues/7363
93
93
  return if node.body.nil? && node.multiline?
94
94
 
95
95
  left_brace = node.loc.begin
@@ -21,6 +21,7 @@ module RuboCop
21
21
  'Use `%<double_colon>sBigDecimal()` instead.'
22
22
  RESTRICT_ON_SEND = %i[new].freeze
23
23
 
24
+ # @!method big_decimal_new(node)
24
25
  def_node_matcher :big_decimal_new, <<~PATTERN
25
26
  (send
26
27
  (const ${nil? cbase} :BigDecimal) :new ...)
@@ -27,6 +27,7 @@ module RuboCop
27
27
  MSG = 'Symbol with a boolean name - ' \
28
28
  'you probably meant to use `%<boolean>s`.'
29
29
 
30
+ # @!method boolean_symbol?(node)
30
31
  def_node_matcher :boolean_symbol?, '(sym {:true :false})'
31
32
 
32
33
  def on_sym(node)
@@ -66,10 +66,12 @@ module RuboCop
66
66
 
67
67
  MSG = 'Do not define constants this way within a block.'
68
68
 
69
+ # @!method constant_assigned_in_block?(node)
69
70
  def_node_matcher :constant_assigned_in_block?, <<~PATTERN
70
71
  ({^block_type? [^begin_type? ^^block_type?]} nil? ...)
71
72
  PATTERN
72
73
 
74
+ # @!method module_defined_in_block?(node)
73
75
  def_node_matcher :module_defined_in_block?, <<~PATTERN
74
76
  ({^block_type? [^begin_type? ^^block_type?]} ...)
75
77
  PATTERN
@@ -58,6 +58,7 @@ module RuboCop
58
58
  class ConstantResolution < Base
59
59
  MSG = 'Fully qualify this constant to avoid possibly ambiguous resolution.'
60
60
 
61
+ # @!method unqualified_const?(node)
61
62
  def_node_matcher :unqualified_const?, <<~PATTERN
62
63
  (const nil? #const_name?)
63
64
  PATTERN
@@ -3,8 +3,21 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # This cop checks for calls to debugger or pry.
7
- # The cop can be configured to define which methods and receivers must be fixed.
6
+ # This cop checks for debug calls (such as `debugger` or `binding.pry`) that should
7
+ # not be kept for production code.
8
+ #
9
+ # The cop can be configured using `DebuggerMethods`. By default, a number of gems
10
+ # debug entrypoints are configured (`Kernel`, `Byebug`, `Capybara`, `Pry`, `Rails`,
11
+ # and `WebConsole`). Additional methods can be added.
12
+ #
13
+ # Specific default groups can be disabled if necessary:
14
+ #
15
+ # [source,yaml]
16
+ # ----
17
+ # Lint/Debugger:
18
+ # WebConsole: ~
19
+ # ----
20
+ #
8
21
  #
9
22
  # @example
10
23
  #
@@ -33,14 +46,34 @@ module RuboCop
33
46
  # def some_method
34
47
  # do_something
35
48
  # end
49
+ #
50
+ # @example DebuggerMethods: [my_debugger]
51
+ #
52
+ # # bad (ok during development)
53
+ #
54
+ # def some_method
55
+ # my_debugger
56
+ # end
36
57
  class Debugger < Base
37
58
  MSG = 'Remove debugger entry point `%<source>s`.'
38
59
 
39
60
  RESTRICT_ON_SEND = [].freeze
40
61
 
62
+ # @!method kernel?(node)
63
+ def_node_matcher :kernel?, <<~PATTERN
64
+ (const {nil? cbase} :Kernel)
65
+ PATTERN
66
+
67
+ # @!method valid_receiver?(node, arg1)
68
+ def_node_matcher :valid_receiver?, <<~PATTERN
69
+ {
70
+ (const {nil? cbase} %1)
71
+ (send {nil? #kernel?} %1)
72
+ }
73
+ PATTERN
74
+
41
75
  def on_send(node)
42
- return unless debugger_method?(node.method_name)
43
- return if !node.receiver.nil? && !debugger_receiver?(node)
76
+ return unless debugger_method?(node)
44
77
 
45
78
  add_offense(node)
46
79
  end
@@ -51,19 +84,32 @@ module RuboCop
51
84
  format(MSG, source: node.source)
52
85
  end
53
86
 
54
- def debugger_method?(name)
55
- cop_config.fetch('DebuggerMethods', []).include?(name.to_s)
87
+ def debugger_methods
88
+ @debugger_methods ||= begin
89
+ config = cop_config.fetch('DebuggerMethods', [])
90
+ values = config.is_a?(Array) ? config : config.values.flatten
91
+ values.map do |v|
92
+ next unless v
93
+
94
+ *receiver, method_name = v.split('.')
95
+ {
96
+ receiver: receiver.empty? ? nil : receiver.join.to_sym,
97
+ method_name: method_name.to_sym
98
+ }
99
+ end.compact
100
+ end
56
101
  end
57
102
 
58
- def debugger_receiver?(node)
59
- receiver = case node.receiver
60
- when RuboCop::AST::SendNode
61
- node.receiver.method_name
62
- when RuboCop::AST::ConstNode
63
- node.receiver.const_name
64
- end
103
+ def debugger_method?(send_node)
104
+ debugger_methods.any? do |method|
105
+ next unless method[:method_name] == send_node.method_name
65
106
 
66
- cop_config.fetch('DebuggerReceivers', []).include?(receiver.to_s)
107
+ if method[:receiver].nil?
108
+ send_node.receiver.nil?
109
+ else
110
+ valid_receiver?(send_node.receiver, method[:receiver])
111
+ end
112
+ end
67
113
  end
68
114
  end
69
115
  end
@@ -37,6 +37,11 @@ module RuboCop
37
37
  DO_NOT_USE_MSG = 'Do not use `%<bad>s`%<deprecated_message>s.'
38
38
 
39
39
  def on_const(node)
40
+ # FIXME: Workaround for "`undefined method `expression' for nil:NilClass`" when processing
41
+ # `__ENCODING__`. It is better to be able to work without this condition.
42
+ # Maybe further investigation of RuboCop AST will lead to an essential solution.
43
+ return unless node.loc
44
+
40
45
  constant = node.absolute? ? consntant_name(node, node.short_name.to_s) : node.source
41
46
  return unless (deprecated_constant = deprecated_constants[constant])
42
47
 
@@ -44,6 +44,9 @@ module RuboCop
44
44
  MSG = 'Use `%<constant>s.%<method>s(%<replacement_args>s)`' \
45
45
  ' instead of `%<original>s`.'
46
46
 
47
+ NO_ARG_ALGORITHM = %w[BF DES IDEA RC4].freeze
48
+
49
+ # @!method algorithm_const(node)
47
50
  def_node_matcher :algorithm_const, <<~PATTERN
48
51
  (send
49
52
  $(const
@@ -104,7 +107,7 @@ module RuboCop
104
107
  def algorithm_name(node)
105
108
  name = node.loc.name.source
106
109
 
107
- if openssl_class(node) == 'OpenSSL::Cipher'
110
+ if openssl_class(node) == 'OpenSSL::Cipher' && !NO_ARG_ALGORITHM.include?(name)
108
111
  name.scan(/.{3}/).join('-')
109
112
  else
110
113
  name
@@ -124,16 +127,23 @@ module RuboCop
124
127
  algorithm_name = algorithm_name(algorithm_constant)
125
128
 
126
129
  if openssl_class(algorithm_constant) == 'OpenSSL::Cipher'
127
- build_cipher_arguments(node, algorithm_name)
130
+ build_cipher_arguments(node, algorithm_name, node.arguments.empty?)
128
131
  else
129
132
  (["'#{algorithm_name}'"] + node.arguments.map(&:source)).join(', ')
130
133
  end
131
134
  end
132
135
 
133
- def build_cipher_arguments(node, algorithm_name)
136
+ def build_cipher_arguments(node, algorithm_name, no_arguments)
134
137
  algorithm_parts = algorithm_name.downcase.split('-')
135
138
  size_and_mode = sanitize_arguments(node.arguments).map(&:downcase)
136
- "'#{(algorithm_parts + size_and_mode + ['cbc']).take(3).join('-')}'"
139
+
140
+ if NO_ARG_ALGORITHM.include?(algorithm_parts.first.upcase) && no_arguments
141
+ "'#{algorithm_parts.first}'"
142
+ else
143
+ mode = 'cbc' unless size_and_mode == ['cbc']
144
+
145
+ "'#{(algorithm_parts + size_and_mode + [mode]).compact.take(3).join('-')}'"
146
+ end
137
147
  end
138
148
  end
139
149
  end
@@ -74,7 +74,7 @@ module RuboCop
74
74
  # else 250
75
75
  # end
76
76
  #
77
- # @example IgnoreLiteralBranches: true
77
+ # @example IgnoreConstantBranches: true
78
78
  # # good
79
79
  # case size
80
80
  # when "small" then SMALL_SIZE
@@ -82,6 +82,7 @@ module RuboCop
82
82
  end
83
83
  end
84
84
 
85
+ # @!method method_alias?(node)
85
86
  def_node_matcher :method_alias?, <<~PATTERN
86
87
  (alias (sym $_name) sym)
87
88
  PATTERN
@@ -94,10 +95,12 @@ module RuboCop
94
95
  found_instance_method(node, name)
95
96
  end
96
97
 
98
+ # @!method alias_method?(node)
97
99
  def_node_matcher :alias_method?, <<~PATTERN
98
100
  (send nil? :alias_method (sym $_name) _)
99
101
  PATTERN
100
102
 
103
+ # @!method sym_name(node)
101
104
  def_node_matcher :sym_name, '(sym $_name)'
102
105
  def on_send(node)
103
106
  if (name = alias_method?(node))
@@ -21,11 +21,12 @@ module RuboCop
21
21
  #
22
22
  class DuplicateRequire < Base
23
23
  MSG = 'Duplicate `%<method>s` detected.'
24
- REQUIRE_METHODS = %i[require require_relative].freeze
24
+ REQUIRE_METHODS = Set.new(%i[require require_relative]).freeze
25
25
  RESTRICT_ON_SEND = REQUIRE_METHODS
26
26
 
27
+ # @!method require_call?(node)
27
28
  def_node_matcher :require_call?, <<~PATTERN
28
- (send {nil? (const _ :Kernel)} {:#{REQUIRE_METHODS.join(' :')}} _)
29
+ (send {nil? (const _ :Kernel)} %REQUIRE_METHODS _)
29
30
  PATTERN
30
31
 
31
32
  def on_new_investigation
@@ -25,6 +25,7 @@ module RuboCop
25
25
  MSG = 'The argument to each_with_object cannot be immutable.'
26
26
  RESTRICT_ON_SEND = %i[each_with_object].freeze
27
27
 
28
+ # @!method each_with_object?(node)
28
29
  def_node_matcher :each_with_object?, <<~PATTERN
29
30
  ({send csend} _ :each_with_object $_)
30
31
  PATTERN
@@ -47,7 +47,7 @@ module RuboCop
47
47
  MSG = 'Odd `else` layout detected. Did you mean to use `elsif`?'
48
48
 
49
49
  def on_if(node)
50
- return if node.ternary? || node.elsif?
50
+ return if node.ternary?
51
51
 
52
52
  check(node)
53
53
  end