rubocop 0.47.1 → 0.48.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.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -16
  3. data/config/default.yml +203 -115
  4. data/config/disabled.yml +0 -5
  5. data/config/enabled.yml +92 -8
  6. data/lib/rubocop.rb +24 -1
  7. data/lib/rubocop/ast/builder.rb +7 -2
  8. data/lib/rubocop/ast/node.rb +23 -20
  9. data/lib/rubocop/ast/node/and_node.rb +37 -0
  10. data/lib/rubocop/ast/node/array_node.rb +4 -1
  11. data/lib/rubocop/ast/node/case_node.rb +1 -0
  12. data/lib/rubocop/ast/node/ensure_node.rb +25 -0
  13. data/lib/rubocop/ast/node/hash_node.rb +10 -3
  14. data/lib/rubocop/ast/node/if_node.rb +2 -0
  15. data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +23 -0
  16. data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +35 -0
  17. data/lib/rubocop/ast/node/or_node.rb +37 -0
  18. data/lib/rubocop/ast/node/resbody_node.rb +25 -0
  19. data/lib/rubocop/ast/node/send_node.rb +190 -0
  20. data/lib/rubocop/ast/node/when_node.rb +1 -1
  21. data/lib/rubocop/ast/traversal.rb +15 -15
  22. data/lib/rubocop/comment_config.rb +1 -1
  23. data/lib/rubocop/config.rb +39 -15
  24. data/lib/rubocop/config_loader.rb +34 -13
  25. data/lib/rubocop/cop/bundler/ordered_gems.rb +23 -4
  26. data/lib/rubocop/cop/commissioner.rb +4 -0
  27. data/lib/rubocop/cop/cop.rb +5 -0
  28. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +53 -0
  29. data/lib/rubocop/cop/lint/debugger.rb +8 -1
  30. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -1
  31. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +2 -4
  32. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
  33. data/lib/rubocop/cop/lint/each_with_object_argument.rb +3 -1
  34. data/lib/rubocop/cop/lint/empty_ensure.rb +6 -2
  35. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  36. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +20 -18
  37. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -3
  38. data/lib/rubocop/cop/lint/literal_in_condition.rb +1 -1
  39. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  40. data/lib/rubocop/cop/lint/multiple_compare.rb +5 -3
  41. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +26 -18
  42. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +7 -8
  43. data/lib/rubocop/cop/lint/require_parentheses.rb +7 -13
  44. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +7 -3
  45. data/lib/rubocop/cop/lint/shadowed_exception.rb +2 -6
  46. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +7 -8
  47. data/lib/rubocop/cop/lint/unneeded_disable.rb +35 -11
  48. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  49. data/lib/rubocop/cop/lint/unreachable_code.rb +5 -2
  50. data/lib/rubocop/cop/lint/unused_block_argument.rb +6 -6
  51. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -1
  52. data/lib/rubocop/cop/lint/useless_comparison.rb +5 -4
  53. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  54. data/lib/rubocop/cop/message_annotator.rb +7 -3
  55. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  56. data/lib/rubocop/cop/metrics/block_nesting.rb +4 -4
  57. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
  58. data/lib/rubocop/cop/metrics/line_length.rb +2 -9
  59. data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -3
  60. data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
  61. data/lib/rubocop/cop/mixin/access_modifier_node.rb +1 -1
  62. data/lib/rubocop/cop/mixin/array_hash_indentation.rb +2 -2
  63. data/lib/rubocop/cop/mixin/check_assignment.rb +6 -6
  64. data/lib/rubocop/cop/mixin/duplication.rb +1 -1
  65. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  66. data/lib/rubocop/cop/mixin/ignored_pattern.rb +27 -0
  67. data/lib/rubocop/cop/mixin/method_preference.rb +2 -0
  68. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +17 -29
  69. data/lib/rubocop/cop/mixin/on_method_def.rb +3 -3
  70. data/lib/rubocop/cop/mixin/percent_literal.rb +27 -0
  71. data/lib/rubocop/cop/mixin/rescue_node.rb +21 -0
  72. data/lib/rubocop/cop/mixin/safe_mode.rb +1 -1
  73. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  74. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -1
  75. data/lib/rubocop/cop/mixin/target_rails_version.rb +16 -0
  76. data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
  77. data/lib/rubocop/cop/offense.rb +3 -3
  78. data/lib/rubocop/cop/performance/casecmp.rb +1 -1
  79. data/lib/rubocop/cop/performance/detect.rb +2 -1
  80. data/lib/rubocop/cop/performance/double_start_end_with.rb +35 -1
  81. data/lib/rubocop/cop/performance/end_with.rb +3 -1
  82. data/lib/rubocop/cop/performance/flat_map.rb +6 -6
  83. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
  84. data/lib/rubocop/cop/performance/range_include.rb +3 -1
  85. data/lib/rubocop/cop/performance/redundant_match.rb +6 -5
  86. data/lib/rubocop/cop/performance/regexp_match.rb +10 -3
  87. data/lib/rubocop/cop/performance/reverse_each.rb +2 -1
  88. data/lib/rubocop/cop/performance/size.rb +6 -11
  89. data/lib/rubocop/cop/performance/start_with.rb +3 -1
  90. data/lib/rubocop/cop/performance/string_replacement.rb +13 -18
  91. data/lib/rubocop/cop/performance/times_map.rb +4 -4
  92. data/lib/rubocop/cop/rails/action_filter.rb +42 -42
  93. data/lib/rubocop/cop/rails/active_support_aliases.rb +68 -0
  94. data/lib/rubocop/cop/rails/blank.rb +131 -0
  95. data/lib/rubocop/cop/rails/date.rb +25 -28
  96. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +5 -7
  97. data/lib/rubocop/cop/rails/dynamic_find_by.rb +7 -3
  98. data/lib/rubocop/cop/rails/exit.rb +9 -9
  99. data/lib/rubocop/cop/rails/file_path.rb +5 -14
  100. data/lib/rubocop/cop/rails/find_by.rb +8 -10
  101. data/lib/rubocop/cop/rails/find_each.rb +6 -9
  102. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -0
  103. data/lib/rubocop/cop/rails/http_positional_arguments.rb +15 -7
  104. data/lib/rubocop/cop/rails/output.rb +3 -5
  105. data/lib/rubocop/cop/rails/output_safety.rb +4 -8
  106. data/lib/rubocop/cop/rails/pluralization_grammar.rb +25 -24
  107. data/lib/rubocop/cop/rails/present.rb +137 -0
  108. data/lib/rubocop/cop/rails/read_write_attribute.rb +9 -18
  109. data/lib/rubocop/cop/rails/relative_date_constant.rb +53 -0
  110. data/lib/rubocop/cop/rails/request_referer.rb +7 -4
  111. data/lib/rubocop/cop/rails/reversible_migration.rb +1 -2
  112. data/lib/rubocop/cop/rails/safe_navigation.rb +2 -1
  113. data/lib/rubocop/cop/rails/save_bang.rb +10 -10
  114. data/lib/rubocop/cop/rails/skips_model_validations.rb +23 -6
  115. data/lib/rubocop/cop/rails/time_zone.rb +20 -18
  116. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -2
  117. data/lib/rubocop/cop/rails/validation.rb +8 -11
  118. data/lib/rubocop/cop/registry.rb +3 -3
  119. data/lib/rubocop/cop/security/json_load.rb +1 -1
  120. data/lib/rubocop/cop/security/marshal_load.rb +5 -1
  121. data/lib/rubocop/cop/security/yaml_load.rb +3 -3
  122. data/lib/rubocop/cop/severity.rb +1 -1
  123. data/lib/rubocop/cop/style/alias.rb +5 -5
  124. data/lib/rubocop/cop/style/align_hash.rb +1 -1
  125. data/lib/rubocop/cop/style/align_parameters.rb +5 -5
  126. data/lib/rubocop/cop/style/and_or.rb +16 -31
  127. data/lib/rubocop/cop/style/attr.rb +14 -8
  128. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +8 -11
  129. data/lib/rubocop/cop/style/block_delimiters.rb +11 -13
  130. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +19 -23
  131. data/lib/rubocop/cop/style/case_indentation.rb +2 -0
  132. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  133. data/lib/rubocop/cop/style/class_check.rb +5 -7
  134. data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +5 -4
  135. data/lib/rubocop/cop/style/collection_methods.rb +8 -8
  136. data/lib/rubocop/cop/style/colon_method_call.rb +2 -9
  137. data/lib/rubocop/cop/style/conditional_assignment.rb +38 -45
  138. data/lib/rubocop/cop/style/constant_name.rb +1 -1
  139. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  140. data/lib/rubocop/cop/style/dot_position.rb +3 -7
  141. data/lib/rubocop/cop/style/double_negation.rb +2 -1
  142. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  143. data/lib/rubocop/cop/style/empty_else.rb +2 -2
  144. data/lib/rubocop/cop/style/empty_line_after_magic_comment.rb +63 -0
  145. data/lib/rubocop/cop/style/empty_line_between_defs.rb +74 -4
  146. data/lib/rubocop/cop/style/empty_lines_around_begin_body.rb +42 -0
  147. data/lib/rubocop/cop/style/empty_lines_around_exception_handling_keywords.rb +127 -0
  148. data/lib/rubocop/cop/style/empty_literal.rb +17 -9
  149. data/lib/rubocop/cop/style/end_of_line.rb +25 -3
  150. data/lib/rubocop/cop/style/file_name.rb +1 -1
  151. data/lib/rubocop/cop/style/first_method_argument_line_break.rb +1 -1
  152. data/lib/rubocop/cop/style/first_parameter_indentation.rb +17 -19
  153. data/lib/rubocop/cop/style/for.rb +2 -4
  154. data/lib/rubocop/cop/style/format_string.rb +5 -4
  155. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  156. data/lib/rubocop/cop/style/identical_conditional_branches.rb +27 -1
  157. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
  158. data/lib/rubocop/cop/style/indent_assignment.rb +2 -2
  159. data/lib/rubocop/cop/style/indent_hash.rb +2 -1
  160. data/lib/rubocop/cop/style/indent_heredoc.rb +173 -0
  161. data/lib/rubocop/cop/style/indentation_width.rb +61 -29
  162. data/lib/rubocop/cop/style/inverse_methods.rb +130 -0
  163. data/lib/rubocop/cop/style/lambda_call.rb +15 -11
  164. data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -4
  165. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +26 -14
  166. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -16
  167. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
  168. data/lib/rubocop/cop/style/missing_else.rb +4 -3
  169. data/lib/rubocop/cop/style/mixin_grouping.rb +97 -0
  170. data/lib/rubocop/cop/style/multiline_memoization.rb +38 -5
  171. data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +2 -3
  172. data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +38 -19
  173. data/lib/rubocop/cop/style/mutable_constant.rb +5 -1
  174. data/lib/rubocop/cop/style/negated_if.rb +73 -1
  175. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +21 -19
  176. data/lib/rubocop/cop/style/next.rb +5 -5
  177. data/lib/rubocop/cop/style/non_nil_check.rb +7 -10
  178. data/lib/rubocop/cop/style/not.rb +3 -4
  179. data/lib/rubocop/cop/style/numeric_literals.rb +25 -3
  180. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  181. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
  182. data/lib/rubocop/cop/style/op_method.rb +2 -2
  183. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -3
  184. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +52 -6
  185. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  186. data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -9
  187. data/lib/rubocop/cop/style/raise_args.rb +28 -24
  188. data/lib/rubocop/cop/style/redundant_freeze.rb +5 -7
  189. data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -3
  190. data/lib/rubocop/cop/style/redundant_self.rb +17 -35
  191. data/lib/rubocop/cop/style/rescue_modifier.rb +2 -14
  192. data/lib/rubocop/cop/style/self_assignment.rb +3 -3
  193. data/lib/rubocop/cop/style/send.rb +4 -5
  194. data/lib/rubocop/cop/style/space_after_not.rb +7 -8
  195. data/lib/rubocop/cop/style/space_around_keyword.rb +8 -9
  196. data/lib/rubocop/cop/style/space_around_operators.rb +19 -15
  197. data/lib/rubocop/cop/style/space_before_first_arg.rb +17 -14
  198. data/lib/rubocop/cop/style/space_inside_brackets.rb +1 -1
  199. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +3 -3
  200. data/lib/rubocop/cop/style/space_inside_parens.rb +1 -1
  201. data/lib/rubocop/cop/style/special_global_vars.rb +14 -14
  202. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -1
  203. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  204. data/lib/rubocop/cop/style/string_methods.rb +10 -5
  205. data/lib/rubocop/cop/style/struct_inheritance.rb +4 -15
  206. data/lib/rubocop/cop/style/symbol_array.rb +31 -35
  207. data/lib/rubocop/cop/style/symbol_proc.rb +2 -2
  208. data/lib/rubocop/cop/style/ternary_parentheses.rb +41 -13
  209. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +6 -9
  210. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -1
  211. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  212. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -2
  213. data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
  214. data/lib/rubocop/cop/style/word_array.rb +12 -34
  215. data/lib/rubocop/cop/style/zero_length_predicate.rb +11 -4
  216. data/lib/rubocop/cop/team.rb +4 -1
  217. data/lib/rubocop/cop/util.rb +33 -26
  218. data/lib/rubocop/cop/variable_force.rb +13 -13
  219. data/lib/rubocop/cop/variable_force/assignment.rb +1 -8
  220. data/lib/rubocop/cop/variable_force/branch.rb +318 -0
  221. data/lib/rubocop/cop/variable_force/branchable.rb +21 -0
  222. data/lib/rubocop/cop/variable_force/reference.rb +1 -3
  223. data/lib/rubocop/cop/variable_force/scope.rb +36 -20
  224. data/lib/rubocop/cop/variable_force/variable.rb +9 -8
  225. data/lib/rubocop/formatter/colorizable.rb +10 -10
  226. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  227. data/lib/rubocop/formatter/html_formatter.rb +2 -1
  228. data/lib/rubocop/formatter/simple_text_formatter.rb +4 -2
  229. data/lib/rubocop/magic_comment.rb +20 -6
  230. data/lib/rubocop/options.rb +1 -1
  231. data/lib/rubocop/platform.rb +11 -0
  232. data/lib/rubocop/processed_source.rb +1 -1
  233. data/lib/rubocop/remote_config.rb +18 -6
  234. data/lib/rubocop/result_cache.rb +8 -8
  235. data/lib/rubocop/rspec/cop_helper.rb +2 -0
  236. data/lib/rubocop/rspec/shared_contexts.rb +20 -0
  237. data/lib/rubocop/rspec/shared_examples.rb +1 -1
  238. data/lib/rubocop/runner.rb +2 -2
  239. data/lib/rubocop/target_finder.rb +64 -6
  240. data/lib/rubocop/version.rb +2 -4
  241. metadata +27 -4
  242. data/lib/rubocop/cop/variable_force/locatable.rb +0 -200
@@ -57,7 +57,7 @@ module RuboCop
57
57
  PERCENT_I = '%i'.freeze
58
58
  PERCENT_CAPITAL_I = '%I'.freeze
59
59
  ARRAY_NEW_PATTERN = '$(send (const nil :Array) :new ...)'.freeze
60
- ASSIGNMENT_TYPES = [:lvasgn, :ivasgn, :cvasgn, :gvasgn].freeze
60
+ ASSIGNMENT_TYPES = %i(lvasgn ivasgn cvasgn gvasgn).freeze
61
61
 
62
62
  def_node_matcher :literal_expansion?, <<-PATTERN
63
63
  (splat {$({str dstr int float array} ...) (block #{ARRAY_NEW_PATTERN} ...) #{ARRAY_NEW_PATTERN}} ...)
@@ -26,14 +26,15 @@ module RuboCop
26
26
  class UnreachableCode < Cop
27
27
  MSG = 'Unreachable code detected.'.freeze
28
28
 
29
- NODE_TYPES = [:return, :next, :break, :retry, :redo].freeze
30
- FLOW_COMMANDS = [:throw, :raise, :fail].freeze
29
+ NODE_TYPES = %i(return next break retry redo).freeze
30
+ FLOW_COMMANDS = %i(throw raise fail).freeze
31
31
 
32
32
  def on_begin(node)
33
33
  expressions = *node
34
34
 
35
35
  expressions.each_cons(2) do |e1, e2|
36
36
  next unless NODE_TYPES.include?(e1.type) || flow_command?(e1)
37
+
37
38
  add_offense(e2, :expression)
38
39
  end
39
40
  end
@@ -41,6 +42,8 @@ module RuboCop
41
42
  private
42
43
 
43
44
  def flow_command?(node)
45
+ return false unless node.send_type?
46
+
44
47
  FLOW_COMMANDS.any? { |c| node.command?(c) }
45
48
  end
46
49
  end
@@ -7,33 +7,33 @@ module RuboCop
7
7
  #
8
8
  # @example
9
9
  #
10
- # #good
10
+ # # bad
11
11
  #
12
12
  # do_something do |used, unused|
13
13
  # puts used
14
14
  # end
15
15
  #
16
- # do_something do
16
+ # do_something do |bar|
17
17
  # puts :foo
18
18
  # end
19
19
  #
20
- # define_method(:foo) do |_bar|
20
+ # define_method(:foo) do |bar|
21
21
  # puts :baz
22
22
  # end
23
23
  #
24
24
  # @example
25
25
  #
26
- # # bad
26
+ # #good
27
27
  #
28
28
  # do_something do |used, _unused|
29
29
  # puts used
30
30
  # end
31
31
  #
32
- # do_something do |bar|
32
+ # do_something do
33
33
  # puts :foo
34
34
  # end
35
35
  #
36
- # define_method(:foo) do |bar|
36
+ # define_method(:foo) do |_bar|
37
37
  # puts :baz
38
38
  # end
39
39
  class UnusedBlockArgument < Cop
@@ -89,7 +89,8 @@ module RuboCop
89
89
  return unless assignment.meta_assignment_node
90
90
  .equal?(return_value_node)
91
91
 
92
- " Use just operator `#{assignment.operator.sub(/=$/, '')}`."
92
+ " Use `#{assignment.operator.sub(/=$/, '')}` " \
93
+ "instead of `#{assignment.operator}`."
93
94
  end
94
95
 
95
96
  def similar_name_message(variable)
@@ -14,12 +14,13 @@ module RuboCop
14
14
  MSG = 'Comparison of something with itself detected.'.freeze
15
15
  OPS = %w(== === != < > <= >= <=>).freeze
16
16
 
17
- def_node_matcher :comparison?, "(send $_ {:#{OPS.join(' :')}} $_)"
17
+ def_node_matcher :useless_comparison?,
18
+ "(send $_match {:#{OPS.join(' :')}} $_match)"
18
19
 
19
20
  def on_send(node)
20
- comparison?(node) do |receiver, args|
21
- add_offense(node, :selector) if receiver == args
22
- end
21
+ return unless useless_comparison?(node)
22
+
23
+ add_offense(node, :selector)
23
24
  end
24
25
  end
25
26
  end
@@ -28,7 +28,7 @@ module RuboCop
28
28
  include OnMethodDef
29
29
 
30
30
  MSG = 'Useless setter call to local variable `%s`.'.freeze
31
- ASSIGNMENT_TYPES = [:lvasgn, :ivasgn, :cvasgn, :gvasgn].freeze
31
+ ASSIGNMENT_TYPES = %i(lvasgn ivasgn cvasgn gvasgn).freeze
32
32
 
33
33
  private
34
34
 
@@ -28,7 +28,7 @@ module RuboCop
28
28
  # @option cop_config [String] :Reference Full reference URL
29
29
  # @option cop_config [String] :Details
30
30
  #
31
- # @param [Hash] options
31
+ # @param [Hash, nil] optional options
32
32
  # @option option [Boolean] :display_style_guide
33
33
  # Include style guide and reference URLs
34
34
  # @option option [Boolean] :extra_details
@@ -51,12 +51,16 @@ module RuboCop
51
51
  message = "#{name}: #{message}" if display_cop_names?
52
52
  message += " #{details}" if extra_details?
53
53
  if display_style_guide?
54
- links = [style_guide_url, reference_url].compact.join(', ')
54
+ links = urls.join(', ')
55
55
  message = "#{message} (#{links})"
56
56
  end
57
57
  message
58
58
  end
59
59
 
60
+ def urls
61
+ [style_guide_url, reference_url].compact
62
+ end
63
+
60
64
  private
61
65
 
62
66
  def style_guide_url
@@ -70,7 +74,7 @@ module RuboCop
70
74
  end
71
75
 
72
76
  def display_style_guide?
73
- (style_guide_url || reference_url) &&
77
+ !urls.empty? &&
74
78
  (options[:display_style_guide] ||
75
79
  config.for_all_cops['DisplayStyleGuide'])
76
80
  end
@@ -11,7 +11,7 @@ module RuboCop
11
11
 
12
12
  MSG = 'Assignment Branch Condition size for %s is too high. ' \
13
13
  '[%.4g/%.4g]'.freeze
14
- BRANCH_NODES = [:send, :csend].freeze
14
+ BRANCH_NODES = %i(send csend).freeze
15
15
  CONDITION_NODES = CyclomaticComplexity::COUNTED_NODES.freeze
16
16
 
17
17
  private
@@ -14,10 +14,10 @@ module RuboCop
14
14
  class BlockNesting < Cop
15
15
  include ConfigurableMax
16
16
 
17
- NESTING_BLOCKS = [
18
- :case, :if, :while, :while_post,
19
- :until, :until_post, :for, :resbody
20
- ].freeze
17
+ NESTING_BLOCKS = %i(
18
+ case if while while_post
19
+ until until_post for resbody
20
+ ).freeze
21
21
 
22
22
  def investigate(processed_source)
23
23
  return unless processed_source.ast
@@ -17,8 +17,8 @@ module RuboCop
17
17
  include MethodComplexity
18
18
 
19
19
  MSG = 'Cyclomatic complexity for %s is too high. [%d/%d]'.freeze
20
- COUNTED_NODES = [:if, :while, :until, :for,
21
- :rescue, :when, :and, :or].freeze
20
+ COUNTED_NODES = %i(if while until for
21
+ rescue when and or).freeze
22
22
 
23
23
  private
24
24
 
@@ -9,6 +9,7 @@ module RuboCop
9
9
  # The maximum length is configurable.
10
10
  class LineLength < Cop
11
11
  include ConfigurableMax
12
+ include IgnoredPattern
12
13
 
13
14
  MSG = 'Line is too long. [%d/%d]'.freeze
14
15
 
@@ -86,14 +87,6 @@ module RuboCop
86
87
  end
87
88
  end
88
89
 
89
- def matches_ignored_pattern?(line)
90
- ignored_patterns.any? { |pattern| Regexp.new(pattern).match(line) }
91
- end
92
-
93
- def ignored_patterns
94
- cop_config['IgnoredPatterns'] || []
95
- end
96
-
97
90
  def allow_uri?
98
91
  cop_config['AllowURI']
99
92
  end
@@ -159,7 +152,7 @@ module RuboCop
159
152
  end
160
153
 
161
154
  def line_length_without_directive(line)
162
- before_comment, = line.split('#')
155
+ before_comment, = line.split(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
163
156
  before_comment.rstrip.length
164
157
  end
165
158
 
@@ -10,13 +10,14 @@ module RuboCop
10
10
  class ParameterLists < Cop
11
11
  include ConfigurableMax
12
12
 
13
- MSG = 'Avoid parameter lists longer than %d parameters.'.freeze
13
+ MSG = 'Avoid parameter lists longer than %d parameters. [%d/%d]'.freeze
14
14
 
15
15
  def on_args(node)
16
16
  count = args_count(node)
17
17
  return unless count > max_params
18
18
 
19
- add_offense(node, :expression, format(MSG, max_params)) do
19
+ message = format(MSG, max_params, count, max_params)
20
+ add_offense(node, :expression, message) do
20
21
  self.max = count
21
22
  end
22
23
  end
@@ -27,7 +28,7 @@ module RuboCop
27
28
  if count_keyword_args?
28
29
  node.children.size
29
30
  else
30
- node.children.count { |a| ![:kwoptarg, :kwarg].include?(a.type) }
31
+ node.children.count { |a| !%i(kwoptarg kwarg).include?(a.type) }
31
32
  end
32
33
  end
33
34
 
@@ -30,8 +30,8 @@ module RuboCop
30
30
  include MethodComplexity
31
31
 
32
32
  MSG = 'Perceived complexity for %s is too high. [%d/%d]'.freeze
33
- COUNTED_NODES = [:if, :case, :while, :until,
34
- :for, :rescue, :and, :or].freeze
33
+ COUNTED_NODES = %i(if case while until
34
+ for rescue and or).freeze
35
35
 
36
36
  private
37
37
 
@@ -32,7 +32,7 @@ module RuboCop
32
32
  if ancestor.block_type?
33
33
  return true if ancestor.class_constructor?
34
34
  elsif !ancestor.begin_type?
35
- return [:casgn, :sclass, :class, :module].include?(ancestor.type)
35
+ return %i(casgn sclass class module).include?(ancestor.type)
36
36
  end
37
37
  end
38
38
  end
@@ -5,11 +5,11 @@ module RuboCop
5
5
  # Common code for indenting literal arrays and hashes.
6
6
  module ArrayHashIndentation
7
7
  def each_argument_node(node, type)
8
- _receiver, _method_name, *args = *node
9
8
  left_parenthesis = node.loc.begin
9
+
10
10
  return unless left_parenthesis
11
11
 
12
- args.each do |arg|
12
+ node.arguments.each do |arg|
13
13
  on_node(type, arg, :send) do |type_node|
14
14
  left_brace = type_node.loc.begin
15
15
  if left_brace && left_brace.line == left_parenthesis.line
@@ -11,13 +11,13 @@ module RuboCop
11
11
  end
12
12
 
13
13
  def on_send(node)
14
- # we only want to indent relative to the receiver
15
- # when the method called looks like a setter
16
- return unless node.asgn_method_call?
14
+ return unless node.setter_method?
17
15
 
18
- # This will match if, case, begin, blocks, etc.
19
16
  rhs = extract_rhs(node)
20
- check_assignment(node, rhs) if rhs.is_a?(AST::Node)
17
+
18
+ return unless rhs
19
+
20
+ check_assignment(node, rhs)
21
21
  end
22
22
 
23
23
  module_function
@@ -30,7 +30,7 @@ module RuboCop
30
30
  elsif Util::ASGN_NODES.include?(node.type)
31
31
  _lhs, rhs = *node
32
32
  elsif node.send_type?
33
- rhs = node.children.last
33
+ rhs = node.last_argument
34
34
  end
35
35
 
36
36
  rhs
@@ -37,7 +37,7 @@ module RuboCop
37
37
  # element and all duplicate instances.
38
38
  #
39
39
  # @param [Array] collection an array to group duplicates for
40
- # @return [Hash] the grouped duplicates
40
+ # @return [Array] the grouped duplicates
41
41
  def grouped_duplicates(collection)
42
42
  collection.group_by { |item| item }.values.reject(&:one?)
43
43
  end
@@ -8,7 +8,7 @@ module RuboCop
8
8
 
9
9
  FROZEN_STRING_LITERAL = '# frozen_string_literal:'.freeze
10
10
  FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'.freeze
11
- FROZEN_STRING_LITERAL_TYPES = [:str, :dstr].freeze
11
+ FROZEN_STRING_LITERAL_TYPES = %i(str dstr).freeze
12
12
 
13
13
  def frozen_string_literal_comment_exists?
14
14
  leading_comment_lines.any? do |line|
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # This module encapsulates the ability to ignore certain lines when
6
+ # parsing.
7
+ module IgnoredPattern
8
+ def ignored_line?(line)
9
+ line = if line.respond_to?(:source_line)
10
+ line.source_line
11
+ elsif line.respond_to?(:node)
12
+ line.node.source_range.source_line
13
+ end
14
+
15
+ matches_ignored_pattern?(line)
16
+ end
17
+
18
+ def matches_ignored_pattern?(line)
19
+ ignored_patterns.any? { |pattern| Regexp.new(pattern).match(line) }
20
+ end
21
+
22
+ def ignored_patterns
23
+ cop_config['IgnoredPatterns'] || []
24
+ end
25
+ end
26
+ end
27
+ end
@@ -4,6 +4,8 @@ module RuboCop
4
4
  module Cop
5
5
  # Common code for cops that deal with preferred methods.
6
6
  module MethodPreference
7
+ private
8
+
7
9
  def preferred_method(method)
8
10
  preferred_methods[method.to_sym]
9
11
  end
@@ -6,13 +6,10 @@ module RuboCop
6
6
  # operations.
7
7
  module MultilineExpressionIndentation
8
8
  def on_send(node)
9
+ return if !node.receiver || node.method?(:[])
9
10
  return unless relevant_node?(node)
10
11
 
11
- receiver, method_name, *_args = *node
12
- return unless receiver
13
- return if method_name == :[] # Don't check parameters inside [].
14
-
15
- lhs = left_hand_side(receiver)
12
+ lhs = left_hand_side(node.receiver)
16
13
  rhs = right_hand_side(node)
17
14
  range = offending_range(node, lhs, rhs, style)
18
15
  check(range, node, lhs, rhs)
@@ -29,9 +26,8 @@ module RuboCop
29
26
  end
30
27
 
31
28
  def right_hand_side(send_node)
32
- _, method_name, *args = *send_node
33
- if operator?(method_name) && args.any?
34
- args.first.source_range # not used for method calls
29
+ if send_node.operator_method? && send_node.arguments?
30
+ send_node.first_argument.source_range # not used for method calls
35
31
  else
36
32
  regular_method_right_hand_side(send_node)
37
33
  end
@@ -40,12 +36,11 @@ module RuboCop
40
36
  def regular_method_right_hand_side(send_node)
41
37
  dot = send_node.loc.dot
42
38
  selector = send_node.loc.selector
43
- if dot && selector && dot.line == selector.line
39
+ if send_node.dot? && selector && dot.line == selector.line
44
40
  dot.join(selector)
45
41
  elsif selector
46
42
  selector
47
- elsif dot.line == send_node.loc.begin.line
48
- # lambda.(args)
43
+ elsif send_node.implicit_call?
49
44
  dot.join(send_node.loc.begin)
50
45
  end
51
46
  end
@@ -116,9 +111,9 @@ module RuboCop
116
111
  # is an argument in a method call. It doesn't count.
117
112
  break false if a.block_type?
118
113
 
119
- _, method_name, *args = *a
120
- next if assignment_call?(method_name)
121
- args.any? { |arg| within_node?(node, arg) }
114
+ next if a.setter_method?
115
+
116
+ a.arguments.any? { |arg| within_node?(node, arg) }
122
117
  end
123
118
  end
124
119
 
@@ -139,20 +134,14 @@ module RuboCop
139
134
 
140
135
  # The []= operator and setters (a.b = c) are parsed as :send nodes.
141
136
  def valid_method_rhs_candidate?(candidate, node)
142
- _receiver, method_name, *args = *node
143
-
144
- assignment_call?(method_name) &&
145
- valid_rhs_candidate?(candidate, args.last)
137
+ node.setter_method? &&
138
+ valid_rhs_candidate?(candidate, node.last_argument)
146
139
  end
147
140
 
148
141
  def valid_rhs_candidate?(candidate, node)
149
142
  !candidate || within_node?(candidate, node)
150
143
  end
151
144
 
152
- def assignment_call?(method_name)
153
- method_name == :[]= || method_name.to_s =~ /^\w.*=$/
154
- end
155
-
156
145
  def part_of_block_body?(candidate, node)
157
146
  _method, _args, body = *node
158
147
  body && within_node?(candidate, body)
@@ -162,14 +151,14 @@ module RuboCop
162
151
  case node.type
163
152
  when :casgn then _scope, _lhs, rhs = *node
164
153
  when :op_asgn then _lhs, _op, rhs = *node
165
- when :send then _receiver, _method_name, *_args, rhs = *node
154
+ when :send then rhs = node.last_argument
166
155
  else _lhs, rhs = *node
167
156
  end
168
157
  rhs
169
158
  end
170
159
 
171
160
  def not_for_this_cop?(node)
172
- node.each_ancestor.any? do |ancestor|
161
+ node.ancestors.any? do |ancestor|
173
162
  grouped_expression?(ancestor) ||
174
163
  inside_arg_list_parentheses?(node, ancestor)
175
164
  end
@@ -180,11 +169,10 @@ module RuboCop
180
169
  end
181
170
 
182
171
  def inside_arg_list_parentheses?(node, ancestor)
183
- a = ancestor.loc
184
- return false unless ancestor.send_type? && a.begin &&
185
- a.begin.is?('(')
186
- n = node.source_range
187
- n.begin_pos > a.begin.begin_pos && n.end_pos < a.end.end_pos
172
+ return false unless ancestor.send_type? && ancestor.parenthesized?
173
+
174
+ node.source_range.begin_pos > ancestor.loc.begin.begin_pos &&
175
+ node.source_range.end_pos < ancestor.loc.end.end_pos
188
176
  end
189
177
  end
190
178
  end