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
@@ -22,7 +22,7 @@ module Rubocop
22
22
  receiver, method_name, *_args = *node
23
23
 
24
24
  if receiver.nil? && BLACKLIST.include?(method_name)
25
- convention(node, :selector)
25
+ add_offence(node, :selector)
26
26
  end
27
27
  end
28
28
  end
@@ -7,6 +7,8 @@ module Rubocop
7
7
  # Modifiers should be indented as deeps are method definitions and
8
8
  # surrounded by blank lines.
9
9
  class AccessModifierIndentation < Cop
10
+ include ConfigurableEnforcedStyle
11
+
10
12
  MSG = '%s access modifiers like %s.'
11
13
 
12
14
  PRIVATE_NODE = s(:send, nil, :private)
@@ -27,24 +29,39 @@ module Rubocop
27
29
  # except other class/module nodes
28
30
  class_node.children.compact.each do |node|
29
31
  on_node(:send, node, [:class, :module, :sclass]) do |send_node|
30
- if modifier_node?(send_node)
31
- send_start_col = send_node.loc.expression.column
32
-
33
- if send_start_col != class_start_col + expected_indent_offset
34
- convention(send_node, :expression)
35
- end
32
+ if self.class.modifier_node?(send_node)
33
+ check(send_node, class_start_col)
36
34
  end
37
35
  end
38
36
  end
39
37
  end
40
38
  end
41
39
 
40
+ def self.modifier_node?(node)
41
+ [PRIVATE_NODE, PROTECTED_NODE, PUBLIC_NODE].include?(node)
42
+ end
43
+
42
44
  private
43
45
 
46
+ def check(send_node, class_start_col)
47
+ access_modifier_start_col = send_node.loc.expression.column
48
+ offset = access_modifier_start_col - class_start_col
49
+
50
+ if offset == expected_indent_offset
51
+ correct_style_detected
52
+ else
53
+ add_offence(send_node, :expression) do
54
+ if offset == unexpected_indent_offset
55
+ opposite_style_detected
56
+ else
57
+ unrecognized_style_detected
58
+ end
59
+ end
60
+ end
61
+ end
62
+
44
63
  def message(node)
45
- format(MSG,
46
- cop_config['EnforcedStyle'].capitalize,
47
- node.loc.selector.source)
64
+ format(MSG, style.capitalize, node.loc.selector.source)
48
65
  end
49
66
 
50
67
  def class_constructor?(block_node)
@@ -55,15 +72,13 @@ module Rubocop
55
72
  end
56
73
 
57
74
  def expected_indent_offset
58
- case cop_config['EnforcedStyle'].downcase
59
- when 'outdent' then 0
60
- when 'indent' then 2
61
- else fail 'Unknown EnforcedStyle specified'
62
- end
75
+ style == :outdent ? 0 : IndentationWidth::CORRECT_INDENTATION
63
76
  end
64
77
 
65
- def modifier_node?(node)
66
- [PRIVATE_NODE, PROTECTED_NODE, PUBLIC_NODE].include?(node)
78
+ # An offset that is not expected, but correct if the configuration is
79
+ # changed.
80
+ def unexpected_indent_offset
81
+ IndentationWidth::CORRECT_INDENTATION - expected_indent_offset
67
82
  end
68
83
  end
69
84
  end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # This cop makes sure that accessor methods are named properly.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # def set_attribute(value) ...
11
+ #
12
+ # # good
13
+ # def attribute=(value)
14
+ #
15
+ # # bad
16
+ # def get_attribute ...
17
+ #
18
+ # # good
19
+ # def attribute ...
20
+ class AccessorMethodName < 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
+ if bad_reader_name?(method_name, args)
35
+ add_offence(node, :name,
36
+ 'Do not prefix reader method names with get_.')
37
+ elsif bad_writer_name?(method_name, args)
38
+ add_offence(node, :name,
39
+ 'Do not prefix writer method names with set_.')
40
+ end
41
+ end
42
+
43
+ def bad_reader_name?(method_name, args)
44
+ method_name.start_with?('get_') && args.to_a.empty?
45
+ end
46
+
47
+ def bad_writer_name?(method_name, args)
48
+ method_name.start_with?('set_') && args.to_a.one?
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -10,7 +10,6 @@ module Rubocop
10
10
 
11
11
  def on_block(node)
12
12
  method, _args, body = *node
13
-
14
13
  _receiver, method_name = *method
15
14
 
16
15
  # using alias is the only option in certain scenarios
@@ -28,7 +27,20 @@ module Rubocop
28
27
 
29
28
  return if new.type == :gvar && old.type == :gvar
30
29
 
31
- convention(node, :keyword)
30
+ add_offence(node, :keyword)
31
+ end
32
+
33
+ def autocorrect(node)
34
+ @corrections << lambda do |corrector|
35
+ # replace alias with alias_method
36
+ corrector.replace(node.loc.keyword, 'alias_method')
37
+ # insert a comma
38
+ new, old = *node
39
+ corrector.insert_after(new.loc.expression, ',')
40
+ # convert bareword arguments to symbols
41
+ corrector.replace(new.loc.expression, ":#{new.children.first}")
42
+ corrector.replace(old.loc.expression, ":#{old.children.first}")
43
+ end
32
44
  end
33
45
  end
34
46
  end
@@ -46,6 +46,14 @@ module Rubocop
46
46
 
47
47
  private
48
48
 
49
+ def separator_delta(first_pair, current_separator, key_delta)
50
+ if current_separator.is?(':')
51
+ 0 # Colon follows directly after key
52
+ else
53
+ hash_rocket_delta(first_pair, current_separator) - key_delta
54
+ end
55
+ end
56
+
49
57
  def any_pairs_on_the_same_line?(node)
50
58
  lines_of_the_children = node.children.map do |pair|
51
59
  key, _value = *pair
@@ -87,13 +95,9 @@ module Rubocop
87
95
  first_pair.loc.column - current_pair.loc.column
88
96
  end
89
97
 
90
- def separator_delta(first_pair, current_separator, key_delta)
91
- if current_separator.is?(':')
92
- 0 # Colon follows directly after key
93
- else
94
- first_pair.loc.column + @max_key_width + 1 -
95
- current_separator.column - key_delta
96
- end
98
+ def hash_rocket_delta(first_pair, current_separator)
99
+ first_pair.loc.column + @max_key_width + 1 -
100
+ current_separator.column
97
101
  end
98
102
 
99
103
  def value_delta(first_pair, current_pair)
@@ -126,13 +130,8 @@ module Rubocop
126
130
  key.loc.column + key.loc.expression.source.length
127
131
  end
128
132
 
129
- def separator_delta(first_pair, current_separator, key_delta)
130
- if current_separator.is?(':')
131
- 0 # Colon follows directly after key
132
- else
133
- first_pair.loc.operator.column - current_separator.column -
134
- key_delta
135
- end
133
+ def hash_rocket_delta(first_pair, current_separator)
134
+ first_pair.loc.operator.column - current_separator.column
136
135
  end
137
136
 
138
137
  def value_delta(first_pair, current_pair)
@@ -161,12 +160,12 @@ module Rubocop
161
160
 
162
161
  @column_deltas = alignment_for(first_pair)
163
162
  .deltas_for_first_pair(first_pair, node)
164
- convention(first_pair, :expression) unless good_alignment?
163
+ add_offence(first_pair, :expression) unless good_alignment?
165
164
 
166
165
  node.children.each_cons(2) do |prev, current|
167
166
  @column_deltas = alignment_for(current).deltas(first_pair, prev,
168
167
  current)
169
- convention(current, :expression) unless good_alignment?
168
+ add_offence(current, :expression) unless good_alignment?
170
169
  end
171
170
  end
172
171
 
@@ -24,9 +24,9 @@ module Rubocop
24
24
  op_type = node.type.to_s
25
25
 
26
26
  if op == op_type
27
- convention(node,
28
- :operator,
29
- sprintf(MSG, OPS[op], op))
27
+ add_offence(node,
28
+ :operator,
29
+ sprintf(MSG, OPS[op], op))
30
30
  end
31
31
  end
32
32
 
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # Common code for ordinary arrays with [] that can be written with %
7
+ # syntax.
8
+ module ArraySyntax
9
+ def array_of?(element_type, node)
10
+ return false unless node.loc.begin && node.loc.begin.is?('[')
11
+
12
+ array_elems = node.children
13
+
14
+ # no need to check empty arrays
15
+ return false unless array_elems && array_elems.size > 1
16
+
17
+ array_elems.all? { |e| e.type == element_type }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -10,7 +10,7 @@ module Rubocop
10
10
 
11
11
  def investigate(processed_source)
12
12
  processed_source.comments.each do |comment|
13
- convention(comment, :expression) unless comment.text.ascii_only?
13
+ add_offence(comment, :expression) unless comment.text.ascii_only?
14
14
  end
15
15
  end
16
16
  end
@@ -1,7 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
- # rubocop:disable SymbolName
4
-
5
3
  module Rubocop
6
4
  module Cop
7
5
  module Style
@@ -12,7 +10,7 @@ module Rubocop
12
10
  def investigate(processed_source)
13
11
  processed_source.tokens.each do |t|
14
12
  if t.type == :tIDENTIFIER && !t.text.ascii_only?
15
- convention(nil, t.pos)
13
+ add_offence(nil, t.pos)
16
14
  end
17
15
  end
18
16
  end
@@ -8,7 +8,7 @@ module Rubocop
8
8
  MSG = 'Do not use `attr`. Use `attr_reader` instead.'
9
9
 
10
10
  def on_send(node)
11
- convention(node, :selector) if command?(:attr, node)
11
+ add_offence(node, :selector) if command?(:attr, node)
12
12
  end
13
13
 
14
14
  def autocorrect(node)
@@ -11,7 +11,7 @@ module Rubocop
11
11
  items.each_cons(2) do |prev, current|
12
12
  if current.loc.line > prev.loc.line && start_of_line?(current.loc)
13
13
  @column_delta = items.first.loc.column - current.loc.column
14
- convention(current, :expression) if @column_delta != 0
14
+ add_offence(current, :expression) if @column_delta != 0
15
15
  end
16
16
  end
17
17
  end
@@ -30,17 +30,7 @@ module Rubocop
30
30
  @corrections << lambda do |corrector|
31
31
  expr = node.loc.expression
32
32
  each_line(expr) do |line_begin_pos, line|
33
- starts_with_space =
34
- expr.source_buffer.source[line_begin_pos] =~ / /
35
- pos_to_remove = if column_delta > 0 || starts_with_space
36
- line_begin_pos
37
- else
38
- line_begin_pos - column_delta.abs
39
- end
40
- range = Parser::Source::Range.new(expr.source_buffer,
41
- pos_to_remove,
42
- pos_to_remove +
43
- column_delta.abs)
33
+ range = calculate_range(expr, line_begin_pos, column_delta)
44
34
  if column_delta > 0
45
35
  corrector.insert_before(range, ' ' * column_delta)
46
36
  else
@@ -50,6 +40,17 @@ module Rubocop
50
40
  end
51
41
  end
52
42
 
43
+ def calculate_range(expr, line_begin_pos, column_delta)
44
+ starts_with_space = expr.source_buffer.source[line_begin_pos] =~ / /
45
+ pos_to_remove = if column_delta > 0 || starts_with_space
46
+ line_begin_pos
47
+ else
48
+ line_begin_pos - column_delta.abs
49
+ end
50
+ Parser::Source::Range.new(expr.source_buffer, pos_to_remove,
51
+ pos_to_remove + column_delta.abs)
52
+ end
53
+
53
54
  def remove(range, corrector)
54
55
  original_stderr = $stderr
55
56
  $stderr = StringIO.new # Avoid error messages on console
@@ -8,7 +8,7 @@ module Rubocop
8
8
  MSG = 'Avoid the use of BEGIN blocks.'
9
9
 
10
10
  def on_preexe(node)
11
- convention(node, :keyword)
11
+ add_offence(node, :keyword)
12
12
  end
13
13
  end
14
14
  end
@@ -10,7 +10,7 @@ module Rubocop
10
10
  def investigate(processed_source)
11
11
  processed_source.comments.each do |comment|
12
12
  if comment.text.start_with?('=begin')
13
- convention(comment, :expression)
13
+ add_offence(comment, :expression)
14
14
  end
15
15
  end
16
16
  end
@@ -9,6 +9,8 @@ module Rubocop
9
9
  #
10
10
  # The maximum level of nesting allowed is configurable.
11
11
  class BlockNesting < Cop
12
+ include ConfigurableMax
13
+
12
14
  NESTING_BLOCKS = [:case, :if, :while, :while_post, :until, :until_post,
13
15
  :for, :resbody]
14
16
 
@@ -27,7 +29,9 @@ module Rubocop
27
29
  current_level += 1
28
30
  end
29
31
  if current_level == max + 1
30
- convention(node, :expression, message(max))
32
+ add_offence(node, :expression, message(max)) do
33
+ self.max = current_level
34
+ end
31
35
  return
32
36
  end
33
37
  end
@@ -13,7 +13,7 @@ module Rubocop
13
13
  _receiver, method_name, *args = *node
14
14
  if args.any?
15
15
  block = get_block(args.last)
16
- if block && !has_parentheses?(node) && !operator?(method_name)
16
+ if block && !parentheses?(node) && !operator?(method_name)
17
17
  # If there are no parentheses around the arguments, then braces
18
18
  # and do-end have different meaning due to how they bind, so we
19
19
  # allow either.
@@ -29,9 +29,9 @@ module Rubocop
29
29
  block_begin = node.loc.begin.source
30
30
 
31
31
  if block_length > 0 && block_begin == '{'
32
- convention(node, :begin, MULTI_LINE_MSG)
32
+ add_offence(node, :begin, MULTI_LINE_MSG)
33
33
  elsif block_length == 0 && block_begin != '{'
34
- convention(node, :begin, SINGLE_LINE_MSG)
34
+ add_offence(node, :begin, SINGLE_LINE_MSG)
35
35
  end
36
36
  end
37
37
 
@@ -65,7 +65,7 @@ module Rubocop
65
65
  end
66
66
  end
67
67
 
68
- def has_parentheses?(send_node)
68
+ def parentheses?(send_node)
69
69
  send_node.loc.begin
70
70
  end
71
71
 
@@ -5,41 +5,67 @@ module Rubocop
5
5
  module Style
6
6
  # This cop checks for braces in method calls with hash parameters.
7
7
  class BracesAroundHashParameters < Cop
8
+ include ConfigurableEnforcedStyle
9
+
8
10
  def on_send(node)
9
11
  _receiver, method_name, *args = *node
10
12
 
11
- # discard attr writer methods.
13
+ # Discard attr writer methods.
12
14
  return if method_name.to_s.end_with?('=')
13
- # discard operator methods
15
+ # Discard operator methods.
14
16
  return if OPERATOR_METHODS.include?(method_name)
15
17
 
16
- # we care only for the first argument
18
+ # We care only for the last argument.
17
19
  arg = args.last
18
- return unless arg && arg.type == :hash && arg.children.any?
19
-
20
- has_braces = !arg.loc.begin.nil?
21
- all_hashes = args.length > 1 && args.all? { |a| a.type == :hash }
22
-
23
- if style == :no_braces && has_braces && !all_hashes
24
- convention(arg,
25
- :expression,
26
- 'Redundant curly braces around a hash parameter.')
27
- elsif style == :braces && !has_braces
28
- convention(arg,
29
- :expression,
30
- 'Missing curly braces around a hash parameter.')
31
- end
20
+
21
+ check(arg, args) if non_empty_hash?(arg)
32
22
  end
33
23
 
34
24
  private
35
25
 
36
- def style
37
- case cop_config['EnforcedStyle']
38
- when 'braces' then :braces
39
- when 'no_braces' then :no_braces
40
- else fail 'Unknown style selected!'
26
+ def check(arg, args)
27
+ if style == :no_braces
28
+ if !braces?(arg) || all_hashes?(args)
29
+ correct_style_detected
30
+ else
31
+ add_offence(arg, :expression,
32
+ 'Redundant curly braces around a hash parameter.') do
33
+ opposite_style_detected
34
+ end
35
+ end
36
+ elsif braces?(arg)
37
+ correct_style_detected
38
+ else
39
+ add_offence(arg, :expression,
40
+ 'Missing curly braces around a hash parameter.') do
41
+ opposite_style_detected
42
+ end
43
+ end
44
+ end
45
+
46
+ def autocorrect(node)
47
+ @corrections << lambda do |corrector|
48
+ if style == :no_braces
49
+ corrector.remove(node.loc.begin)
50
+ corrector.remove(node.loc.end)
51
+ elsif style == :braces
52
+ corrector.insert_before(node.loc.expression, '{')
53
+ corrector.insert_after(node.loc.expression, '}')
54
+ end
41
55
  end
42
56
  end
57
+
58
+ def non_empty_hash?(arg)
59
+ arg && arg.type == :hash && arg.children.any?
60
+ end
61
+
62
+ def braces?(arg)
63
+ !arg.loc.begin.nil?
64
+ end
65
+
66
+ def all_hashes?(args)
67
+ args.length > 1 && args.all? { |a| a.type == :hash }
68
+ end
43
69
  end
44
70
  end
45
71
  end