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
@@ -81,6 +81,7 @@ module RuboCop
81
81
  end
82
82
 
83
83
  def simple_if_without_break?(node)
84
+ return false unless node
84
85
  return false unless if_without_else?(node)
85
86
  return false if style == :skip_modifier_ifs && modifier_if?(node)
86
87
  return false if !modifier_if?(node) && !min_body_length?(node)
@@ -155,9 +156,8 @@ module RuboCop
155
156
  else
156
157
  cond.source_range.end_pos
157
158
  end
158
- Parser::Source::Range.new(node.source_range.source_buffer,
159
- node.source_range.begin_pos,
160
- end_pos)
159
+
160
+ range_between(node.source_range.begin_pos, end_pos)
161
161
  end
162
162
 
163
163
  def end_range(node)
@@ -167,7 +167,7 @@ module RuboCop
167
167
  begin_pos -= 1 if end_followed_by_whitespace_only?(source_buffer,
168
168
  end_pos)
169
169
 
170
- Parser::Source::Range.new(source_buffer, begin_pos, end_pos)
170
+ range_between(begin_pos, end_pos)
171
171
  end
172
172
 
173
173
  def end_followed_by_whitespace_only?(source_buffer, end_pos)
@@ -33,15 +33,24 @@ module RuboCop
33
33
  return if ignored_node?(node)
34
34
  receiver, method, args = *node
35
35
 
36
- if method == :!=
37
- add_offense(node, :selector) if args == NIL_NODE
38
- elsif method == :! && include_semantic_changes?
39
- add_offense(node, :expression) if nil_check?(receiver)
36
+ if not_equal_to_nil?(method, args)
37
+ add_offense(node, :selector)
38
+ elsif include_semantic_changes? &&
39
+ not_and_nil_check?(method, receiver)
40
+ add_offense(node, :expression)
40
41
  end
41
42
  end
42
43
 
43
44
  private
44
45
 
46
+ def not_equal_to_nil?(method, args)
47
+ method == :!= && args == NIL_NODE
48
+ end
49
+
50
+ def not_and_nil_check?(method, receiver)
51
+ method == :! && nil_check?(receiver)
52
+ end
53
+
45
54
  def message(node)
46
55
  _receiver, method, _args = *node
47
56
  if method == :!=
@@ -57,18 +66,17 @@ module RuboCop
57
66
 
58
67
  def on_method_def(_node, name, _args, body)
59
68
  # only predicate methods are handled differently
60
- return unless name.to_s.end_with?('?')
61
- return unless body
69
+ return unless name.to_s.end_with?('?') && body
62
70
 
63
- if body.type != :begin
64
- ignore_node(body)
65
- elsif body.type == :begin
71
+ if body.begin_type?
66
72
  ignore_node(body.children.last)
73
+ else
74
+ ignore_node(body)
67
75
  end
68
76
  end
69
77
 
70
78
  def nil_check?(node)
71
- return false unless node && node.type == :send
79
+ return false unless node && node.send_type?
72
80
 
73
81
  _receiver, method, *_args = *node
74
82
  method == :nil?
@@ -47,12 +47,20 @@ module RuboCop
47
47
  def literal_type(node)
48
48
  literal = integer_part(node)
49
49
 
50
+ octal_literal_type(literal) || hex_bin_dec_literal_type(literal)
51
+ end
52
+
53
+ def octal_literal_type(literal)
54
+ # rubocop:disable Style/GuardClause
50
55
  if literal =~ OCTAL_ZERO_ONLY_REGEX && octal_zero_only?
51
56
  return :octal_zero_only
52
57
  elsif literal =~ OCTAL_REGEX && !octal_zero_only?
53
58
  return :octal
54
59
  end
60
+ # rubocop:enable Style/GuardClause
61
+ end
55
62
 
63
+ def hex_bin_dec_literal_type(literal)
56
64
  case literal
57
65
  when HEX_REGEX
58
66
  :hex
@@ -4,8 +4,8 @@
4
4
  module RuboCop
5
5
  module Cop
6
6
  module Style
7
- # This cop checks for using of comparison operators (`==`, `!=`,
8
- # `>`, `<`), to test numbers as zero, nonzero, positive, or negative.
7
+ # This cop checks for usage of comparison operators (`==`, `!=`,
8
+ # `>`, `<`) to test numbers as zero, nonzero, positive, or negative.
9
9
  # These can be replaced by their respective predicate methods.
10
10
  # The cop can also be configured to do the reverse.
11
11
  #
@@ -59,10 +59,10 @@ module RuboCop
59
59
  def on_send(node)
60
60
  numeric, replacement = check(node)
61
61
 
62
- if numeric
63
- add_offense(node, node.loc.expression,
64
- format(MSG, replacement, node.source))
65
- end
62
+ return unless numeric
63
+
64
+ add_offense(node, node.loc.expression,
65
+ format(MSG, replacement, node.source))
66
66
  end
67
67
 
68
68
  private
@@ -75,9 +75,9 @@ module RuboCop
75
75
  predicate(node)
76
76
  end
77
77
 
78
- if numeric && operator && replacement_supported?(operator)
79
- return numeric, replacement(numeric, operator)
80
- end
78
+ return unless numeric && operator && replacement_supported?(operator)
79
+
80
+ [numeric, replacement(numeric, operator)]
81
81
  end
82
82
 
83
83
  def autocorrect(node)
@@ -15,8 +15,11 @@ module RuboCop
15
15
  def on_normal_if_unless(node)
16
16
  exp = node.source
17
17
  return if exp.include?("\n")
18
- return unless node.loc.respond_to?(:else) && node.loc.else
18
+
19
19
  condition = exp.include?('if') ? 'if' : 'unless'
20
+ return unless node.loc.respond_to?(:else) &&
21
+ node.loc.else &&
22
+ else_branch_present?(node, condition)
20
23
 
21
24
  add_offense(node, :expression, format(MSG, condition))
22
25
  end
@@ -70,6 +73,13 @@ module RuboCop
70
73
 
71
74
  !parenthesized_call?(node)
72
75
  end
76
+
77
+ private
78
+
79
+ def else_branch_present?(node, condition)
80
+ _, if_branch, else_branch = *node
81
+ condition == 'if' ? else_branch : if_branch
82
+ end
73
83
  end
74
84
  end
75
85
  end
@@ -31,7 +31,7 @@ module RuboCop
31
31
  args.children.one? &&
32
32
  !TARGET_ARGS.include?(args)
33
33
 
34
- add_offense(args.children[0], :expression, format(MSG, name))
34
+ add_offense(args.children.first, :expression, format(MSG, name))
35
35
  end
36
36
 
37
37
  def op_method?(name)
@@ -48,14 +48,14 @@ module RuboCop
48
48
  end
49
49
 
50
50
  def validate_config
51
- if target_ruby_version < 2.0
52
- raise ValidationError, 'The `Style/OptionHash` cop is only ' \
53
- 'compatible with Ruby 2.0 and up, but the ' \
54
- 'target Ruby version for your project is ' \
55
- "1.9.\nPlease disable this cop or adjust " \
56
- 'the `TargetRubyVersion` parameter in your ' \
57
- 'configuration.'
58
- end
51
+ return unless target_ruby_version < 2.0
52
+
53
+ raise ValidationError, 'The `Style/OptionHash` cop is only ' \
54
+ 'compatible with Ruby 2.0 and up, but the ' \
55
+ 'target Ruby version for your project is ' \
56
+ "1.9.\nPlease disable this cop or adjust " \
57
+ 'the `TargetRubyVersion` parameter in your ' \
58
+ 'configuration.'
59
59
  end
60
60
 
61
61
  private
@@ -25,25 +25,38 @@ module RuboCop
25
25
  def on_def(node)
26
26
  _method, arguments, = *node
27
27
  arguments = *arguments
28
- optarg_positions = []
29
- arg_positions = []
30
28
 
31
- arguments.each_with_index do |argument, index|
32
- optarg_positions << index if argument.optarg_type?
33
- arg_positions << index if argument.arg_type?
29
+ each_misplaced_optional_arg(arguments) do |argument|
30
+ arg, = *argument
31
+ add_offense(argument, :expression, format(MSG, arg))
34
32
  end
33
+ end
34
+
35
+ private
35
36
 
37
+ def each_misplaced_optional_arg(arguments)
38
+ optarg_positions, arg_positions = argument_positions(arguments)
36
39
  return if optarg_positions.empty? || arg_positions.empty?
37
40
 
38
41
  optarg_positions.each do |optarg_position|
39
42
  # there can only be one group of optional arguments
40
43
  break if optarg_position > arg_positions.max
41
- argument = arguments[optarg_position]
42
- arg, = *argument
43
44
 
44
- add_offense(argument, :expression, format(MSG, arg))
45
+ yield arguments[optarg_position]
45
46
  end
46
47
  end
48
+
49
+ def argument_positions(arguments)
50
+ optarg_positions = []
51
+ arg_positions = []
52
+
53
+ arguments.each_with_index do |argument, index|
54
+ optarg_positions << index if argument.optarg_type?
55
+ arg_positions << index if argument.arg_type?
56
+ end
57
+
58
+ [optarg_positions, arg_positions]
59
+ end
47
60
  end
48
61
  end
49
62
  end
@@ -29,30 +29,43 @@ module RuboCop
29
29
  MSG = 'Do not use parallel assignment.'.freeze
30
30
 
31
31
  def on_masgn(node)
32
- left, right = *node
33
- left_elements = *left
34
- right_elements = [*right].compact # edge case for one constant
32
+ lhs, rhs = *node
33
+ lhs_elements = *lhs
34
+ rhs_elements = [*rhs].compact # edge case for one constant
35
35
 
36
- # only complain when the number of variables matches
37
- return if left_elements.size != right_elements.size
36
+ return if allowed_lhs?(lhs) || allowed_rhs?(rhs) ||
37
+ allowed_masign?(lhs_elements, rhs_elements)
38
38
 
39
- # account for edge cases using one variable with a comma
40
- return if left_elements.size == 1
39
+ add_offense(node, :expression)
40
+ end
41
+
42
+ private
43
+
44
+ def allowed_masign?(lhs_elements, rhs_elements)
45
+ lhs_elements.size != rhs_elements.size ||
46
+ !find_valid_order(lhs_elements, rhs_elements)
47
+ end
41
48
 
42
- # account for edge case of Constant::CONSTANT
43
- return unless right.array_type?
49
+ def allowed_lhs?(node)
50
+ elements = *node
44
51
 
45
- # allow mass assignment as the return of a method call
46
- return if right.block_type? || right.send_type?
52
+ # Account for edge cases using one variable with a comma
53
+ # E.g.: `foo, = *bar`
54
+ elements.one? || elements.any?(&:splat_type?)
55
+ end
47
56
 
48
- # allow mass assignment when using splat
49
- return if (left_elements + right_elements).any?(&:splat_type?)
57
+ def allowed_rhs?(node)
58
+ # Edge case for one constant
59
+ elements = [*node].compact
50
60
 
51
- order = find_valid_order(left_elements, right_elements)
52
- # For `a, b = b, a` or similar, there is no valid order
53
- return if order.nil?
61
+ # Account for edge case of `Constant::CONSTANT`
62
+ !node.array_type? ||
63
+ return_of_method_call?(node) ||
64
+ elements.any?(&:splat_type?)
65
+ end
54
66
 
55
- add_offense(node, :expression)
67
+ def return_of_method_call?(node)
68
+ node.block_type? || node.send_type?
56
69
  end
57
70
 
58
71
  def autocorrect(node)
@@ -61,22 +74,22 @@ module RuboCop
61
74
  left_elements = *left
62
75
  right_elements = [*right].compact
63
76
  order = find_valid_order(left_elements, right_elements)
77
+ correction = assignment_corrector(node, order)
64
78
 
65
- assignment_corrector =
66
- if modifier_statement?(node.parent)
67
- ModifierCorrector.new(node, config, order)
68
- elsif rescue_modifier?(node.parent)
69
- RescueCorrector.new(node, config, order)
70
- else
71
- GenericCorrector.new(node, config, order)
72
- end
73
-
74
- corrector.replace(assignment_corrector.correction_range,
75
- assignment_corrector.correction)
79
+ corrector.replace(correction.correction_range,
80
+ correction.correction)
76
81
  end
77
82
  end
78
83
 
79
- private
84
+ def assignment_corrector(node, order)
85
+ if modifier_statement?(node.parent)
86
+ ModifierCorrector.new(node, config, order)
87
+ elsif rescue_modifier?(node.parent)
88
+ RescueCorrector.new(node, config, order)
89
+ else
90
+ GenericCorrector.new(node, config, order)
91
+ end
92
+ end
80
93
 
81
94
  def find_valid_order(left_elements, right_elements)
82
95
  # arrange left_elements in an order such that no corresponding right
@@ -247,12 +260,7 @@ module RuboCop
247
260
  def correction
248
261
  parent = node.parent
249
262
 
250
- modifier_range =
251
- Parser::Source::Range.new(parent.source_range.source_buffer,
252
- parent.loc.keyword.begin_pos,
253
- parent.source_range.end_pos)
254
-
255
- "#{modifier_range.source}\n" \
263
+ "#{modifier_range(parent).source}\n" \
256
264
  "#{indentation(node)}" \
257
265
  "#{assignment.join("\n#{indentation(node)}")}" \
258
266
  "\n#{offset(node)}end"
@@ -261,6 +269,14 @@ module RuboCop
261
269
  def correction_range
262
270
  node.parent.source_range
263
271
  end
272
+
273
+ private
274
+
275
+ def modifier_range(node)
276
+ Parser::Source::Range.new(node.source_range.source_buffer,
277
+ node.loc.keyword.begin_pos,
278
+ node.source_range.end_pos)
279
+ end
264
280
  end
265
281
  end
266
282
  end
@@ -29,7 +29,7 @@ module RuboCop
29
29
  def process_control_op(node)
30
30
  cond, _body = *node
31
31
 
32
- return unless cond.type == :begin
32
+ return unless cond.begin_type?
33
33
  # handle `if (something rescue something_else) ...`
34
34
  return if modifier_op?(cond.children.first)
35
35
  # check if there's any whitespace between the keyword and the cond
@@ -42,7 +42,7 @@ module RuboCop
42
42
 
43
43
  def modifier_op?(node)
44
44
  return false if ternary?(node)
45
- return true if node.type == :rescue
45
+ return true if node.rescue_type?
46
46
 
47
47
  [:if, :while, :until].include?(node.type) &&
48
48
  node.loc.end.nil?
@@ -76,7 +76,7 @@ module RuboCop
76
76
  def string_source(node)
77
77
  if node.is_a?(String)
78
78
  node
79
- elsif node.respond_to?(:type) && node.type == :str
79
+ elsif node.respond_to?(:type) && node.str_type?
80
80
  node.source
81
81
  end
82
82
  end
@@ -106,10 +106,10 @@ module RuboCop
106
106
  def check_exploded(node)
107
107
  _receiver, selector, *args = *node
108
108
 
109
- if args.size == 1
109
+ if args.one?
110
110
  arg, = *args
111
111
 
112
- if arg.type == :send && arg.loc.selector.is?('new')
112
+ if arg.send_type? && arg.loc.selector.is?('new')
113
113
  _receiver, _selector, *constructor_args = *arg
114
114
 
115
115
  # Allow code like `raise Ex.new(arg1, arg2)`.
@@ -31,7 +31,7 @@ module RuboCop
31
31
  MSG = 'Redundant `begin` block detected.'.freeze
32
32
 
33
33
  def on_method_def(_node, _method_name, _args, body)
34
- return unless body && body.type == :kwbegin
34
+ return unless body && body.kwbegin_type?
35
35
 
36
36
  add_offense(body, :begin)
37
37
  end
@@ -32,31 +32,33 @@ module RuboCop
32
32
  end
33
33
 
34
34
  def parens_allowed?(node)
35
- # don't flag `()`
36
35
  empty_parentheses?(node) ||
37
- # don't flag `break(1)`, etc
38
- (keyword_ancestor?(node) && parens_required?(node)) ||
39
- # don't flag `method ({key: value})`
36
+ allowed_ancestor?(node) ||
40
37
  hash_literal_as_first_arg?(node) ||
41
- # don't flag `rescue(ExceptionClass)`
42
38
  rescue?(node) ||
43
- # don't flag `method (arg) { }`
44
- (arg_in_call_with_block?(node) && !parentheses?(node.parent)) ||
45
- # don't flag
46
- # ```
47
- # { a: (1
48
- # ), }
49
- # ```
39
+ allowed_method_call?(node) ||
50
40
  allowed_array_or_hash_element?(node)
51
41
  end
52
42
 
43
+ def allowed_ancestor?(node)
44
+ # Don't flag `break(1)`, etc
45
+ keyword_ancestor?(node) && parens_required?(node)
46
+ end
47
+
48
+ def allowed_method_call?(node)
49
+ # Don't flag `method (arg) { }`
50
+ arg_in_call_with_block?(node) && !parentheses?(node.parent)
51
+ end
52
+
53
53
  def empty_parentheses?(node)
54
+ # Don't flag `()`
54
55
  node.children.empty?
55
56
  end
56
57
 
57
58
  def hash_literal_as_first_arg?(node)
58
- child = node.children.first
59
- child.hash_type? && first_arg?(node) && !parentheses?(node.parent)
59
+ # Don't flag `method ({key: value})`
60
+ node.children.first.hash_type? && first_arg?(node) &&
61
+ !parentheses?(node.parent)
60
62
  end
61
63
 
62
64
  def check(begin_node)
@@ -100,6 +102,11 @@ module RuboCop
100
102
  end
101
103
 
102
104
  def allowed_array_or_hash_element?(node)
105
+ # Don't flag
106
+ # ```
107
+ # { a: (1
108
+ # ), }
109
+ # ```
103
110
  (hash_element?(node) || array_element?(node)) &&
104
111
  only_closing_paren_before_comma?(node)
105
112
  end
@@ -129,7 +136,7 @@ module RuboCop
129
136
 
130
137
  args = *node
131
138
 
132
- if args.size == 1 && args.first && args.first.begin_type?
139
+ if only_begin_arg?(args)
133
140
  parentheses?(args.first)
134
141
  else
135
142
  args.empty? || parentheses?(node)
@@ -146,6 +153,10 @@ module RuboCop
146
153
  args.empty? || parentheses?(send_node) || square_brackets?(send_node)
147
154
  end
148
155
 
156
+ def only_begin_arg?(args)
157
+ args.size == 1 && args.first && args.first.begin_type?
158
+ end
159
+
149
160
  def first_arg?(node)
150
161
  send_node = node.parent
151
162
  return false unless send_node && send_node.send_type?