rubocop 0.42.0 → 0.43.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/assets/output.html.erb +21 -10
  4. data/config/default.yml +32 -2
  5. data/config/disabled.yml +8 -1
  6. data/config/enabled.yml +40 -12
  7. data/lib/rubocop.rb +14 -2
  8. data/lib/rubocop/ast_node.rb +2 -0
  9. data/lib/rubocop/cached_data.rb +13 -11
  10. data/lib/rubocop/cli.rb +5 -5
  11. data/lib/rubocop/config.rb +68 -24
  12. data/lib/rubocop/config_loader.rb +13 -11
  13. data/lib/rubocop/config_loader_resolver.rb +4 -2
  14. data/lib/rubocop/cop/cop.rb +16 -5
  15. data/lib/rubocop/cop/lint/assignment_in_condition.rb +21 -20
  16. data/lib/rubocop/cop/lint/block_alignment.rb +3 -4
  17. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -3
  18. data/lib/rubocop/cop/lint/duplicate_methods.rb +16 -6
  19. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  20. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  21. data/lib/rubocop/cop/lint/end_alignment.rb +4 -6
  22. data/lib/rubocop/cop/lint/eval.rb +1 -1
  23. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  24. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +8 -8
  25. data/lib/rubocop/cop/lint/inherit_exception.rb +22 -7
  26. data/lib/rubocop/cop/lint/literal_in_condition.rb +5 -5
  27. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +3 -5
  28. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  29. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +9 -8
  30. data/lib/rubocop/cop/lint/percent_string_array.rb +17 -6
  31. data/lib/rubocop/cop/lint/percent_symbol_array.rb +4 -4
  32. data/lib/rubocop/cop/lint/rand_one.rb +3 -3
  33. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -3
  34. data/lib/rubocop/cop/lint/shadowed_exception.rb +39 -44
  35. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +2 -2
  36. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -2
  37. data/lib/rubocop/cop/lint/unified_integer.rb +38 -0
  38. data/lib/rubocop/cop/lint/unneeded_disable.rb +51 -38
  39. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +114 -0
  40. data/lib/rubocop/cop/lint/useless_assignment.rb +25 -12
  41. data/lib/rubocop/cop/lint/useless_setter_call.rb +27 -28
  42. data/lib/rubocop/cop/lint/void.rb +2 -4
  43. data/lib/rubocop/cop/mixin/access_modifier_node.rb +5 -5
  44. data/lib/rubocop/cop/mixin/array_hash_indentation.rb +19 -17
  45. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +3 -5
  46. data/lib/rubocop/cop/mixin/configurable_naming.rb +4 -5
  47. data/lib/rubocop/cop/mixin/configurable_numbering.rb +52 -0
  48. data/lib/rubocop/cop/mixin/def_node.rb +28 -0
  49. data/lib/rubocop/cop/mixin/documentation_comment.rb +41 -0
  50. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +18 -13
  51. data/lib/rubocop/cop/mixin/if_node.rb +6 -0
  52. data/lib/rubocop/cop/mixin/match_range.rb +2 -5
  53. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
  54. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +40 -28
  55. data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
  56. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -5
  57. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +14 -4
  58. data/lib/rubocop/cop/mixin/safe_mode.rb +23 -0
  59. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -4
  60. data/lib/rubocop/cop/mixin/space_inside.rb +1 -3
  61. data/lib/rubocop/cop/mixin/statement_modifier.rb +30 -20
  62. data/lib/rubocop/cop/mixin/trailing_comma.rb +19 -17
  63. data/lib/rubocop/cop/performance/case_when_splat.rb +16 -41
  64. data/lib/rubocop/cop/performance/casecmp.rb +28 -16
  65. data/lib/rubocop/cop/performance/count.rb +58 -34
  66. data/lib/rubocop/cop/performance/detect.rb +3 -7
  67. data/lib/rubocop/cop/performance/double_start_end_with.rb +17 -13
  68. data/lib/rubocop/cop/performance/fixed_size.rb +19 -14
  69. data/lib/rubocop/cop/performance/flat_map.rb +16 -9
  70. data/lib/rubocop/cop/performance/hash_each.rb +2 -3
  71. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +4 -6
  72. data/lib/rubocop/cop/performance/redundant_match.rb +4 -1
  73. data/lib/rubocop/cop/performance/redundant_merge.rb +63 -32
  74. data/lib/rubocop/cop/performance/redundant_sort_by.rb +8 -7
  75. data/lib/rubocop/cop/performance/reverse_each.rb +1 -4
  76. data/lib/rubocop/cop/performance/size.rb +21 -8
  77. data/lib/rubocop/cop/performance/sort_with_block.rb +54 -0
  78. data/lib/rubocop/cop/performance/string_replacement.rb +3 -7
  79. data/lib/rubocop/cop/rails/delegate.rb +2 -3
  80. data/lib/rubocop/cop/rails/find_by.rb +4 -8
  81. data/lib/rubocop/cop/rails/not_null_column.rb +45 -0
  82. data/lib/rubocop/cop/rails/request_referer.rb +3 -3
  83. data/lib/rubocop/cop/rails/safe_navigation.rb +89 -0
  84. data/lib/rubocop/cop/rails/save_bang.rb +78 -9
  85. data/lib/rubocop/cop/rails/scope_args.rb +3 -1
  86. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +2 -3
  87. data/lib/rubocop/cop/rails/validation.rb +1 -1
  88. data/lib/rubocop/cop/security/json_load.rb +36 -0
  89. data/lib/rubocop/cop/style/alias.rb +1 -1
  90. data/lib/rubocop/cop/style/align_hash.rb +25 -14
  91. data/lib/rubocop/cop/style/and_or.rb +13 -3
  92. data/lib/rubocop/cop/style/array_join.rb +3 -3
  93. data/lib/rubocop/cop/style/ascii_comments.rb +1 -2
  94. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -2
  95. data/lib/rubocop/cop/style/attr.rb +1 -3
  96. data/lib/rubocop/cop/style/block_comments.rb +2 -6
  97. data/lib/rubocop/cop/style/block_delimiters.rb +35 -21
  98. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +4 -4
  99. data/lib/rubocop/cop/style/case_indentation.rb +1 -3
  100. data/lib/rubocop/cop/style/class_methods.rb +3 -4
  101. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  102. data/lib/rubocop/cop/style/command_literal.rb +15 -8
  103. data/lib/rubocop/cop/style/comment_annotation.rb +1 -2
  104. data/lib/rubocop/cop/style/conditional_assignment.rb +68 -36
  105. data/lib/rubocop/cop/style/copyright.rb +1 -5
  106. data/lib/rubocop/cop/style/def_with_parentheses.rb +3 -5
  107. data/lib/rubocop/cop/style/documentation.rb +28 -56
  108. data/lib/rubocop/cop/style/documentation_method.rb +80 -0
  109. data/lib/rubocop/cop/style/each_for_simple_loop.rb +6 -5
  110. data/lib/rubocop/cop/style/each_with_object.rb +2 -2
  111. data/lib/rubocop/cop/style/else_alignment.rb +10 -9
  112. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -4
  113. data/lib/rubocop/cop/style/empty_else.rb +1 -4
  114. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -3
  115. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -5
  116. data/lib/rubocop/cop/style/encoding.rb +28 -14
  117. data/lib/rubocop/cop/style/even_odd.rb +28 -17
  118. data/lib/rubocop/cop/style/extra_spacing.rb +36 -25
  119. data/lib/rubocop/cop/style/file_name.rb +19 -10
  120. data/lib/rubocop/cop/style/first_parameter_indentation.rb +2 -3
  121. data/lib/rubocop/cop/style/for.rb +12 -8
  122. data/lib/rubocop/cop/style/format_string.rb +1 -1
  123. data/lib/rubocop/cop/style/guard_clause.rb +22 -56
  124. data/lib/rubocop/cop/style/hash_syntax.rb +72 -7
  125. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -19
  126. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -3
  127. data/lib/rubocop/cop/style/indentation_width.rb +30 -16
  128. data/lib/rubocop/cop/style/infinite_loop.rb +16 -13
  129. data/lib/rubocop/cop/style/initial_indentation.rb +23 -18
  130. data/lib/rubocop/cop/style/inline_comment.rb +16 -3
  131. data/lib/rubocop/cop/style/lambda.rb +22 -10
  132. data/lib/rubocop/cop/style/leading_comment_space.rb +12 -1
  133. data/lib/rubocop/cop/style/line_end_concatenation.rb +24 -6
  134. data/lib/rubocop/cop/style/method_call_parentheses.rb +18 -9
  135. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -4
  136. data/lib/rubocop/cop/style/method_def_parentheses.rb +3 -4
  137. data/lib/rubocop/cop/style/method_missing.rb +10 -2
  138. data/lib/rubocop/cop/style/module_function.rb +14 -6
  139. data/lib/rubocop/cop/style/multiline_assignment_layout.rb +2 -5
  140. data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -5
  141. data/lib/rubocop/cop/style/multiline_block_layout.rb +22 -15
  142. data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +9 -0
  143. data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +41 -20
  144. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +6 -6
  145. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +3 -5
  146. data/lib/rubocop/cop/style/mutable_constant.rb +21 -13
  147. data/lib/rubocop/cop/style/negated_if.rb +1 -1
  148. data/lib/rubocop/cop/style/negated_while.rb +3 -3
  149. data/lib/rubocop/cop/style/nested_modifier.rb +2 -4
  150. data/lib/rubocop/cop/style/next.rb +4 -4
  151. data/lib/rubocop/cop/style/non_nil_check.rb +18 -10
  152. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +8 -0
  153. data/lib/rubocop/cop/style/numeric_predicate.rb +9 -9
  154. data/lib/rubocop/cop/style/one_line_conditional.rb +11 -1
  155. data/lib/rubocop/cop/style/op_method.rb +1 -1
  156. data/lib/rubocop/cop/style/option_hash.rb +8 -8
  157. data/lib/rubocop/cop/style/optional_arguments.rb +21 -8
  158. data/lib/rubocop/cop/style/parallel_assignment.rb +51 -35
  159. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  160. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  161. data/lib/rubocop/cop/style/raise_args.rb +2 -2
  162. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  163. data/lib/rubocop/cop/style/redundant_parentheses.rb +26 -15
  164. data/lib/rubocop/cop/style/redundant_return.rb +5 -5
  165. data/lib/rubocop/cop/style/redundant_self.rb +20 -11
  166. data/lib/rubocop/cop/style/regexp_literal.rb +16 -10
  167. data/lib/rubocop/cop/style/rescue_ensure_alignment.rb +8 -6
  168. data/lib/rubocop/cop/style/safe_navigation.rb +125 -0
  169. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  170. data/lib/rubocop/cop/style/semicolon.rb +9 -10
  171. data/lib/rubocop/cop/style/signal_exception.rb +2 -4
  172. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  173. data/lib/rubocop/cop/style/single_line_methods.rb +18 -11
  174. data/lib/rubocop/cop/style/space_after_method_name.rb +2 -3
  175. data/lib/rubocop/cop/style/space_after_not.rb +4 -6
  176. data/lib/rubocop/cop/style/space_around_block_parameters.rb +1 -2
  177. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -3
  178. data/lib/rubocop/cop/style/space_around_operators.rb +21 -16
  179. data/lib/rubocop/cop/style/space_before_block_braces.rb +2 -12
  180. data/lib/rubocop/cop/style/space_before_first_arg.rb +1 -3
  181. data/lib/rubocop/cop/style/space_inside_array_percent_literal.rb +1 -1
  182. data/lib/rubocop/cop/style/space_inside_block_braces.rb +33 -40
  183. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +38 -23
  184. data/lib/rubocop/cop/style/space_inside_percent_literal_delimiters.rb +1 -1
  185. data/lib/rubocop/cop/style/space_inside_string_interpolation.rb +26 -12
  186. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -4
  187. data/lib/rubocop/cop/style/symbol_array.rb +10 -10
  188. data/lib/rubocop/cop/style/symbol_proc.rb +28 -13
  189. data/lib/rubocop/cop/style/ternary_parentheses.rb +35 -5
  190. data/lib/rubocop/cop/style/trailing_blank_lines.rb +2 -4
  191. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +29 -17
  192. data/lib/rubocop/cop/style/trivial_accessors.rb +6 -6
  193. data/lib/rubocop/cop/style/unless_else.rb +2 -6
  194. data/lib/rubocop/cop/style/unneeded_capital_w.rb +8 -4
  195. data/lib/rubocop/cop/style/unneeded_interpolation.rb +4 -5
  196. data/lib/rubocop/cop/style/unneeded_percent_q.rb +13 -7
  197. data/lib/rubocop/cop/style/variable_number.rb +79 -0
  198. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  199. data/lib/rubocop/cop/style/word_array.rb +25 -15
  200. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -0
  201. data/lib/rubocop/cop/util.rb +23 -4
  202. data/lib/rubocop/cop/variable_force.rb +59 -25
  203. data/lib/rubocop/cop/variable_force/locatable.rb +8 -6
  204. data/lib/rubocop/cop/variable_force/variable.rb +2 -2
  205. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
  206. data/lib/rubocop/formatter/disabled_config_formatter.rb +16 -11
  207. data/lib/rubocop/formatter/formatter_set.rb +12 -10
  208. data/lib/rubocop/formatter/worst_offenders_formatter.rb +4 -4
  209. data/lib/rubocop/node_pattern.rb +79 -35
  210. data/lib/rubocop/options.rb +4 -4
  211. data/lib/rubocop/processed_source.rb +9 -5
  212. data/lib/rubocop/remote_config.rb +14 -10
  213. data/lib/rubocop/result_cache.rb +14 -6
  214. data/lib/rubocop/runner.rb +55 -34
  215. data/lib/rubocop/string_util.rb +9 -5
  216. data/lib/rubocop/target_finder.rb +1 -1
  217. data/lib/rubocop/token.rb +1 -1
  218. data/lib/rubocop/version.rb +1 -1
  219. metadata +15 -4
  220. data/lib/rubocop/cop/lint/useless_array_splat.rb +0 -56
  221. data/lib/rubocop/cop/performance/push_splat.rb +0 -47
@@ -14,7 +14,7 @@ module RuboCop
14
14
  return unless receiver.nil? &&
15
15
  method_name == :eval &&
16
16
  !args.empty? &&
17
- args.first.type != :str
17
+ !args.first.str_type?
18
18
  add_offense(node, :selector)
19
19
  end
20
20
  end
@@ -73,7 +73,7 @@ module RuboCop
73
73
 
74
74
  _receiver_node, _method_name, *args = *node
75
75
 
76
- args.butfirst.any? { |arg| arg.type == :splat }
76
+ args.butfirst.any?(&:splat_type?)
77
77
  end
78
78
 
79
79
  def heredoc?(node)
@@ -69,14 +69,14 @@ module RuboCop
69
69
  end
70
70
 
71
71
  def check_node(node)
72
- if node && node.begin_type?
73
- clear
74
- check_scope(node)
75
-
76
- @useless.each do |_name, (defs_node, visibility, modifier)|
77
- add_offense(defs_node, :keyword,
78
- format_message(visibility, modifier))
79
- end
72
+ return unless node && node.begin_type?
73
+
74
+ clear
75
+ check_scope(node)
76
+
77
+ @useless.each do |_name, (defs_node, visibility, modifier)|
78
+ add_offense(defs_node, :keyword,
79
+ format_message(visibility, modifier))
80
80
  end
81
81
  end
82
82
 
@@ -4,9 +4,10 @@
4
4
  module RuboCop
5
5
  module Cop
6
6
  module Lint
7
- # This cop looks for error classes inheriting from `Exception`.
8
- # It is configurable to suggest using either `RuntimeError` (default) or
9
- # `StandardError` instead.
7
+ # This cop looks for error classes inheriting from `Exception`
8
+ # and its standard library subclasses, excluding subclasses of
9
+ # `StandardError`. It is configurable to suggest using either
10
+ # `RuntimeError` (default) or `StandardError` instead.
10
11
  #
11
12
  # @example
12
13
  #
@@ -32,11 +33,24 @@ module RuboCop
32
33
  class InheritException < Cop
33
34
  include ConfigurableEnforcedStyle
34
35
 
35
- MSG = 'Inherit from `%s` instead of `Exception`.'.freeze
36
+ MSG = 'Inherit from `%s` instead of `%s`.'.freeze
36
37
  PREFERRED_BASE_CLASS = {
37
38
  runtime_error: 'RuntimeError',
38
39
  standard_error: 'StandardError'
39
40
  }.freeze
41
+ ILLEGAL_CLASSES = %w(
42
+ Exception
43
+ SystemStackError
44
+ NoMemoryError
45
+ SecurityError
46
+ NotImplementedError
47
+ LoadError
48
+ SyntaxError
49
+ ScriptError
50
+ Interrupt
51
+ SignalException
52
+ SystemExit
53
+ ).freeze
40
54
 
41
55
  def on_class(node)
42
56
  _class, base_class, _body = *node
@@ -49,9 +63,10 @@ module RuboCop
49
63
  private
50
64
 
51
65
  def check(node)
52
- if node.const_name == 'Exception'
53
- add_offense(node, :expression, format(MSG, preferred_base_class))
54
- end
66
+ return unless ILLEGAL_CLASSES.include?(node.const_name)
67
+
68
+ msg = format(MSG, preferred_base_class, node.const_name)
69
+ add_offense(node, :expression, msg)
55
70
  end
56
71
 
57
72
  def autocorrect(node)
@@ -73,7 +73,7 @@ module RuboCop
73
73
  end
74
74
 
75
75
  def not?(node)
76
- return false unless node && node.type == :send
76
+ return false unless node && node.send_type?
77
77
 
78
78
  _receiver, method_name, *_args = *node
79
79
 
@@ -81,7 +81,7 @@ module RuboCop
81
81
  end
82
82
 
83
83
  def basic_literal?(node)
84
- if node && node.type == :array
84
+ if node && node.array_type?
85
85
  primitive_array?(node)
86
86
  else
87
87
  node.basic_literal?
@@ -104,7 +104,7 @@ module RuboCop
104
104
  operands.each do |op|
105
105
  handle_node(op)
106
106
  end
107
- elsif node.type == :begin && node.children.size == 1
107
+ elsif node.begin_type? && node.children.one?
108
108
  child_node = node.children.first
109
109
  handle_node(child_node)
110
110
  end
@@ -119,8 +119,8 @@ module RuboCop
119
119
  end
120
120
 
121
121
  def check_case_cond(node)
122
- return if node.type == :array && !primitive_array?(node)
123
- return if node.type == :dstr
122
+ return if node.array_type? && !primitive_array?(node)
123
+ return if node.dstr_type?
124
124
 
125
125
  handle_node(node)
126
126
  end
@@ -14,7 +14,7 @@ module RuboCop
14
14
  COMPOSITE = [:array, :hash, :pair, :irange, :erange].freeze
15
15
 
16
16
  def on_dstr(node)
17
- node.children.select { |n| n.type == :begin }.each do |begin_node|
17
+ node.each_child_node(:begin) do |begin_node|
18
18
  final_node = begin_node.children.last
19
19
  next unless final_node
20
20
  next if special_keyword?(final_node)
@@ -35,7 +35,7 @@ module RuboCop
35
35
 
36
36
  def special_keyword?(node)
37
37
  # handle strings like __FILE__
38
- (node.type == :str && !node.loc.respond_to?(:begin)) ||
38
+ (node.str_type? && !node.loc.respond_to?(:begin)) ||
39
39
  node.source_range.is?('__LINE__')
40
40
  end
41
41
 
@@ -54,9 +54,7 @@ module RuboCop
54
54
  end_pos =
55
55
  node.loc.end ? node.loc.end.begin_pos : node.loc.expression.end_pos
56
56
 
57
- Parser::Source::Range.new(node.source_range.source_buffer,
58
- node.loc.begin.end_pos,
59
- end_pos).source
57
+ range_between(node.loc.begin.end_pos, end_pos).source
60
58
  end
61
59
 
62
60
  # Does node print its own source when converted to a string?
@@ -38,7 +38,7 @@ module RuboCop
38
38
  private
39
39
 
40
40
  def parent_block_node(node)
41
- node.each_ancestor.find { |n| n.type == :block }
41
+ node.each_ancestor(:block).first
42
42
  end
43
43
  end
44
44
  end
@@ -15,19 +15,16 @@ module RuboCop
15
15
 
16
16
  def on_send(node)
17
17
  _receiver, method_name, *args = *node
18
- return if args.size > 1
18
+ return unless args.one?
19
19
  return if operator?(method_name) || node.asgn_method_call?
20
- return unless args.first && args.first.source.start_with?('(')
20
+
21
+ first_arg = args.first
22
+ return unless first_arg.source.start_with?('(')
21
23
 
22
24
  space_length = spaces_before_left_parenthesis(node)
23
25
  return unless space_length > 0
24
- expr = args.first.source_range
25
- space_range =
26
- Parser::Source::Range.new(expr.source_buffer,
27
- expr.begin_pos - space_length,
28
- expr.begin_pos)
29
26
 
30
- add_offense(nil, space_range)
27
+ add_offense(nil, space_range(first_arg.source_range, space_length))
31
28
  end
32
29
 
33
30
  private
@@ -47,6 +44,10 @@ module RuboCop
47
44
  match = without_receiver.match(/^\s*\.?\s*#{method_regexp}(\s+)\(/)
48
45
  match ? match.captures[0].length : 0
49
46
  end
47
+
48
+ def space_range(expr, space_length)
49
+ range_between(expr.begin_pos - space_length, expr.begin_pos)
50
+ end
50
51
  end
51
52
  end
52
53
  end
@@ -18,13 +18,13 @@ module RuboCop
18
18
  'unwanted in the resulting strings.'.freeze
19
19
 
20
20
  def on_array(node)
21
- process(node, *%w(%w %W))
21
+ process(node, '%w', '%W')
22
22
  end
23
23
 
24
24
  def on_percent_literal(node)
25
- if contains_quotes_or_commas?(node)
26
- add_offense(node, :expression, MSG)
27
- end
25
+ return unless contains_quotes_or_commas?(node)
26
+
27
+ add_offense(node, :expression, MSG)
28
28
  end
29
29
 
30
30
  private
@@ -33,10 +33,10 @@ module RuboCop
33
33
  patterns = [/,$/, /^'.*'$/, /^".*"$/]
34
34
 
35
35
  node.children.any? do |child|
36
- literal = child.children.first
36
+ literal = scrub_string(child.children.first.to_s)
37
37
 
38
38
  # To avoid likely false positives (e.g. a single ' or ")
39
- next if literal.to_s.gsub(/[^\p{Alnum}]/, '').empty?
39
+ next if literal.gsub(/[^\p{Alnum}]/, '').empty?
40
40
 
41
41
  patterns.any? { |pat| literal =~ pat }
42
42
  end
@@ -54,6 +54,17 @@ module RuboCop
54
54
  end
55
55
  end
56
56
  end
57
+
58
+ def scrub_string(string)
59
+ if string.respond_to?(:scrub)
60
+ string.scrub
61
+ else
62
+ string
63
+ .encode('UTF-16BE', 'UTF-8',
64
+ invalid: :replace, undef: :replace, replace: '?')
65
+ .encode('UTF-8')
66
+ end
67
+ end
57
68
  end
58
69
  end
59
70
  end
@@ -18,13 +18,13 @@ module RuboCop
18
18
  'unwanted in the resulting symbols.'.freeze
19
19
 
20
20
  def on_array(node)
21
- process(node, *%w(%i %I))
21
+ process(node, '%i', '%I')
22
22
  end
23
23
 
24
24
  def on_percent_literal(node)
25
- if contains_colons_or_commas?(node)
26
- add_offense(node, :expression, MSG)
27
- end
25
+ return unless contains_colons_or_commas?(node)
26
+
27
+ add_offense(node, :expression, MSG)
28
28
  end
29
29
 
30
30
  private
@@ -26,9 +26,9 @@ module RuboCop
26
26
  PATTERN
27
27
 
28
28
  def on_send(node)
29
- if rand_one?(node)
30
- add_offense(node, :expression, format(MSG, node.source))
31
- end
29
+ return unless rand_one?(node)
30
+
31
+ add_offense(node, :expression, format(MSG, node.source))
32
32
  end
33
33
  end
34
34
  end
@@ -46,9 +46,7 @@ module RuboCop
46
46
  return unless offense?(condition)
47
47
 
48
48
  expr = node.source_range
49
- range = Parser::Source::Range.new(expr.source_buffer,
50
- expr.begin_pos,
51
- condition.source_range.end_pos)
49
+ range = range_between(expr.begin_pos, condition.source_range.end_pos)
52
50
  add_offense(range, range)
53
51
  end
54
52
 
@@ -8,24 +8,25 @@ module RuboCop
8
8
  # less specific exception being rescued before a more specific
9
9
  # exception is rescued.
10
10
  #
11
- # @example
12
- # # bad
13
- # begin
14
- # something
15
- # rescue Exception
16
- # handle_exception
17
- # rescue StandardError
18
- # handle_standard_error
19
- # end
11
+ # @example
20
12
  #
21
- # #good
22
- # begin
23
- # something
24
- # rescue StandardError
25
- # handle_standard_error
26
- # rescue Exception
27
- # handle_exception
28
- # end
13
+ # # bad
14
+ # begin
15
+ # something
16
+ # rescue Exception
17
+ # handle_exception
18
+ # rescue StandardError
19
+ # handle_standard_error
20
+ # end
21
+ #
22
+ # # good
23
+ # begin
24
+ # something
25
+ # rescue StandardError
26
+ # handle_standard_error
27
+ # rescue Exception
28
+ # handle_exception
29
+ # end
29
30
  class ShadowedException < Cop
30
31
  MSG = 'Do not shadow rescued Exceptions'.freeze
31
32
 
@@ -43,20 +44,25 @@ module RuboCop
43
44
  end
44
45
 
45
46
  return if !rescue_group_rescues_multiple_levels &&
46
- rescued_groups == sort_rescued_groups(rescued_groups)
47
+ sorted?(rescued_groups)
47
48
 
48
- add_offense(node, offense_range(node, rescues))
49
+ add_offense(node, offense_range(rescues))
49
50
  end
50
51
 
51
52
  private
52
53
 
53
- def offense_range(node, rescues)
54
+ def offense_range(rescues)
54
55
  first_rescue = rescues.first
55
56
  last_rescue = rescues.last
56
57
  last_exceptions, = *last_rescue
57
- Parser::Source::Range.new(node.loc.expression.source_buffer,
58
- first_rescue.loc.expression.begin_pos,
59
- last_exceptions.loc.expression.end_pos)
58
+ # last_rescue clause may not specify exception class
59
+ end_pos = if last_exceptions
60
+ last_exceptions.loc.expression.end_pos
61
+ else
62
+ last_rescue.loc.keyword.end_pos
63
+ end
64
+
65
+ range_between(first_rescue.loc.expression.begin_pos, end_pos)
60
66
  end
61
67
 
62
68
  def rescue_modifier?(node)
@@ -72,20 +78,7 @@ module RuboCop
72
78
  return !(group.size == 2 && group.include?(NilClass))
73
79
  end
74
80
 
75
- group.any? do |exception|
76
- higher_exception = false
77
- group.each_with_index do |_e, i|
78
- higher_exception ||= begin
79
- if group[i].nil? || exception.nil?
80
- false
81
- else
82
- group[i] < exception
83
- end
84
- end
85
- end
86
-
87
- higher_exception
88
- end
81
+ group.combination(2).any? { |a, b| a && b && a <=> b }
89
82
  end
90
83
 
91
84
  def evaluate_exceptions(rescue_group)
@@ -104,16 +97,18 @@ module RuboCop
104
97
  end
105
98
  end
106
99
 
107
- def sort_rescued_groups(groups)
108
- groups.sort do |x, y|
100
+ def sorted?(rescued_groups)
101
+ rescued_groups.each_cons(2).all? do |x, y|
109
102
  if x.include?(Exception)
110
- 1
103
+ false
111
104
  elsif y.include?(Exception)
112
- -1
113
- elsif x.empty? || y.empty?
114
- 0
105
+ true
106
+ elsif x.none? || y.none?
107
+ # consider sorted if a group is empty or only contains
108
+ # `nil`s
109
+ true
115
110
  else
116
- x <=> y || 0
111
+ (x <=> y || 0) <= 0
117
112
  end
118
113
  end
119
114
  end
@@ -16,9 +16,9 @@ module RuboCop
16
16
  'interpolation.'.freeze
17
17
 
18
18
  def on_dstr(node)
19
- node.children.select { |n| n.type == :begin }.each do |begin_node|
19
+ node.each_child_node(:begin) do |begin_node|
20
20
  final_node = begin_node.children.last
21
- next unless final_node && final_node.type == :send
21
+ next unless final_node && final_node.send_type?
22
22
 
23
23
  receiver, method_name, *args = *final_node
24
24
  next unless method_name == :to_s && args.empty?
@@ -21,12 +21,11 @@ module RuboCop
21
21
 
22
22
  def check_variable(variable)
23
23
  return unless variable.should_be_unused?
24
- return if variable.references.empty?
25
24
  return if variable.references.none?(&:explicit?)
26
25
 
27
26
  node = variable.declaration_node
28
27
 
29
- location = if node.type == :match_with_lvasgn
28
+ location = if node.match_with_lvasgn_type?
30
29
  node.children.first.source_range
31
30
  else
32
31
  node.loc.name