rubocop 0.15.0 → 0.16.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/.rubocop.yml +1 -10
  3. data/.travis.yml +2 -1
  4. data/CHANGELOG.md +55 -0
  5. data/Gemfile +3 -0
  6. data/README.md +37 -0
  7. data/config/default.yml +99 -16
  8. data/config/enabled.yml +28 -16
  9. data/lib/rubocop.rb +16 -2
  10. data/lib/rubocop/cli.rb +10 -91
  11. data/lib/rubocop/config.rb +4 -1
  12. data/lib/rubocop/config_loader.rb +18 -10
  13. data/lib/rubocop/config_store.rb +3 -2
  14. data/lib/rubocop/cop/check_assignment.rb +43 -0
  15. data/lib/rubocop/cop/check_methods.rb +18 -0
  16. data/lib/rubocop/cop/commissioner.rb +15 -3
  17. data/lib/rubocop/cop/cop.rb +51 -43
  18. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
  19. data/lib/rubocop/cop/lint/block_alignment.rb +7 -7
  20. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  21. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  22. data/lib/rubocop/cop/lint/end_alignment.rb +56 -19
  23. data/lib/rubocop/cop/lint/end_in_method.rb +4 -10
  24. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  25. data/lib/rubocop/cop/lint/eval.rb +1 -1
  26. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
  27. data/lib/rubocop/cop/lint/literal_in_condition.rb +2 -2
  28. data/lib/rubocop/cop/lint/loop.rb +1 -1
  29. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  30. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  31. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  32. data/lib/rubocop/cop/lint/syntax.rb +1 -1
  33. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  34. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -2
  35. data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
  36. data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -16
  37. data/lib/rubocop/cop/lint/void.rb +5 -5
  38. data/lib/rubocop/cop/rails/default_scope.rb +33 -0
  39. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -4
  40. data/lib/rubocop/cop/rails/output.rb +1 -1
  41. data/lib/rubocop/cop/rails/read_attribute.rb +1 -1
  42. data/lib/rubocop/cop/rails/validation.rb +1 -1
  43. data/lib/rubocop/cop/style/access_modifier_indentation.rb +31 -16
  44. data/lib/rubocop/cop/style/accessor_method_name.rb +53 -0
  45. data/lib/rubocop/cop/style/alias.rb +14 -2
  46. data/lib/rubocop/cop/style/align_hash.rb +15 -16
  47. data/lib/rubocop/cop/style/and_or.rb +3 -3
  48. data/lib/rubocop/cop/style/array_syntax.rb +22 -0
  49. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  50. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -3
  51. data/lib/rubocop/cop/style/attr.rb +1 -1
  52. data/lib/rubocop/cop/style/autocorrect_alignment.rb +13 -12
  53. data/lib/rubocop/cop/style/begin_block.rb +1 -1
  54. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  55. data/lib/rubocop/cop/style/block_nesting.rb +5 -1
  56. data/lib/rubocop/cop/style/blocks.rb +4 -4
  57. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +48 -22
  58. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  59. data/lib/rubocop/cop/style/case_indentation.rb +25 -6
  60. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +1 -1
  61. data/lib/rubocop/cop/style/class_length.rb +7 -19
  62. data/lib/rubocop/cop/style/class_methods.rb +1 -1
  63. data/lib/rubocop/cop/style/class_vars.rb +1 -1
  64. data/lib/rubocop/cop/style/code_length.rb +35 -0
  65. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  66. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  67. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  68. data/lib/rubocop/cop/style/configurable_enforced_style.rb +51 -0
  69. data/lib/rubocop/cop/style/configurable_max.rb +17 -0
  70. data/lib/rubocop/cop/style/configurable_naming.rb +4 -10
  71. data/lib/rubocop/cop/style/constant_name.rb +5 -3
  72. data/lib/rubocop/cop/style/cyclomatic_complexity.rb +8 -13
  73. data/lib/rubocop/cop/style/def_parentheses.rb +4 -43
  74. data/lib/rubocop/cop/style/documentation.rb +1 -1
  75. data/lib/rubocop/cop/style/dot_position.rb +6 -2
  76. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
  77. data/lib/rubocop/cop/style/empty_lines.rb +5 -1
  78. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
  79. data/lib/rubocop/cop/style/empty_lines_around_body.rb +28 -13
  80. data/lib/rubocop/cop/style/empty_literal.rb +3 -3
  81. data/lib/rubocop/cop/style/encoding.rb +5 -5
  82. data/lib/rubocop/cop/style/end_block.rb +1 -1
  83. data/lib/rubocop/cop/style/end_of_line.rb +8 -6
  84. data/lib/rubocop/cop/style/even_odd.rb +6 -6
  85. data/lib/rubocop/cop/style/favor_join.rb +1 -1
  86. data/lib/rubocop/cop/style/favor_modifier.rb +20 -34
  87. data/lib/rubocop/cop/style/favor_sprintf.rb +1 -1
  88. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -1
  89. data/lib/rubocop/cop/style/final_newline.rb +1 -1
  90. data/lib/rubocop/cop/style/flip_flop.rb +20 -0
  91. data/lib/rubocop/cop/style/for.rb +34 -3
  92. data/lib/rubocop/cop/style/global_vars.rb +1 -1
  93. data/lib/rubocop/cop/style/hash_methods.rb +3 -3
  94. data/lib/rubocop/cop/style/hash_syntax.rb +8 -11
  95. data/lib/rubocop/cop/style/if_node.rb +25 -0
  96. data/lib/rubocop/cop/style/if_then_else.rb +1 -1
  97. data/lib/rubocop/cop/style/indentation_width.rb +77 -43
  98. data/lib/rubocop/cop/style/lambda.rb +2 -2
  99. data/lib/rubocop/cop/style/lambda_call.rb +4 -12
  100. data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
  101. data/lib/rubocop/cop/style/line_length.rb +9 -5
  102. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
  103. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  104. data/lib/rubocop/cop/style/method_def_parentheses.rb +52 -0
  105. data/lib/rubocop/cop/style/method_length.rb +9 -22
  106. data/lib/rubocop/cop/style/module_function.rb +1 -1
  107. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  108. data/lib/rubocop/cop/style/multiline_if_then.rb +11 -1
  109. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  110. data/lib/rubocop/cop/style/not.rb +1 -1
  111. data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
  112. data/lib/rubocop/cop/style/op_method.rb +2 -2
  113. data/lib/rubocop/cop/style/parameter_lists.rb +2 -2
  114. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -1
  115. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  116. data/lib/rubocop/cop/style/predicate_name.rb +54 -0
  117. data/lib/rubocop/cop/style/proc.rb +1 -1
  118. data/lib/rubocop/cop/style/raise_args.rb +4 -10
  119. data/lib/rubocop/cop/style/redundant_begin.rb +5 -15
  120. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  121. data/lib/rubocop/cop/style/redundant_return.rb +9 -19
  122. data/lib/rubocop/cop/style/redundant_self.rb +8 -3
  123. data/lib/rubocop/cop/style/regexp_literal.rb +4 -3
  124. data/lib/rubocop/cop/style/rescue_modifier.rb +9 -17
  125. data/lib/rubocop/cop/style/semicolon.rb +20 -22
  126. data/lib/rubocop/cop/style/signal_exception.rb +10 -17
  127. data/lib/rubocop/cop/style/single_line_block_params.rb +62 -0
  128. data/lib/rubocop/cop/style/single_line_methods.rb +5 -15
  129. data/lib/rubocop/cop/style/space_after_comma_etc.rb +1 -3
  130. data/lib/rubocop/cop/style/space_after_control_keyword.rb +1 -1
  131. data/lib/rubocop/cop/style/space_after_method_name.rb +4 -12
  132. data/lib/rubocop/cop/style/space_after_not.rb +1 -1
  133. data/lib/rubocop/cop/style/space_around_block_braces.rb +105 -94
  134. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -1
  135. data/lib/rubocop/cop/style/space_around_operators.rb +38 -123
  136. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +1 -1
  137. data/lib/rubocop/cop/style/space_inside.rb +1 -3
  138. data/lib/rubocop/cop/style/space_inside_brackets.rb +0 -2
  139. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +5 -5
  140. data/lib/rubocop/cop/style/space_inside_parens.rb +0 -2
  141. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  142. data/lib/rubocop/cop/style/string_help.rb +1 -1
  143. data/lib/rubocop/cop/style/string_literals.rb +4 -11
  144. data/lib/rubocop/cop/style/surrounding_space.rb +0 -2
  145. data/lib/rubocop/cop/style/symbol_array.rb +3 -10
  146. data/lib/rubocop/cop/style/tab.rb +5 -5
  147. data/lib/rubocop/cop/style/ternary_operator.rb +2 -2
  148. data/lib/rubocop/cop/style/trailing_blank_lines.rb +1 -1
  149. data/lib/rubocop/cop/style/trailing_whitespace.rb +1 -1
  150. data/lib/rubocop/cop/style/trivial_accessors.rb +4 -14
  151. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  152. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  153. data/lib/rubocop/cop/style/when_then.rb +3 -1
  154. data/lib/rubocop/cop/style/while_until_do.rb +2 -2
  155. data/lib/rubocop/cop/style/word_array.rb +4 -10
  156. data/lib/rubocop/cop/util.rb +85 -12
  157. data/lib/rubocop/cop/variable_inspector.rb +4 -2
  158. data/lib/rubocop/file_inspector.rb +98 -0
  159. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  160. data/lib/rubocop/formatter/disabled_config_formatter.rb +10 -2
  161. data/lib/rubocop/rake_task.rb +5 -1
  162. data/lib/rubocop/target_finder.rb +3 -3
  163. data/lib/rubocop/version.rb +1 -1
  164. data/rubocop-todo.yml +12 -0
  165. data/rubocop.gemspec +1 -1
  166. data/spec/isolated_environment_spec.rb +24 -0
  167. data/spec/rubocop/cli_spec.rb +120 -27
  168. data/spec/rubocop/config_loader_spec.rb +25 -11
  169. data/spec/rubocop/config_spec.rb +25 -9
  170. data/spec/rubocop/config_store_spec.rb +1 -1
  171. data/spec/rubocop/cop/cop_spec.rb +22 -3
  172. data/spec/rubocop/cop/lint/block_alignment_spec.rb +4 -4
  173. data/spec/rubocop/cop/lint/end_alignment_spec.rb +79 -47
  174. data/spec/rubocop/cop/lint/syntax_spec.rb +4 -3
  175. data/spec/rubocop/cop/rails/default_scope_spec.rb +31 -0
  176. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +37 -2
  177. data/spec/rubocop/cop/style/accessor_method_name_spec.rb +81 -0
  178. data/spec/rubocop/cop/style/alias_spec.rb +10 -0
  179. data/spec/rubocop/cop/style/align_parameters_spec.rb +3 -3
  180. data/spec/rubocop/cop/style/block_nesting_spec.rb +4 -1
  181. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +83 -0
  182. data/spec/rubocop/cop/style/case_indentation_spec.rb +227 -71
  183. data/spec/rubocop/cop/style/class_length_spec.rb +2 -0
  184. data/spec/rubocop/cop/style/constant_name_spec.rb +6 -0
  185. data/spec/rubocop/cop/style/cyclomatic_complexity_spec.rb +1 -0
  186. data/spec/rubocop/cop/style/documentation_spec.rb +12 -0
  187. data/spec/rubocop/cop/style/dot_position_spec.rb +10 -0
  188. data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +22 -0
  189. data/spec/rubocop/cop/style/empty_lines_spec.rb +6 -0
  190. data/spec/rubocop/cop/style/end_of_line_spec.rb +17 -0
  191. data/spec/rubocop/cop/style/favor_modifier_spec.rb +15 -0
  192. data/spec/rubocop/cop/style/flip_flop_spec.rb +23 -0
  193. data/spec/rubocop/cop/style/for_spec.rb +96 -18
  194. data/spec/rubocop/cop/style/hash_syntax_spec.rb +8 -2
  195. data/spec/rubocop/cop/style/indentation_width_spec.rb +185 -20
  196. data/spec/rubocop/cop/style/line_length_spec.rb +1 -0
  197. data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +72 -0
  198. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +16 -0
  199. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +34 -3
  200. data/spec/rubocop/cop/style/predicate_name_spec.rb +25 -0
  201. data/spec/rubocop/cop/style/redundant_self_spec.rb +15 -0
  202. data/spec/rubocop/cop/style/regexp_literal_spec.rb +5 -0
  203. data/spec/rubocop/cop/style/semicolon_spec.rb +16 -16
  204. data/spec/rubocop/cop/style/{reduce_arguments_spec.rb → single_line_block_params_spec.rb} +12 -4
  205. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +58 -8
  206. data/spec/rubocop/cop/style/space_around_operators_spec.rb +71 -0
  207. data/spec/rubocop/cop/style/string_literals_spec.rb +18 -0
  208. data/spec/rubocop/file_inspector_spec.rb +78 -0
  209. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +7 -7
  210. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +3 -3
  211. data/spec/rubocop/formatter/file_list_formatter_spec.rb +2 -2
  212. data/spec/rubocop/processed_source_spec.rb +7 -3
  213. data/spec/rubocop/token_spec.rb +1 -1
  214. data/spec/spec_helper.rb +3 -2
  215. data/spec/support/isolated_environment.rb +7 -0
  216. data/spec/support/shared_context.rb +5 -1
  217. metadata +35 -12
  218. data/lib/rubocop/cop/style/reduce_arguments.rb +0 -34
  219. data/lib/rubocop/cop/style/symbol_name.rb +0 -45
  220. data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +0 -33
  221. data/spec/rubocop/cop/style/symbol_name_spec.rb +0 -138
@@ -29,7 +29,7 @@ module Rubocop
29
29
  if OPS.include?(op)
30
30
  _receiver, _method, args = *node
31
31
 
32
- convention(node, :selector) if args == NIL_NODE
32
+ add_offence(node, :selector) if args == NIL_NODE
33
33
  end
34
34
  end
35
35
  end
@@ -13,7 +13,7 @@ module Rubocop
13
13
  # not does not take any arguments
14
14
  if args.empty? && method_name == :! &&
15
15
  node.loc.selector.is?('not')
16
- convention(node, :selector)
16
+ add_offence(node, :selector)
17
17
  end
18
18
  end
19
19
  end
@@ -6,7 +6,7 @@ module Rubocop
6
6
  # This cop checks for big numeric literals without _ between groups
7
7
  # of digits in them.
8
8
  class NumericLiterals < Cop
9
- MSG = 'Separate every 3 digits in the integer portion of a number' \
9
+ MSG = 'Separate every 3 digits in the integer portion of a number ' \
10
10
  'with underscores(_).'
11
11
 
12
12
  def on_int(node)
@@ -27,7 +27,7 @@ module Rubocop
27
27
 
28
28
  if int.size >= min_digits
29
29
  if int =~ /\d{4}/ || int =~ /_\d{1,2}_/
30
- convention(node, :expression)
30
+ add_offence(node, :expression)
31
31
  end
32
32
  end
33
33
  end
@@ -17,8 +17,8 @@ module Rubocop
17
17
 
18
18
  if name !~ /\A\w/ && !BLACKLISTED.include?(name) &&
19
19
  args.children.size == 1 && args != TARGET_ARGS
20
- convention(args.children[0], :expression,
21
- sprintf(MSG, name))
20
+ add_offence(args.children[0], :expression,
21
+ sprintf(MSG, name))
22
22
  end
23
23
  end
24
24
  end
@@ -12,8 +12,8 @@ module Rubocop
12
12
 
13
13
  def on_args(node)
14
14
  if args_count(node) > max_params
15
- convention(node, :expression,
16
- sprintf(MSG, max_params))
15
+ add_offence(node, :expression,
16
+ sprintf(MSG, max_params))
17
17
  end
18
18
  end
19
19
 
@@ -31,7 +31,7 @@ module Rubocop
31
31
  # allow safe assignment
32
32
  return if safe_assignment?(cond) && safe_assignment_allowed?
33
33
 
34
- convention(cond, :expression)
34
+ add_offence(cond, :expression)
35
35
  end
36
36
  end
37
37
 
@@ -42,6 +42,13 @@ module Rubocop
42
42
  def safe_assignment_allowed?
43
43
  cop_config['AllowSafeAssignment']
44
44
  end
45
+
46
+ def autocorrect(node)
47
+ @corrections << lambda do |corrector|
48
+ corrector.remove(node.loc.begin)
49
+ corrector.remove(node.loc.end)
50
+ end
51
+ end
45
52
  end
46
53
  end
47
54
  end
@@ -9,7 +9,7 @@ module Rubocop
9
9
  MSG = 'Avoid the use of Perl-style backrefs.'
10
10
 
11
11
  def on_nth_ref(node)
12
- convention(node, :expression)
12
+ add_offence(node, :expression)
13
13
  end
14
14
 
15
15
  def autocorrect(node)
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # This cop makes sure that predicates are named properly.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # def is_even?(value) ...
11
+ #
12
+ # # good
13
+ # def even?(value)
14
+ #
15
+ # # bad
16
+ # def has_value? ...
17
+ #
18
+ # # good
19
+ # def value? ...
20
+ class PredicateName < Cop
21
+ def on_def(node)
22
+ method_name, args, _body = *node
23
+ check(node, method_name.to_s, args)
24
+ end
25
+
26
+ def on_defs(node)
27
+ _scope, method_name, args, _body = *node
28
+ check(node, method_name.to_s, args)
29
+ end
30
+
31
+ private
32
+
33
+ def check(node, method_name, args)
34
+ prefix_blacklist.each do |prefix|
35
+ if method_name.start_with?(prefix)
36
+ add_offence(node, :name,
37
+ message(method_name, prefix))
38
+ end
39
+ end
40
+ end
41
+
42
+ def message(method_name, prefix)
43
+ new_name = method_name.sub(prefix, '')
44
+ new_name << '?' unless method_name.end_with?('?')
45
+ "Rename #{method_name} to #{new_name}."
46
+ end
47
+
48
+ def prefix_blacklist
49
+ cop_config['NamePrefixBlacklist']
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -18,7 +18,7 @@ module Rubocop
18
18
  # ...)
19
19
  block_method, = *node
20
20
 
21
- convention(block_method, :expression) if block_method == TARGET
21
+ add_offence(block_method, :expression) if block_method == TARGET
22
22
  end
23
23
  end
24
24
  end
@@ -5,6 +5,8 @@ module Rubocop
5
5
  module Style
6
6
  # This cop checks the args passed to `fail` and `raise`.
7
7
  class RaiseArgs < Cop
8
+ include ConfigurableEnforcedStyle
9
+
8
10
  def on_send(node)
9
11
  return unless command?(:raise, node) || command?(:fail, node)
10
12
 
@@ -23,7 +25,7 @@ module Rubocop
23
25
 
24
26
  return unless args.size > 1
25
27
 
26
- convention(node, :expression, message(selector))
28
+ add_offence(node, :expression, message(selector))
27
29
  end
28
30
 
29
31
  def check_exploded(node)
@@ -38,19 +40,11 @@ module Rubocop
38
40
 
39
41
  # Allow code like `raise Ex.new(arg1, arg2)`.
40
42
  unless constructor_args.size > 1
41
- convention(node, :expression, message(selector))
43
+ add_offence(node, :expression, message(selector))
42
44
  end
43
45
  end
44
46
  end
45
47
 
46
- def style
47
- case cop_config['EnforcedStyle']
48
- when 'compact' then :compact
49
- when 'exploded' then :exploded
50
- else fail 'Unknown style selected!'
51
- end
52
- end
53
-
54
48
  def message(method)
55
49
  case style
56
50
  when :compact
@@ -18,26 +18,16 @@ module Rubocop
18
18
  # end
19
19
  # end
20
20
  class RedundantBegin < Cop
21
- MSG = 'Redundant `begin` block detected.'
22
-
23
- def on_def(node)
24
- _method_name, _args, body = *node
25
-
26
- check(body)
27
- end
21
+ include CheckMethods
28
22
 
29
- def on_defs(node)
30
- _scope, _method_name, _args, body = *node
31
-
32
- check(body)
33
- end
23
+ MSG = 'Redundant `begin` block detected.'
34
24
 
35
25
  private
36
26
 
37
- def check(node)
38
- return unless node && node.type == :kwbegin
27
+ def check(_node, _method_name, _args, body)
28
+ return unless body && body.type == :kwbegin
39
29
 
40
- convention(node, :begin)
30
+ add_offence(body, :begin)
41
31
  end
42
32
  end
43
33
  end
@@ -24,7 +24,7 @@ module Rubocop
24
24
 
25
25
  first_arg, = *args
26
26
 
27
- convention(first_arg, :expression) if first_arg == TARGET_NODE
27
+ add_offence(first_arg, :expression) if first_arg == TARGET_NODE
28
28
  end
29
29
  end
30
30
  end
@@ -21,19 +21,9 @@ module Rubocop
21
21
  # It should be extended to handle methods whose body is if/else
22
22
  # or a case expression with a default branch.
23
23
  class RedundantReturn < Cop
24
- MSG = 'Redundant `return` detected.'
25
-
26
- def on_def(node)
27
- _method_name, _args, body = *node
28
-
29
- check(body)
30
- end
24
+ include CheckMethods
31
25
 
32
- def on_defs(node)
33
- _scope, _method_name, _args, body = *node
34
-
35
- check(body)
36
- end
26
+ MSG = 'Redundant `return` detected.'
37
27
 
38
28
  private
39
29
 
@@ -49,13 +39,13 @@ module Rubocop
49
39
  end
50
40
  end
51
41
 
52
- def check(node)
53
- return unless node
42
+ def check(_node, _method_name, _args, body)
43
+ return unless body
54
44
 
55
- if node.type == :return
56
- check_return_node(node)
57
- elsif node.type == :begin
58
- expressions = *node
45
+ if body.type == :return
46
+ check_return_node(body)
47
+ elsif body.type == :begin
48
+ expressions = *body
59
49
  last_expr = expressions.last
60
50
 
61
51
  if last_expr && last_expr.type == :return
@@ -68,7 +58,7 @@ module Rubocop
68
58
  return if cop_config['AllowMultipleReturnValues'] &&
69
59
  node.children.size > 1
70
60
 
71
- convention(node, :keyword)
61
+ add_offence(node, :keyword)
72
62
  end
73
63
  end
74
64
  end
@@ -75,8 +75,8 @@ module Rubocop
75
75
  @local_variables = []
76
76
  end
77
77
 
78
- def on_arg(node)
79
- on_argument(node)
78
+ def on_args(node)
79
+ node.children.each { |arg| on_argument(arg) }
80
80
  end
81
81
 
82
82
  def on_blockarg(node)
@@ -94,9 +94,10 @@ module Rubocop
94
94
  receiver, method_name, *_args = *node
95
95
  if receiver && receiver.type == :self
96
96
  unless operator?(method_name) || keyword?(method_name) ||
97
+ constant_name?(method_name) ||
97
98
  @allowed_send_nodes.include?(node) ||
98
99
  @local_variables.include?(method_name)
99
- convention(node, :expression)
100
+ add_offence(node, :expression)
100
101
  end
101
102
  end
102
103
  end
@@ -128,6 +129,10 @@ module Rubocop
128
129
  :yield].include?(method_name)
129
130
  end
130
131
 
132
+ def constant_name?(method_name)
133
+ method_name.match(/^[A-Z]/)
134
+ end
135
+
131
136
  def allow_self(node)
132
137
  if node.type == :send
133
138
  receiver, _method_name, *_args = *node
@@ -8,14 +8,15 @@ module Rubocop
8
8
  # value of the configuration parameter MaxSlashes.
9
9
  class RegexpLiteral < Cop
10
10
  def on_regexp(node)
11
- slashes = node.loc.expression.source.count('/')
11
+ string_parts = node.children.select { |child| child.type == :str }
12
+ total_string = string_parts.map { |s| s.loc.expression.source }.join
13
+ slashes = total_string.count('/')
12
14
  msg = if node.loc.begin.is?('/')
13
- slashes -= 2 # subtract delimiters
14
15
  error_message('') if slashes > max_slashes
15
16
  else
16
17
  error_message('only ') if slashes <= max_slashes
17
18
  end
18
- convention(node, :expression, msg) if msg
19
+ add_offence(node, :expression, msg) if msg
19
20
  end
20
21
 
21
22
  def max_slashes
@@ -5,37 +5,29 @@ module Rubocop
5
5
  module Style
6
6
  # This cop checks for uses of rescue in its modifier form.
7
7
  class RescueModifier < Cop
8
+ include CheckMethods
9
+
8
10
  MSG = 'Avoid using rescue in its modifier form.'
9
11
 
10
12
  def on_rescue(node)
11
13
  return if ignored_node?(node)
12
14
 
13
- convention(node, :expression)
15
+ add_offence(node, :expression)
14
16
  end
15
17
 
16
18
  def on_kwbegin(node)
17
19
  body, *_ = *node
18
- check_rescue(body)
19
- end
20
-
21
- def on_def(node)
22
- _method_name, _args, body = *node
23
- check_rescue(body)
24
- end
25
-
26
- def on_defs(node)
27
- _receiver, _method_name, _args, body = *node
28
- check_rescue(body)
20
+ check(nil, nil, nil, body)
29
21
  end
30
22
 
31
- def check_rescue(node)
32
- return unless node
23
+ def check(_node, _method_name, _args, body)
24
+ return unless body
33
25
 
34
- case node.type
26
+ case body.type
35
27
  when :rescue
36
- ignore_node(node)
28
+ ignore_node(body)
37
29
  when :ensure
38
- first_child = node.children.first
30
+ first_child = body.children.first
39
31
  if first_child && first_child.type == :rescue
40
32
  ignore_node(first_child)
41
33
  end
@@ -10,20 +10,16 @@ module Rubocop
10
10
 
11
11
  def investigate(processed_source)
12
12
  return unless processed_source.ast
13
+ @processed_source = processed_source
13
14
 
14
- unless cop_config['AllowAsExpressionSeparator']
15
- check_for_expr_separator(processed_source)
16
- end
17
- check_for_line_terminator(processed_source)
15
+ check_for_line_terminator
18
16
  end
19
17
 
20
- private
21
-
22
- def check_for_expr_separator(processed_source)
23
- on_node(:begin, processed_source.ast) do |node|
18
+ def on_begin(node)
19
+ unless cop_config['AllowAsExpressionSeparator']
24
20
  exprs = node.children
25
21
 
26
- next if exprs.size < 2
22
+ return if exprs.size < 2
27
23
 
28
24
  # create a map matching lines to the number of expressions on them
29
25
  exprs_lines = exprs.map { |e| e.loc.expression.line }
@@ -35,31 +31,33 @@ module Rubocop
35
31
  # TODO: Find the correct position of the semicolon. We don't
36
32
  # know if the first semicolon on the line is a separator of
37
33
  # expressions. It's just a guess.
38
- column = processed_source[line - 1].index(';')
39
- convention(nil,
40
- source_range(processed_source.buffer,
41
- processed_source[0...(line - 1)],
42
- column, 1))
34
+ column = @processed_source[line - 1].index(';')
35
+ convention_on(line, column)
43
36
  end
44
37
  end
45
38
  end
46
39
  end
47
40
 
48
- def check_for_line_terminator(processed_source)
49
- tokens_for_lines = processed_source.tokens.group_by do |token|
41
+ private
42
+
43
+ def check_for_line_terminator
44
+ tokens_for_lines = @processed_source.tokens.group_by do |token|
50
45
  token.pos.line
51
46
  end
52
47
 
53
48
  tokens_for_lines.each do |line, tokens|
54
- if tokens.last.type == :tSEMI # rubocop:disable SymbolName
55
- column = tokens.last.pos.column
56
- convention(nil,
57
- source_range(processed_source.buffer,
58
- processed_source[0...(line - 1)],
59
- column, 1))
49
+ if tokens.last.type == :tSEMI
50
+ convention_on(line, tokens.last.pos.column)
60
51
  end
61
52
  end
62
53
  end
54
+
55
+ def convention_on(line, column)
56
+ add_offence(nil,
57
+ source_range(@processed_source.buffer,
58
+ @processed_source[0...(line - 1)], column,
59
+ 1))
60
+ end
63
61
  end
64
62
  end
65
63
  end