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
@@ -29,12 +29,11 @@ module RuboCop
29
29
  def on_send(node)
30
30
  return if ignored_node?(node)
31
31
  receiver, _method_name, *_args = *node
32
- return unless receiver && receiver.type == :block &&
32
+ return unless receiver && receiver.block_type? &&
33
33
  receiver.loc.end.is?('end')
34
34
 
35
- range = Parser::Source::Range.new(receiver.loc.end.source_buffer,
36
- receiver.loc.end.begin_pos,
37
- node.source_range.end_pos)
35
+ range = range_between(receiver.loc.end.begin_pos,
36
+ node.source_range.end_pos)
38
37
  add_offense(nil, range)
39
38
  end
40
39
  end
@@ -33,9 +33,8 @@ module RuboCop
33
33
  else
34
34
  args_expr = args_node(node).source_range
35
35
  args_with_space = range_with_surrounding_space(args_expr, :left)
36
- just_space = Parser::Source::Range.new(args_expr.source_buffer,
37
- args_with_space.begin_pos,
38
- args_expr.begin_pos)
36
+ just_space = range_between(args_with_space.begin_pos,
37
+ args_expr.begin_pos)
39
38
  corrector.replace(just_space, '(')
40
39
  corrector.insert_after(args_expr, ')')
41
40
  end
@@ -68,7 +67,7 @@ module RuboCop
68
67
  end
69
68
 
70
69
  def args_node(def_node)
71
- if def_node.type == :def
70
+ if def_node.def_type?
72
71
  _method_name, args, _body = *def_node
73
72
  else
74
73
  _scope, _method_name, args, _body = *def_node
@@ -60,14 +60,22 @@ module RuboCop
60
60
  end
61
61
 
62
62
  def implements_respond_to_missing?(node)
63
- node.parent.children.any? do |sibling|
64
- respond_to_missing_def?(sibling)
63
+ node.parent.each_child_node(node.type).any? do |sibling|
64
+ if node.def_type?
65
+ respond_to_missing_def?(sibling)
66
+ elsif node.defs_type?
67
+ respond_to_missing_defs?(sibling)
68
+ end
65
69
  end
66
70
  end
67
71
 
68
72
  def_node_matcher :respond_to_missing_def?, <<-PATTERN
69
73
  (def :respond_to_missing? (...) ...)
70
74
  PATTERN
75
+
76
+ def_node_matcher :respond_to_missing_defs?, <<-PATTERN
77
+ (defs (self) :respond_to_missing? (...) ...)
78
+ PATTERN
71
79
  end
72
80
  end
73
81
  end
@@ -38,13 +38,21 @@ module RuboCop
38
38
 
39
39
  def on_module(node)
40
40
  _name, body = *node
41
- return unless body && body.type == :begin
41
+ return unless body && body.begin_type?
42
42
 
43
- body.children.each do |body_node|
44
- if style == :module_function && body_node == EXTEND_SELF_NODE
45
- add_offense(body_node, :expression, MODULE_FUNCTION_MSG)
46
- elsif style == :extend_self && body_node == MODULE_FUNCTION_NODE
47
- add_offense(body_node, :expression, EXTEND_SELF_MSG)
43
+ each_wrong_style(body.children) do |child_node, msg|
44
+ add_offense(child_node, :expression, msg)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def each_wrong_style(nodes)
51
+ nodes.each do |node|
52
+ if style == :module_function && node == EXTEND_SELF_NODE
53
+ yield node, MODULE_FUNCTION_MSG
54
+ elsif style == :extend_self && node == MODULE_FUNCTION_NODE
55
+ yield node, EXTEND_SELF_MSG
48
56
  end
49
57
  end
50
58
  end
@@ -71,11 +71,8 @@ module RuboCop
71
71
  when :new_line
72
72
  ->(corrector) { corrector.insert_after(node.loc.operator, "\n") }
73
73
  when :same_line
74
- range = Parser::Source::Range.new(
75
- node.source_range.source_buffer,
76
- node.loc.operator.end_pos,
77
- extract_rhs(node).source_range.begin_pos
78
- )
74
+ range = range_between(node.loc.operator.end_pos,
75
+ extract_rhs(node).source_range.begin_pos)
79
76
 
80
77
  ->(corrector) { corrector.replace(range, ' ') }
81
78
  end
@@ -21,7 +21,7 @@ module RuboCop
21
21
  method, _args, _body = *node
22
22
  method.each_node(:send) do |send_node|
23
23
  receiver, _method_name, *_args = *send_node
24
- next unless receiver && receiver.type == :block
24
+ next unless receiver && receiver.block_type?
25
25
 
26
26
  # The begin and end could also be braces, but we call the
27
27
  # variables do... and end...
@@ -29,10 +29,8 @@ module RuboCop
29
29
  end_kw_loc = receiver.loc.end
30
30
  next if do_kw_loc.line == end_kw_loc.line
31
31
 
32
- range =
33
- Parser::Source::Range.new(end_kw_loc.source_buffer,
34
- end_kw_loc.begin_pos,
35
- method.source_range.end_pos)
32
+ range = range_between(end_kw_loc.begin_pos,
33
+ method.source_range.end_pos)
36
34
  add_offense(nil, range)
37
35
  # Done. If there are more blocks in the chain, they will be
38
36
  # found by subsequent calls to on_block.
@@ -43,19 +43,16 @@ module RuboCop
43
43
  'block start.'.freeze
44
44
 
45
45
  def on_block(node)
46
- end_loc = node.loc.end
47
- do_loc = node.loc.begin # Actually it's either do or {.
48
- return if do_loc.line == end_loc.line # One-liner, no newline needed.
46
+ return if oneliner?(node)
49
47
 
50
48
  # A block node has three children: the block start,
51
49
  # the arguments, and the expression. We care if the block start
52
50
  # with arguments and the expression start on the same line.
53
51
  _block_start, args, last_expression = node.children
52
+ do_loc = node.loc.begin # Actually it's either do or {.
54
53
 
55
- unless args.children.empty?
56
- if do_loc.line != args.loc.last_line
57
- add_offense_for_expression(node, args, ARG_MSG)
58
- end
54
+ if args_on_different_line?(do_loc.line, args)
55
+ add_offense_for_expression(node, args, ARG_MSG)
59
56
  end
60
57
 
61
58
  return unless last_expression
@@ -64,11 +61,19 @@ module RuboCop
64
61
  add_offense_for_expression(node, last_expression, MSG)
65
62
  end
66
63
 
64
+ def oneliner?(node)
65
+ node.loc.begin.line == node.loc.end.line
66
+ end
67
+
68
+ def args_on_different_line?(do_line, args)
69
+ return false if args.children.empty?
70
+
71
+ do_line != args.loc.last_line
72
+ end
73
+
67
74
  def add_offense_for_expression(node, expr, msg)
68
75
  expression = expr.source_range
69
- range = Parser::Source::Range.new(expression.source_buffer,
70
- expression.begin_pos,
71
- expression.end_pos)
76
+ range = range_between(expression.begin_pos, expression.end_pos)
72
77
 
73
78
  add_offense(node, range, msg)
74
79
  end
@@ -76,7 +81,7 @@ module RuboCop
76
81
  def autocorrect(node)
77
82
  lambda do |corrector|
78
83
  _method, args, block_body = *node
79
- unless args.children.empty? || args.loc.last_line == node.loc.line
84
+ unless arguments_on_different_line?(node, args)
80
85
  autocorrect_arguments(corrector, node, args)
81
86
  expr_before_body = args.source_range.end
82
87
  end
@@ -90,18 +95,20 @@ module RuboCop
90
95
  end
91
96
  end
92
97
 
98
+ def arguments_on_different_line?(node, args)
99
+ args.children.empty? || args.loc.last_line == node.loc.line
100
+ end
101
+
93
102
  def autocorrect_arguments(corrector, node, args)
94
103
  end_pos =
95
104
  range_with_surrounding_space(args.source_range, :right, false)
96
105
  .end_pos
97
- range = Parser::Source::Range.new(args.source_range.source_buffer,
98
- node.loc.begin.end.begin_pos,
99
- end_pos)
106
+ range = range_between(node.loc.begin.end.begin_pos, end_pos)
100
107
  corrector.replace(range, " |#{block_arg_string(args)}|")
101
108
  end
102
109
 
103
110
  def autocorrect_body(corrector, node, block_body)
104
- first_node = if block_body.type == :begin
111
+ first_node = if block_body.begin_type?
105
112
  block_body.children.first
106
113
  else
107
114
  block_body
@@ -84,6 +84,15 @@ module RuboCop
84
84
 
85
85
  args
86
86
  end
87
+
88
+ def ignored_literal?(node)
89
+ single_line_ignoring_receiver?(node) || super
90
+ end
91
+
92
+ def single_line_ignoring_receiver?(node)
93
+ return false unless node.loc.begin && node.loc.end
94
+ node.loc.begin.line == node.loc.end.line
95
+ end
87
96
  end
88
97
  end
89
98
  end
@@ -36,13 +36,13 @@ module RuboCop
36
36
  include MultilineExpressionIndentation
37
37
 
38
38
  def validate_config
39
- if style == :aligned && cop_config['IndentationWidth']
40
- raise ValidationError,
41
- 'The `Style/MultilineMethodCallIndentation`' \
42
- ' cop only accepts an `IndentationWidth` ' \
43
- 'configuration parameter when ' \
44
- '`EnforcedStyle` is `indented`.'
45
- end
39
+ return unless style == :aligned && cop_config['IndentationWidth']
40
+
41
+ raise ValidationError,
42
+ 'The `Style/MultilineMethodCallIndentation`' \
43
+ ' cop only accepts an `IndentationWidth` ' \
44
+ 'configuration parameter when ' \
45
+ '`EnforcedStyle` is `indented`.'
46
46
  end
47
47
 
48
48
  private
@@ -74,23 +74,44 @@ module RuboCop
74
74
  end
75
75
 
76
76
  def message(node, lhs, rhs)
77
- if @base
78
- base_source = @base.source[/[^\n]*/]
79
- if style == :indented_relative_to_receiver
80
- "Indent `#{rhs.source}` #{configured_indentation_width} spaces " \
81
- "more than `#{base_source}` on line #{@base.line}."
82
- else
83
- "Align `#{rhs.source}` with `#{base_source}` on " \
84
- "line #{@base.line}."
85
- end
77
+ if should_indent_relative_to_receiver?
78
+ relative_to_receiver_message(rhs)
79
+ elsif should_align_with_base?
80
+ align_with_base_message(rhs)
86
81
  else
87
- used_indentation = rhs.column - indentation(lhs)
88
- what = operation_description(node, rhs)
89
- "Use #{correct_indentation(node)} (not #{used_indentation}) " \
90
- "spaces for indenting #{what} spanning multiple lines."
82
+ no_base_message(lhs, rhs, node)
91
83
  end
92
84
  end
93
85
 
86
+ def should_indent_relative_to_receiver?
87
+ @base && style == :indented_relative_to_receiver
88
+ end
89
+
90
+ def should_align_with_base?
91
+ @base && style != :indented_relative_to_receiver
92
+ end
93
+
94
+ def relative_to_receiver_message(rhs)
95
+ "Indent `#{rhs.source}` #{configured_indentation_width} spaces " \
96
+ "more than `#{base_source}` on line #{@base.line}."
97
+ end
98
+
99
+ def align_with_base_message(rhs)
100
+ "Align `#{rhs.source}` with `#{base_source}` on line #{@base.line}."
101
+ end
102
+
103
+ def base_source
104
+ @base.source[/[^\n]*/]
105
+ end
106
+
107
+ def no_base_message(lhs, rhs, node)
108
+ used_indentation = rhs.column - indentation(lhs)
109
+ what = operation_description(node, rhs)
110
+
111
+ "Use #{correct_indentation(node)} (not #{used_indentation}) " \
112
+ "spaces for indenting #{what} spanning multiple lines."
113
+ end
114
+
94
115
  def alignment_base(node, rhs, given_style)
95
116
  return nil if given_style == :indented
96
117
 
@@ -27,12 +27,12 @@ module RuboCop
27
27
  end
28
28
 
29
29
  def validate_config
30
- if style == :aligned && cop_config['IndentationWidth']
31
- raise ValidationError, 'The `Style/MultilineOperationIndentation`' \
32
- ' cop only accepts an `IndentationWidth` ' \
33
- 'configuration parameter when ' \
34
- '`EnforcedStyle` is `indented`.'
35
- end
30
+ return unless style == :aligned && cop_config['IndentationWidth']
31
+
32
+ raise ValidationError, 'The `Style/MultilineOperationIndentation`' \
33
+ ' cop only accepts an `IndentationWidth` ' \
34
+ 'configuration parameter when ' \
35
+ '`EnforcedStyle` is `indented`.'
36
36
  end
37
37
 
38
38
  private
@@ -12,13 +12,11 @@ module RuboCop
12
12
  'use `if` or `unless` instead.'.freeze
13
13
 
14
14
  def on_if(node)
15
- _condition, _if_branch, else_branch = *node
15
+ _condition, _if_branch, = *node
16
16
 
17
- return unless ternary?(node)
17
+ return unless ternary?(node) && node.multiline?
18
18
 
19
- unless node.loc.line == else_branch.loc.line
20
- add_offense(node, :expression)
21
- end
19
+ add_offense(node, :expression)
22
20
  end
23
21
  end
24
22
  end
@@ -14,10 +14,10 @@ module RuboCop
14
14
  # # good
15
15
  # CONST = [1, 2, 3].freeze
16
16
  class MutableConstant < Cop
17
- MSG = 'Freeze mutable objects assigned to constants.'.freeze
18
-
19
17
  include FrozenStringLiteral
20
18
 
19
+ MSG = 'Freeze mutable objects assigned to constants.'.freeze
20
+
21
21
  def on_casgn(node)
22
22
  _scope, _const_name, value = *node
23
23
  on_assignment(value)
@@ -25,7 +25,22 @@ module RuboCop
25
25
 
26
26
  def on_or_asgn(node)
27
27
  lhs, value = *node
28
- on_assignment(value) if lhs && lhs.type == :casgn
28
+
29
+ return unless lhs && lhs.casgn_type?
30
+
31
+ on_assignment(value)
32
+ end
33
+
34
+ private
35
+
36
+ def on_assignment(value)
37
+ value = splat_value(value) if splat_value(value)
38
+
39
+ return unless value && value.mutable_literal?
40
+ return if FROZEN_STRING_LITERAL_TYPES.include?(value.type) &&
41
+ frozen_string_literals_enabled?(processed_source)
42
+
43
+ add_offense(value, :expression)
29
44
  end
30
45
 
31
46
  def autocorrect(node)
@@ -40,16 +55,9 @@ module RuboCop
40
55
  end
41
56
  end
42
57
 
43
- private
44
-
45
- def on_assignment(value)
46
- return unless value
47
- return unless value.mutable_literal?
48
- return if FROZEN_STRING_LITERAL_TYPES.include?(value.type) &&
49
- frozen_string_literals_enabled?(processed_source)
50
-
51
- add_offense(value, :expression)
52
- end
58
+ def_node_matcher :splat_value, <<-PATTERN
59
+ (array (splat $_))
60
+ PATTERN
53
61
  end
54
62
  end
55
63
  end
@@ -32,7 +32,7 @@ module RuboCop
32
32
  lambda do |corrector|
33
33
  condition, _body, _rest = *node
34
34
  # look inside parentheses around the condition
35
- condition = condition.children.first while condition.type == :begin
35
+ condition = condition.children.first while condition.begin_type?
36
36
  # unwrap the negated portion of the condition (a send node)
37
37
  pos_condition, _method, = *condition
38
38
  corrector.replace(
@@ -19,7 +19,7 @@ module RuboCop
19
19
  end
20
20
 
21
21
  def message(node)
22
- if node.type == :while
22
+ if node.while_type?
23
23
  format(MSG, 'until', 'while')
24
24
  else
25
25
  format(MSG, 'while', 'until')
@@ -32,12 +32,12 @@ module RuboCop
32
32
  lambda do |corrector|
33
33
  condition, _body, _rest = *node
34
34
  # Look inside parentheses around the condition, if any.
35
- condition, = *condition while condition.type == :begin
35
+ condition, = *condition while condition.begin_type?
36
36
  # Unwrap the negated portion of the condition (a send node).
37
37
  pos_condition, _method, = *condition
38
38
  corrector.replace(
39
39
  node.loc.keyword,
40
- node.type == :while ? 'until' : 'while'
40
+ node.while_type? ? 'until' : 'while'
41
41
  )
42
42
  corrector.replace(condition.source_range, pos_condition.source)
43
43
  end
@@ -66,10 +66,8 @@ module RuboCop
66
66
  def autocorrect_if_unless(outer_node, inner_node)
67
67
  outer_cond, = *outer_node
68
68
 
69
- range =
70
- Parser::Source::Range.new(inner_node.source_range.source_buffer,
71
- inner_node.loc.keyword.begin_pos,
72
- outer_cond.source_range.end_pos)
69
+ range = range_between(inner_node.loc.keyword.begin_pos,
70
+ outer_cond.source_range.end_pos)
73
71
 
74
72
  lambda do |corrector|
75
73
  corrector.replace(range, new_expression(outer_node, inner_node))