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
@@ -92,8 +92,7 @@ module RuboCop
92
92
  def check_no_space(space_begin_pos, space_end_pos, msg)
93
93
  return if space_begin_pos >= space_end_pos
94
94
 
95
- range = Parser::Source::Range.new(processed_source.buffer,
96
- space_begin_pos, space_end_pos)
95
+ range = range_between(space_begin_pos, space_end_pos)
97
96
  add_offense(range, range, "#{msg} block parameter detected.")
98
97
  end
99
98
 
@@ -33,9 +33,7 @@ module RuboCop
33
33
 
34
34
  def incorrect_style_detected(arg, value, space_on_both_sides,
35
35
  no_surrounding_space)
36
- range = Parser::Source::Range.new(processed_source.buffer,
37
- arg.pos.end_pos,
38
- value.pos.begin_pos)
36
+ range = range_between(arg.pos.end_pos, value.pos.begin_pos)
39
37
  add_offense(range, range) do
40
38
  if style == :space && no_surrounding_space ||
41
39
  style == :no_space && space_on_both_sides
@@ -19,6 +19,7 @@ module RuboCop
19
19
  !any_pairs_on_the_same_line?(node.parent)
20
20
 
21
21
  _, right = *node
22
+
22
23
  check_operator(node.loc.operator, right.source_range)
23
24
  end
24
25
 
@@ -31,10 +32,10 @@ module RuboCop
31
32
  end
32
33
 
33
34
  def on_resbody(node)
34
- if node.loc.assoc
35
- _, variable, = *node
36
- check_operator(node.loc.assoc, variable.source_range)
37
- end
35
+ return unless node.loc.assoc
36
+ _, variable, = *node
37
+
38
+ check_operator(node.loc.assoc, variable.source_range)
38
39
  end
39
40
 
40
41
  def on_send(node)
@@ -88,20 +89,24 @@ module RuboCop
88
89
  with_space = range_with_surrounding_space(op)
89
90
  return if with_space.source.start_with?("\n")
90
91
 
92
+ offense(op, with_space, right_operand) do |msg|
93
+ add_offense(with_space, op, msg)
94
+ end
95
+ end
96
+
97
+ def offense(op, with_space, right_operand)
98
+ msg = offense_message(op, with_space, right_operand)
99
+ yield msg if msg
100
+ end
101
+
102
+ def offense_message(op, with_space, right_operand)
91
103
  if op.is?('**')
92
- unless with_space.is?('**')
93
- add_offense(with_space, op,
94
- 'Space around operator `**` detected.')
95
- end
104
+ 'Space around operator `**` detected.' unless with_space.is?('**')
96
105
  elsif with_space.source !~ /^\s.*\s$/
97
- add_offense(with_space, op, 'Surrounding space missing for ' \
98
- "operator `#{op.source}`.")
99
- elsif excess_leading_space?(op, with_space)
100
- add_offense(with_space, op, "Operator `#{op.source}` should be " \
101
- 'surrounded by a single space.')
102
- elsif excess_trailing_space?(right_operand, with_space)
103
- add_offense(with_space, op, "Operator `#{op.source}` should be " \
104
- 'surrounded by a single space.')
106
+ "Surrounding space missing for operator `#{op.source}`."
107
+ elsif excess_leading_space?(op, with_space) ||
108
+ excess_trailing_space?(right_operand, with_space)
109
+ "Operator `#{op.source}` should be surrounded by a single space."
105
110
  end
106
111
  end
107
112
 
@@ -12,15 +12,6 @@ module RuboCop
12
12
  def on_block(node)
13
13
  return if node.loc.begin.is?('do') # No braces.
14
14
 
15
- # If braces are on separate lines, and the BlockDelimiters cop is
16
- # enabled, those braces will be changed to do..end by the user or by
17
- # auto-correct, so reporting space issues is not useful, and it
18
- # creates auto-correct conflicts.
19
- if config.for_cop('Style/BlockDelimiters')['Enabled'] &&
20
- block_length(node) > 0
21
- return
22
- end
23
-
24
15
  left_brace = node.loc.begin
25
16
  space_plus_brace = range_with_surrounding_space(left_brace)
26
17
  used_style =
@@ -43,9 +34,8 @@ module RuboCop
43
34
  end
44
35
 
45
36
  def space_detected(left_brace, space_plus_brace)
46
- space = Parser::Source::Range.new(left_brace.source_buffer,
47
- space_plus_brace.begin_pos,
48
- left_brace.begin_pos)
37
+ space = range_between(space_plus_brace.begin_pos,
38
+ left_brace.begin_pos)
49
39
  add_offense(space, space, 'Space detected to the left of {.') do
50
40
  opposite_style_detected
51
41
  end
@@ -29,9 +29,7 @@ module RuboCop
29
29
  _receiver, _method_name, *args = *node
30
30
  arg1 = args.first.source_range
31
31
  arg1_with_space = range_with_surrounding_space(arg1, :left)
32
- space = Parser::Source::Range.new(arg1.source_buffer,
33
- arg1_with_space.begin_pos,
34
- arg1.begin_pos)
32
+ space = range_between(arg1_with_space.begin_pos, arg1.begin_pos)
35
33
  add_offense(space, space) if space.length > 1
36
34
  end
37
35
 
@@ -21,7 +21,7 @@ module RuboCop
21
21
  /(?:[\S&&[^\\]](?:\\ )*)( {2,})(?=\S)/
22
22
 
23
23
  def on_array(node)
24
- process(node, *%w(%i %I %w %W))
24
+ process(node, '%i', '%I', '%w', '%W')
25
25
  end
26
26
 
27
27
  def on_percent_literal(node)
@@ -15,15 +15,6 @@ module RuboCop
15
15
  def on_block(node)
16
16
  return if node.loc.begin.is?('do') # No braces.
17
17
 
18
- # If braces are on separate lines, and the BlockDelimiters cop is
19
- # enabled, those braces will be changed to do..end by the user or by
20
- # auto-correct, so reporting space issues is not useful, and it
21
- # creates auto-correct conflicts.
22
- if config.for_cop('Style/BlockDelimiters')['Enabled'] &&
23
- block_length(node) > 0
24
- return
25
- end
26
-
27
18
  left_brace = node.loc.begin
28
19
  right_brace = node.loc.end
29
20
 
@@ -33,56 +24,58 @@ module RuboCop
33
24
  private
34
25
 
35
26
  def check_inside(node, left_brace, right_brace)
36
- sb = node.source_range.source_buffer
37
-
38
27
  if left_brace.end_pos == right_brace.begin_pos
39
- adjacent_braces(sb, left_brace, right_brace)
28
+ adjacent_braces(left_brace, right_brace)
40
29
  else
41
- range = Parser::Source::Range.new(sb, left_brace.end_pos,
42
- right_brace.begin_pos)
30
+ range = range_between(left_brace.end_pos, right_brace.begin_pos)
43
31
  inner = range.source
44
32
 
45
33
  if inner =~ /\S/
46
- braces_with_contents_inside(node, inner, sb)
34
+ braces_with_contents_inside(node, inner)
47
35
  elsif style_for_empty_braces == :no_space
48
- offense(sb, range.begin_pos, range.end_pos,
36
+ offense(range.begin_pos, range.end_pos,
49
37
  'Space inside empty braces detected.')
50
38
  end
51
39
  end
52
40
  end
53
41
 
54
- def adjacent_braces(sb, left_brace, right_brace)
42
+ def adjacent_braces(left_brace, right_brace)
55
43
  return if style_for_empty_braces != :space
56
44
 
57
- offense(sb, left_brace.begin_pos, right_brace.end_pos,
45
+ offense(left_brace.begin_pos, right_brace.end_pos,
58
46
  'Space missing inside empty braces.')
59
47
  end
60
48
 
61
- def braces_with_contents_inside(node, inner, sb)
49
+ def braces_with_contents_inside(node, inner)
62
50
  _method, args, _body = *node
63
- left_brace = node.loc.begin
64
- right_brace = node.loc.end
65
51
  args_delimiter = args.loc.begin # Can be ( | or nil.
66
52
 
53
+ check_left_brace(inner, node.loc.begin, args_delimiter)
54
+ check_right_brace(inner, node.loc.end, block_length(node))
55
+ end
56
+
57
+ def check_left_brace(inner, left_brace, args_delimiter)
67
58
  if inner =~ /^\S/
68
- no_space_inside_left_brace(left_brace, args_delimiter, sb)
59
+ no_space_inside_left_brace(left_brace, args_delimiter)
69
60
  else
70
- space_inside_left_brace(left_brace, args_delimiter, sb)
61
+ space_inside_left_brace(left_brace, args_delimiter)
71
62
  end
63
+ end
72
64
 
73
- if inner =~ /\S$/ && block_length(node).zero?
74
- no_space(sb, right_brace.begin_pos, right_brace.end_pos,
65
+ def check_right_brace(inner, right_brace, block_length)
66
+ if inner =~ /\S$/ && block_length.zero?
67
+ no_space(right_brace.begin_pos, right_brace.end_pos,
75
68
  'Space missing inside }.')
76
69
  else
77
- space_inside_right_brace(right_brace, sb)
70
+ space_inside_right_brace(right_brace)
78
71
  end
79
72
  end
80
73
 
81
- def no_space_inside_left_brace(left_brace, args_delimiter, sb)
74
+ def no_space_inside_left_brace(left_brace, args_delimiter)
82
75
  if pipe?(args_delimiter)
83
76
  if left_brace.end_pos == args_delimiter.begin_pos &&
84
77
  cop_config['SpaceBeforeBlockParameters']
85
- offense(sb, left_brace.begin_pos, args_delimiter.end_pos,
78
+ offense(left_brace.begin_pos, args_delimiter.end_pos,
86
79
  'Space between { and | missing.') do
87
80
  opposite_style_detected
88
81
  end
@@ -91,22 +84,22 @@ module RuboCop
91
84
  # We indicate the position after the left brace. Otherwise it's
92
85
  # difficult to distinguish between space missing to the left and to
93
86
  # the right of the brace in autocorrect.
94
- no_space(sb, left_brace.end_pos, left_brace.end_pos + 1,
87
+ no_space(left_brace.end_pos, left_brace.end_pos + 1,
95
88
  'Space missing inside {.')
96
89
  end
97
90
  end
98
91
 
99
- def space_inside_left_brace(left_brace, args_delimiter, sb)
92
+ def space_inside_left_brace(left_brace, args_delimiter)
100
93
  if pipe?(args_delimiter)
101
94
  unless cop_config['SpaceBeforeBlockParameters']
102
- offense(sb, left_brace.end_pos, args_delimiter.begin_pos,
95
+ offense(left_brace.end_pos, args_delimiter.begin_pos,
103
96
  'Space between { and | detected.') do
104
97
  opposite_style_detected
105
98
  end
106
99
  end
107
100
  else
108
101
  brace_with_space = range_with_surrounding_space(left_brace, :right)
109
- space(sb, brace_with_space.begin_pos + 1, brace_with_space.end_pos,
102
+ space(brace_with_space.begin_pos + 1, brace_with_space.end_pos,
110
103
  'Space inside { detected.')
111
104
  end
112
105
  end
@@ -115,30 +108,30 @@ module RuboCop
115
108
  args_delimiter && args_delimiter.is?('|')
116
109
  end
117
110
 
118
- def space_inside_right_brace(right_brace, sb)
111
+ def space_inside_right_brace(right_brace)
119
112
  brace_with_space = range_with_surrounding_space(right_brace, :left)
120
- space(sb, brace_with_space.begin_pos, brace_with_space.end_pos - 1,
113
+ space(brace_with_space.begin_pos, brace_with_space.end_pos - 1,
121
114
  'Space inside } detected.')
122
115
  end
123
116
 
124
- def no_space(sb, begin_pos, end_pos, msg)
117
+ def no_space(begin_pos, end_pos, msg)
125
118
  if style == :space
126
- offense(sb, begin_pos, end_pos, msg) { opposite_style_detected }
119
+ offense(begin_pos, end_pos, msg) { opposite_style_detected }
127
120
  else
128
121
  correct_style_detected
129
122
  end
130
123
  end
131
124
 
132
- def space(sb, begin_pos, end_pos, msg)
125
+ def space(begin_pos, end_pos, msg)
133
126
  if style == :no_space
134
- offense(sb, begin_pos, end_pos, msg) { opposite_style_detected }
127
+ offense(begin_pos, end_pos, msg) { opposite_style_detected }
135
128
  else
136
129
  correct_style_detected
137
130
  end
138
131
  end
139
132
 
140
- def offense(sb, begin_pos, end_pos, msg)
141
- range = Parser::Source::Range.new(sb, begin_pos, end_pos)
133
+ def offense(begin_pos, end_pos, msg)
134
+ range = range_between(begin_pos, end_pos)
142
135
  add_offense(range, range, msg) { yield if block_given? }
143
136
  end
144
137
 
@@ -13,21 +13,37 @@ module RuboCop
13
13
  MSG = 'Space inside %s.'.freeze
14
14
 
15
15
  def on_hash(node)
16
- b_ix = index_of_first_token(node)
17
16
  tokens = processed_source.tokens
18
17
 
19
- # Hash literal with braces?
20
- return unless tokens[b_ix].type == :tLBRACE
18
+ hash_literal_with_braces(node) do |begin_index, end_index|
19
+ check(tokens[begin_index], tokens[begin_index + 1])
20
+ return if begin_index == end_index - 1
21
21
 
22
- e_ix = index_of_last_token(node)
23
- return unless tokens[e_ix].type == :tRCURLY
24
-
25
- check(tokens[b_ix], tokens[b_ix + 1])
26
- check(tokens[e_ix - 1], tokens[e_ix]) unless b_ix == e_ix - 1
22
+ check(tokens[end_index - 1], tokens[end_index])
23
+ end
27
24
  end
28
25
 
29
26
  private
30
27
 
28
+ def hash_literal_with_braces(node)
29
+ tokens = processed_source.tokens
30
+ begin_index = index_of_first_token(node)
31
+ return unless left_brace?(tokens[begin_index])
32
+
33
+ end_index = index_of_last_token(node)
34
+ return unless right_brace?(tokens[end_index])
35
+
36
+ yield begin_index, end_index
37
+ end
38
+
39
+ def left_brace?(token)
40
+ token.type == :tLBRACE
41
+ end
42
+
43
+ def right_brace?(token)
44
+ token.type == :tRCURLY
45
+ end
46
+
31
47
  def check(t1, t2)
32
48
  # No offense if line break inside.
33
49
  return if t1.pos.line < t2.pos.line
@@ -61,17 +77,16 @@ module RuboCop
61
77
  range = expect_space ? brace : space_range(brace)
62
78
  add_offense(range, range,
63
79
  message(brace, is_empty_braces, expect_space)) do
64
- if expect_space
65
- if t1.text == t2.text
66
- ambiguous_style_detected(:no_space, :compact)
67
- else
68
- unexpected_style_detected(:no_space)
69
- end
70
- elsif t1.text == t2.text
71
- unexpected_style_detected(:space)
72
- else
73
- ambiguous_style_detected(:space, :compact)
74
- end
80
+ style = expect_space ? :no_space : :space
81
+ ambiguous_or_unexpected_style_detected(style, t1.text == t2.text)
82
+ end
83
+ end
84
+
85
+ def ambiguous_or_unexpected_style_detected(style, is_match)
86
+ if is_match
87
+ ambiguous_style_detected(style, :compact)
88
+ else
89
+ unexpected_style_detected(style)
75
90
  end
76
91
  end
77
92
 
@@ -119,16 +134,16 @@ module RuboCop
119
134
  src = range.source_buffer.source
120
135
  end_pos = range.end_pos
121
136
  end_pos += 1 while src[end_pos] =~ /[ \t]/
122
- Parser::Source::Range.new(range.source_buffer,
123
- range.begin_pos + 1, end_pos)
137
+
138
+ range_between(range.begin_pos + 1, end_pos)
124
139
  end
125
140
 
126
141
  def range_of_space_to_the_left(range)
127
142
  src = range.source_buffer.source
128
143
  begin_pos = range.begin_pos
129
144
  begin_pos -= 1 while src[begin_pos - 1] =~ /[ \t]/
130
- Parser::Source::Range.new(range.source_buffer, begin_pos,
131
- range.end_pos - 1)
145
+
146
+ range_between(begin_pos, range.end_pos - 1)
132
147
  end
133
148
  end
134
149
  end
@@ -24,7 +24,7 @@ module RuboCop
24
24
  END_REGEX = /(?<!\\)( +)\z/
25
25
 
26
26
  def on_array(node)
27
- process(node, *%w(%i %I %w %W))
27
+ process(node, '%i', '%I', '%w', '%W')
28
28
  end
29
29
 
30
30
  def on_percent_literal(node)
@@ -19,25 +19,39 @@ module RuboCop
19
19
  SPACE_MSG = 'Missing space around string interpolation detected.'.freeze
20
20
 
21
21
  def on_dstr(node)
22
- node.children.select { |n| n.type == :begin }.each do |begin_node|
22
+ each_style_violation(node) do |final_node, msg|
23
+ add_offense(final_node, :expression, msg)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def each_style_violation(node)
30
+ node.each_child_node(:begin) do |begin_node|
23
31
  final_node = begin_node.children.last
24
32
  next unless final_node
25
33
 
26
- interp = final_node.source_range
27
- interp_with_surrounding_space = range_with_surrounding_space(interp)
28
- if style == :no_space
29
- if interp_with_surrounding_space != interp
30
- add_offense(final_node, :expression, NO_SPACE_MSG)
31
- end
32
- elsif style == :space
33
- if interp_with_surrounding_space.source != " #{interp.source} "
34
- add_offense(final_node, :expression, SPACE_MSG)
35
- end
34
+ if style == :no_space && space_on_any_side?(final_node)
35
+ yield final_node, NO_SPACE_MSG
36
+ elsif style == :space && !space_on_each_side?(final_node)
37
+ yield final_node, SPACE_MSG
36
38
  end
37
39
  end
38
40
  end
39
41
 
40
- private
42
+ def space_on_any_side?(node)
43
+ interp = node.source_range
44
+ interp_with_surrounding_space = range_with_surrounding_space(interp)
45
+
46
+ interp_with_surrounding_space != interp
47
+ end
48
+
49
+ def space_on_each_side?(node)
50
+ interp = node.source_range
51
+ interp_with_surrounding_space = range_with_surrounding_space(interp)
52
+
53
+ interp_with_surrounding_space.source == " #{interp.source} "
54
+ end
41
55
 
42
56
  def autocorrect(node)
43
57
  new_source = style == :no_space ? node.source : " #{node.source} "