rubocop 0.13.1 → 0.14.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 (222) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/CHANGELOG.md +42 -0
  4. data/README.md +59 -54
  5. data/config/default.yml +124 -95
  6. data/config/disabled.yml +4 -0
  7. data/config/enabled.yml +286 -264
  8. data/lib/rubocop.rb +10 -3
  9. data/lib/rubocop/cli.rb +10 -171
  10. data/lib/rubocop/cop/cop.rb +14 -11
  11. data/lib/rubocop/cop/lint/debugger.rb +35 -0
  12. data/lib/rubocop/cop/lint/syntax.rb +34 -0
  13. data/lib/rubocop/cop/offence.rb +15 -23
  14. data/lib/rubocop/cop/style/align_array.rb +1 -10
  15. data/lib/rubocop/cop/style/align_hash.rb +14 -2
  16. data/lib/rubocop/cop/style/align_parameters.rb +1 -11
  17. data/lib/rubocop/cop/style/and_or.rb +13 -11
  18. data/lib/rubocop/cop/style/attr.rb +1 -1
  19. data/lib/rubocop/cop/style/autocorrect_alignment.rb +31 -6
  20. data/lib/rubocop/cop/style/blocks.rb +14 -6
  21. data/lib/rubocop/cop/style/character_literal.rb +1 -1
  22. data/lib/rubocop/cop/style/class_length.rb +38 -0
  23. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  24. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  25. data/lib/rubocop/cop/style/configurable_naming.rb +47 -0
  26. data/lib/rubocop/cop/style/def_parentheses.rb +2 -2
  27. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  28. data/lib/rubocop/cop/style/even_odd.rb +7 -3
  29. data/lib/rubocop/cop/style/hash_methods.rb +1 -1
  30. data/lib/rubocop/cop/style/hash_syntax.rb +36 -7
  31. data/lib/rubocop/cop/style/lambda_call.rb +36 -11
  32. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
  33. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +41 -0
  34. data/lib/rubocop/cop/style/method_length.rb +2 -13
  35. data/lib/rubocop/cop/style/method_name.rb +42 -0
  36. data/lib/rubocop/cop/style/numeric_literals.rb +4 -0
  37. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  38. data/lib/rubocop/cop/style/raise_args.rb +60 -0
  39. data/lib/rubocop/cop/style/redundant_exception.rb +32 -0
  40. data/lib/rubocop/cop/style/redundant_return.rb +14 -5
  41. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  42. data/lib/rubocop/cop/style/semicolon.rb +13 -4
  43. data/lib/rubocop/cop/style/signal_exception.rb +63 -14
  44. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  45. data/lib/rubocop/cop/style/string_help.rb +4 -1
  46. data/lib/rubocop/cop/style/string_literals.rb +34 -11
  47. data/lib/rubocop/cop/style/surrounding_space.rb +64 -17
  48. data/lib/rubocop/cop/style/tab.rb +1 -1
  49. data/lib/rubocop/cop/style/variable_name.rb +44 -0
  50. data/lib/rubocop/cop/style/when_then.rb +1 -1
  51. data/lib/rubocop/cop/style/while_until_do.rb +1 -1
  52. data/lib/rubocop/cop/team.rb +4 -7
  53. data/lib/rubocop/cop/util.rb +12 -0
  54. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  55. data/lib/rubocop/formatter/emacs_style_formatter.rb +5 -2
  56. data/lib/rubocop/formatter/json_formatter.rb +1 -0
  57. data/lib/rubocop/formatter/progress_formatter.rb +9 -13
  58. data/lib/rubocop/formatter/simple_text_formatter.rb +63 -15
  59. data/lib/rubocop/options.rb +184 -0
  60. data/lib/rubocop/processed_source.rb +4 -0
  61. data/lib/rubocop/version.rb +1 -1
  62. data/rubocop.gemspec +1 -1
  63. data/spec/rubocop/cli_spec.rb +870 -1001
  64. data/spec/rubocop/config_spec.rb +13 -13
  65. data/spec/rubocop/config_store_spec.rb +38 -37
  66. data/spec/rubocop/cop/commissioner_spec.rb +42 -46
  67. data/spec/rubocop/cop/cop_spec.rb +72 -77
  68. data/spec/rubocop/cop/corrector_spec.rb +51 -55
  69. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +100 -106
  70. data/spec/rubocop/cop/lint/block_alignment_spec.rb +403 -409
  71. data/spec/rubocop/cop/lint/debugger_spec.rb +39 -0
  72. data/spec/rubocop/cop/lint/empty_ensure_spec.rb +19 -25
  73. data/spec/rubocop/cop/lint/end_alignment_spec.rb +50 -56
  74. data/spec/rubocop/cop/lint/end_in_method_spec.rb +20 -26
  75. data/spec/rubocop/cop/lint/ensure_return_spec.rb +30 -36
  76. data/spec/rubocop/cop/lint/eval_spec.rb +25 -31
  77. data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +22 -28
  78. data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +51 -57
  79. data/spec/rubocop/cop/lint/loop_spec.rb +17 -23
  80. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +51 -57
  81. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +105 -111
  82. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +230 -236
  83. data/spec/rubocop/cop/lint/syntax_spec.rb +33 -0
  84. data/spec/rubocop/cop/lint/unreachable_code_spec.rb +51 -57
  85. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +1544 -1550
  86. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +21 -27
  87. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +119 -125
  88. data/spec/rubocop/cop/lint/void_spec.rb +50 -56
  89. data/spec/rubocop/cop/offence_spec.rb +101 -131
  90. data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +6 -12
  91. data/spec/rubocop/cop/rails/read_attribute_spec.rb +6 -12
  92. data/spec/rubocop/cop/rails/validation_spec.rb +13 -19
  93. data/spec/rubocop/cop/style/access_control_spec.rb +146 -152
  94. data/spec/rubocop/cop/style/alias_spec.rb +30 -36
  95. data/spec/rubocop/cop/style/align_array_spec.rb +57 -56
  96. data/spec/rubocop/cop/style/align_hash_spec.rb +256 -237
  97. data/spec/rubocop/cop/style/align_parameters_spec.rb +216 -202
  98. data/spec/rubocop/cop/style/and_or_spec.rb +51 -55
  99. data/spec/rubocop/cop/style/ascii_comments_spec.rb +14 -20
  100. data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +26 -32
  101. data/spec/rubocop/cop/style/attr_spec.rb +11 -17
  102. data/spec/rubocop/cop/style/begin_block_spec.rb +6 -12
  103. data/spec/rubocop/cop/style/block_comments_spec.rb +13 -19
  104. data/spec/rubocop/cop/style/block_nesting_spec.rb +134 -140
  105. data/spec/rubocop/cop/style/blocks_spec.rb +81 -84
  106. data/spec/rubocop/cop/style/case_equality_spec.rb +5 -11
  107. data/spec/rubocop/cop/style/case_indentation_spec.rb +71 -77
  108. data/spec/rubocop/cop/style/character_literal_spec.rb +31 -37
  109. data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +31 -37
  110. data/spec/rubocop/cop/style/class_length_spec.rb +65 -0
  111. data/spec/rubocop/cop/style/class_methods_spec.rb +35 -41
  112. data/spec/rubocop/cop/style/class_vars_spec.rb +11 -17
  113. data/spec/rubocop/cop/style/collection_methods_spec.rb +41 -47
  114. data/spec/rubocop/cop/style/colon_method_call_spec.rb +45 -51
  115. data/spec/rubocop/cop/style/comment_annotation_spec.rb +79 -85
  116. data/spec/rubocop/cop/style/constant_name_spec.rb +45 -51
  117. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +28 -34
  118. data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +23 -29
  119. data/spec/rubocop/cop/style/documentation_spec.rb +60 -66
  120. data/spec/rubocop/cop/style/dot_position_spec.rb +50 -56
  121. data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +109 -110
  122. data/spec/rubocop/cop/style/empty_lines_spec.rb +28 -34
  123. data/spec/rubocop/cop/style/empty_literal_spec.rb +80 -86
  124. data/spec/rubocop/cop/style/encoding_spec.rb +50 -56
  125. data/spec/rubocop/cop/style/end_block_spec.rb +6 -12
  126. data/spec/rubocop/cop/style/end_of_line_spec.rb +13 -19
  127. data/spec/rubocop/cop/style/even_odd_spec.rb +69 -41
  128. data/spec/rubocop/cop/style/favor_join_spec.rb +21 -27
  129. data/spec/rubocop/cop/style/favor_modifier_spec.rb +180 -176
  130. data/spec/rubocop/cop/style/favor_sprintf_spec.rb +41 -47
  131. data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +63 -69
  132. data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +32 -38
  133. data/spec/rubocop/cop/style/final_newline_spec.rb +16 -22
  134. data/spec/rubocop/cop/style/for_spec.rb +18 -24
  135. data/spec/rubocop/cop/style/global_vars_spec.rb +23 -29
  136. data/spec/rubocop/cop/style/hash_methods_spec.rb +39 -45
  137. data/spec/rubocop/cop/style/hash_syntax_spec.rb +93 -65
  138. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +11 -17
  139. data/spec/rubocop/cop/style/indentation_width_spec.rb +377 -383
  140. data/spec/rubocop/cop/style/lambda_call_spec.rb +40 -22
  141. data/spec/rubocop/cop/style/lambda_spec.rb +30 -34
  142. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +44 -50
  143. data/spec/rubocop/cop/style/line_length_spec.rb +11 -17
  144. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +14 -20
  145. data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +60 -0
  146. data/spec/rubocop/cop/style/method_length_spec.rb +127 -133
  147. data/spec/rubocop/cop/style/method_name_spec.rb +103 -0
  148. data/spec/rubocop/cop/style/module_function_spec.rb +16 -22
  149. data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +64 -70
  150. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +74 -80
  151. data/spec/rubocop/cop/style/nil_comparison_spec.rb +20 -26
  152. data/spec/rubocop/cop/style/not_spec.rb +13 -19
  153. data/spec/rubocop/cop/style/numeric_literals_spec.rb +36 -35
  154. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +5 -11
  155. data/spec/rubocop/cop/style/op_method_spec.rb +61 -67
  156. data/spec/rubocop/cop/style/parameter_lists_spec.rb +31 -37
  157. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +62 -68
  158. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +9 -15
  159. data/spec/rubocop/cop/style/proc_spec.rb +13 -19
  160. data/spec/rubocop/cop/style/raise_args_spec.rb +55 -0
  161. data/spec/rubocop/cop/style/reduce_arguments_spec.rb +49 -55
  162. data/spec/rubocop/cop/style/redundant_begin_spec.rb +47 -53
  163. data/spec/rubocop/cop/style/redundant_exception_spec.rb +27 -0
  164. data/spec/rubocop/cop/style/redundant_return_spec.rb +164 -72
  165. data/spec/rubocop/cop/style/redundant_self_spec.rb +109 -115
  166. data/spec/rubocop/cop/style/regexp_literal_spec.rb +57 -63
  167. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +99 -105
  168. data/spec/rubocop/cop/style/semicolon_spec.rb +95 -90
  169. data/spec/rubocop/cop/style/signal_exception_spec.rb +248 -95
  170. data/spec/rubocop/cop/style/single_line_methods_spec.rb +45 -51
  171. data/spec/rubocop/cop/style/space_after_colon_spec.rb +14 -20
  172. data/spec/rubocop/cop/style/space_after_comma_spec.rb +16 -22
  173. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +48 -54
  174. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +49 -55
  175. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +10 -16
  176. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +118 -50
  177. data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +18 -24
  178. data/spec/rubocop/cop/style/space_around_operators_spec.rb +204 -210
  179. data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +36 -42
  180. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +39 -45
  181. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +62 -68
  182. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +18 -24
  183. data/spec/rubocop/cop/style/special_global_vars_spec.rb +40 -46
  184. data/spec/rubocop/cop/style/string_literals_spec.rb +148 -70
  185. data/spec/rubocop/cop/style/symbol_array_spec.rb +28 -34
  186. data/spec/rubocop/cop/style/symbol_name_spec.rb +132 -138
  187. data/spec/rubocop/cop/style/tab_spec.rb +9 -15
  188. data/spec/rubocop/cop/style/ternary_operator_spec.rb +25 -31
  189. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +10 -16
  190. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +14 -20
  191. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +374 -380
  192. data/spec/rubocop/cop/style/unless_else_spec.rb +17 -23
  193. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +36 -42
  194. data/spec/rubocop/cop/style/variable_name_spec.rb +83 -0
  195. data/spec/rubocop/cop/style/when_then_spec.rb +31 -37
  196. data/spec/rubocop/cop/style/while_until_do_spec.rb +47 -53
  197. data/spec/rubocop/cop/style/word_array_spec.rb +49 -55
  198. data/spec/rubocop/cop/team_spec.rb +125 -127
  199. data/spec/rubocop/cop/variable_inspector/assignment_spec.rb +176 -180
  200. data/spec/rubocop/cop/variable_inspector/locatable_spec.rb +580 -586
  201. data/spec/rubocop/cop/variable_inspector/scope_spec.rb +140 -146
  202. data/spec/rubocop/cop/variable_inspector/variable_spec.rb +52 -58
  203. data/spec/rubocop/cop/variable_inspector/variable_table_spec.rb +217 -223
  204. data/spec/rubocop/cop/variable_inspector_spec.rb +15 -19
  205. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +35 -36
  206. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +2 -2
  207. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +24 -1
  208. data/spec/rubocop/formatter/file_list_formatter_spec.rb +1 -1
  209. data/spec/rubocop/formatter/formatter_set_spec.rb +3 -3
  210. data/spec/rubocop/formatter/json_formatter_spec.rb +7 -2
  211. data/spec/rubocop/formatter/offence_count_formatter_spec.rb +1 -1
  212. data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
  213. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +55 -6
  214. data/spec/rubocop/options_spec.rb +233 -0
  215. data/spec/rubocop/processed_source_spec.rb +85 -42
  216. data/spec/rubocop/source_parser_spec.rb +108 -110
  217. data/spec/rubocop/target_finder_spec.rb +125 -127
  218. data/spec/rubocop/token_spec.rb +15 -17
  219. metadata +32 -8
  220. data/lib/rubocop/backports/bsearch.rb +0 -39
  221. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +0 -77
  222. data/spec/rubocop/cop/style/method_and_variable_snake_case_spec.rb +0 -102
@@ -12,16 +12,7 @@ module Rubocop
12
12
  'than one line.'
13
13
 
14
14
  def on_array(node)
15
- first_element = node.children.first
16
-
17
- node.children.each_cons(2) do |prev, current|
18
- if current.loc.line != prev.loc.line
19
- @column_delta = first_element.loc.column - current.loc.column
20
- if current.loc.column != first_element.loc.column
21
- convention(current, :expression)
22
- end
23
- end
24
- end
15
+ check_alignment(node.children)
25
16
  end
26
17
  end
27
18
  end
@@ -14,12 +14,22 @@ module Rubocop
14
14
 
15
15
  if [cop_config['EnforcedHashRocketStyle'],
16
16
  cop_config['EnforcedColonStyle']].include?('table')
17
+
18
+ lines_of_the_children = node.children.map do |pair|
19
+ key, _value = *pair
20
+ key.loc.line
21
+ end
22
+ on_the_same_line = lines_of_the_children.uniq.size == 1
23
+ return if on_the_same_line
24
+
17
25
  key_widths = node.children.map do |pair|
18
26
  key, _value = *pair
19
27
  key.loc.expression.source.length
20
28
  end
21
29
  @max_key_width = key_widths.max
22
- if first_pair && value_delta(nil, first_pair, @max_key_width) != 0
30
+ if first_pair && !on_the_same_line &&
31
+ value_delta(nil, first_pair, @max_key_width) != 0
32
+
23
33
  @column_deltas = {}
24
34
  convention(first_pair, :expression)
25
35
  end
@@ -31,7 +41,7 @@ module Rubocop
31
41
  end
32
42
  end
33
43
 
34
- def autocorrect_action(node)
44
+ def autocorrect(node)
35
45
  # We can't use the instance variable inside the lambda. That would
36
46
  # just give each lambda the same reference and they would all get the
37
47
  # last value of each. Some local variables fix the problem.
@@ -120,6 +130,8 @@ module Rubocop
120
130
  key.loc.column +
121
131
  spaced_separator(current_pair).length +
122
132
  max_key_width
133
+ elsif first_pair.nil? # Only one pair?
134
+ value.loc.column
123
135
  else
124
136
  _key1, value1 = *first_pair
125
137
  value1.loc.column
@@ -17,17 +17,7 @@ module Rubocop
17
17
  return if method == :[]=
18
18
  return if args.size <= 1
19
19
 
20
- first_arg_column = args.first.loc.expression.column
21
-
22
- args.each_cons(2) do |prev, current|
23
- current_pos = current.loc.expression
24
-
25
- if current_pos.line > prev.loc.expression.line &&
26
- current_pos.column != first_arg_column
27
- @column_delta = first_arg_column - current_pos.column
28
- convention(current, current_pos)
29
- end
30
- end
20
+ check_alignment(args)
31
21
  end
32
22
  end
33
23
  end
@@ -30,23 +30,25 @@ module Rubocop
30
30
  end
31
31
  end
32
32
 
33
- def autocorrect_action(node)
34
- correction = lambda do |corrector|
35
- replacement = (node.type == :and ? '&&' : '||')
36
- corrector.replace(node.loc.operator, replacement)
37
- end
38
-
39
- new_source = rewrite_node(node, correction)
33
+ def autocorrect(node)
34
+ c = correction(node)
35
+ new_source = rewrite_node(node, c)
40
36
 
41
37
  # Make the correction only if it doesn't change the AST.
42
- if node == SourceParser.parse(new_source).ast
43
- @corrections << correction
44
- end
38
+ @corrections << c if node == SourceParser.parse(new_source).ast
45
39
  end
46
40
 
47
41
  def rewrite_node(node, correction)
48
42
  processed_source = SourceParser.parse(node.loc.expression.source)
49
- Corrector.new(processed_source.buffer, [correction]).rewrite
43
+ c = correction(processed_source.ast)
44
+ Corrector.new(processed_source.buffer, [c]).rewrite
45
+ end
46
+
47
+ def correction(node)
48
+ lambda do |corrector|
49
+ replacement = (node.type == :and ? '&&' : '||')
50
+ corrector.replace(node.loc.operator, replacement)
51
+ end
50
52
  end
51
53
  end
52
54
  end
@@ -11,7 +11,7 @@ module Rubocop
11
11
  convention(node, :selector) if command?(:attr, node)
12
12
  end
13
13
 
14
- def autocorrect_action(node)
14
+ def autocorrect(node)
15
15
  @corrections << lambda do |corrector|
16
16
  corrector.replace(node.loc.selector, 'attr_reader')
17
17
  end
@@ -7,7 +7,20 @@ module Rubocop
7
7
  # the left or to the right, amount being determined by the instance
8
8
  # variable @column_delta.
9
9
  module AutocorrectAlignment
10
- def autocorrect_action(node)
10
+ def check_alignment(items)
11
+ items.each_cons(2) do |prev, current|
12
+ if current.loc.line > prev.loc.line && start_of_line?(current.loc)
13
+ @column_delta = items.first.loc.column - current.loc.column
14
+ convention(current, :expression) if @column_delta != 0
15
+ end
16
+ end
17
+ end
18
+
19
+ def start_of_line?(loc)
20
+ loc.expression.source_line[0...loc.column] =~ /^\s*$/
21
+ end
22
+
23
+ def autocorrect(node)
11
24
  # We can't use the instance variable inside the lambda. That would
12
25
  # just give each lambda the same reference and they would all get
13
26
  # the last value of @column_delta. A local variable fixes the
@@ -17,12 +30,24 @@ module Rubocop
17
30
  @corrections << lambda do |corrector|
18
31
  expr = node.loc.expression
19
32
  if column_delta > 0
20
- corrector.replace(expr, ' ' * column_delta + expr.source)
33
+ corrector.replace(expr,
34
+ expr.source.gsub(/^/, ' ' * column_delta))
21
35
  else
22
- range = Parser::Source::Range.new(expr.source_buffer,
23
- expr.begin_pos + column_delta,
24
- expr.end_pos)
25
- corrector.replace(range, expr.source)
36
+ offset = 0
37
+ expr.source.each_line do |line|
38
+ b = expr.begin_pos + offset
39
+ if offset == 0
40
+ range = Parser::Source::Range.new(expr.source_buffer,
41
+ b + column_delta,
42
+ b + line.length)
43
+ corrector.replace(range, line)
44
+ else
45
+ range = Parser::Source::Range.new(expr.source_buffer,
46
+ b, b + line.length)
47
+ corrector.replace(range, line[-column_delta..-1])
48
+ end
49
+ offset += line.length
50
+ end
26
51
  end
27
52
  end
28
53
  end
@@ -35,14 +35,22 @@ module Rubocop
35
35
  end
36
36
  end
37
37
 
38
- def autocorrect_action(node)
38
+ def autocorrect(node)
39
39
  @corrections << lambda do |corrector|
40
- if node.loc.begin.is?('{')
41
- corrector.replace(node.loc.begin, 'do')
42
- corrector.replace(node.loc.end, 'end')
40
+ b, e = node.loc.begin, node.loc.end
41
+ if b.is?('{')
42
+ # If the left brace is immediately preceded by a word character,
43
+ # then we need a space before `do` to get valid Ruby code.
44
+ padding = if b.source_buffer.source[b.begin_pos - 1, 1] =~ /\w/
45
+ ' '
46
+ else
47
+ ''
48
+ end
49
+ corrector.replace(b, padding + 'do')
50
+ corrector.replace(e, 'end')
43
51
  else
44
- corrector.replace(node.loc.begin, '{')
45
- corrector.replace(node.loc.end, '}')
52
+ corrector.replace(b, '{')
53
+ corrector.replace(e, '}')
46
54
  end
47
55
  end
48
56
  end
@@ -15,7 +15,7 @@ module Rubocop
15
15
  node.loc.expression.source.size.between?(2, 3)
16
16
  end
17
17
 
18
- def autocorrect_action(node)
18
+ def autocorrect(node)
19
19
  @corrections << lambda do |corrector|
20
20
  string = node.loc.expression.source[1..-1]
21
21
 
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # This cop checks if the length a class exceeds some maximum value.
7
+ # Comment lines can optionally be ignored.
8
+ # The maximum allowed length is configurable.
9
+ class ClassLength < Cop
10
+ MSG = 'Class definition is too long. [%d/%d]'
11
+
12
+ def on_class(node)
13
+ check(node)
14
+ end
15
+
16
+ def max_length
17
+ cop_config['Max']
18
+ end
19
+
20
+ def count_comments?
21
+ cop_config['CountComments']
22
+ end
23
+
24
+ private
25
+
26
+ def check(node)
27
+ method_length = Util.source_length(node.loc.expression.source,
28
+ count_comments?)
29
+
30
+ if method_length > max_length
31
+ message = sprintf(MSG, method_length, max_length)
32
+ convention(node, :keyword, message)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -32,7 +32,7 @@ module Rubocop
32
32
  end
33
33
  end
34
34
 
35
- def autocorrect_action(node)
35
+ def autocorrect(node)
36
36
  @corrections << lambda do |corrector|
37
37
  corrector.replace(node.loc.selector,
38
38
  preferred_method(node.loc.selector.source))
@@ -22,7 +22,7 @@ module Rubocop
22
22
  method_name.match(/^[A-Z]/)
23
23
  end
24
24
 
25
- def autocorrect_action(node)
25
+ def autocorrect(node)
26
26
  @corrections << lambda do |corrector|
27
27
  corrector.replace(node.loc.dot, '.')
28
28
  end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # This module provides functionality for checking if names match the
7
+ # configured EnforcedStyle.
8
+ module ConfigurableNaming
9
+ SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
10
+ CAMEL_CASE = /^@?[a-z][\da-zA-Z]+[!?=]?$/
11
+
12
+ def check(node, range)
13
+ return unless range
14
+
15
+ name = range.source.to_sym
16
+ unless matches_config?(name) || Cop::OPERATOR_METHODS.include?(name)
17
+ convention(node, range, message(cop_config['EnforcedStyle']))
18
+ end
19
+ end
20
+
21
+ def matches_config?(name)
22
+ case cop_config['EnforcedStyle']
23
+ when 'snake_case'
24
+ name =~ SNAKE_CASE
25
+ when 'camelCase'
26
+ name =~ CAMEL_CASE
27
+ else
28
+ fail 'Illegal value for EnforcedStyle. Must be snake_case or ' +
29
+ 'camelCase.'
30
+ end
31
+ end
32
+
33
+ # Returns a range containing the method name after the given regexp and
34
+ # a dot.
35
+ def after_dot(node, method_name_length, regexp)
36
+ expr = node.loc.expression
37
+ match = /\A#{regexp}\s*\.\s*/.match(expr.source)
38
+ return unless match
39
+ offset = match[0].length
40
+ begin_pos = expr.begin_pos + offset
41
+ Parser::Source::Range.new(expr.source_buffer, begin_pos,
42
+ begin_pos + method_name_length)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -30,7 +30,7 @@ module Rubocop
30
30
  convention(args, :begin) if args.children == [] && args.loc.begin
31
31
  end
32
32
 
33
- def autocorrect_action(node)
33
+ def autocorrect(node)
34
34
  @corrections << lambda do |corrector|
35
35
  corrector.remove(node.loc.begin)
36
36
  corrector.remove(node.loc.end)
@@ -60,7 +60,7 @@ module Rubocop
60
60
  end
61
61
  end
62
62
 
63
- def autocorrect_action(node)
63
+ def autocorrect(node)
64
64
  @corrections << lambda do |corrector|
65
65
  corrector.insert_before(node.loc.expression, '(')
66
66
  corrector.insert_after(node.loc.expression, ')')
@@ -44,7 +44,7 @@ module Rubocop
44
44
  # TODO: Check block contents as well.
45
45
  alias_method :on_block, :ignore_node
46
46
 
47
- def autocorrect_action(node)
47
+ def autocorrect(node)
48
48
  @corrections << lambda do |corrector|
49
49
  name = case node
50
50
  when ARRAY_NODE then '[]'
@@ -24,13 +24,17 @@ module Rubocop
24
24
  def on_send(node)
25
25
  receiver, method, args = *node
26
26
 
27
- return unless method == :==
27
+ return unless [:==, :!=].include?(method)
28
28
  return unless div_by_2?(receiver)
29
29
 
30
30
  if args == ZERO
31
- convention(node, :expression, MSG_EVEN)
31
+ convention(node,
32
+ :expression,
33
+ method == :== ? MSG_EVEN : MSG_ODD)
32
34
  elsif args == ONE
33
- convention(node, :expression, MSG_ODD)
35
+ convention(node,
36
+ :expression,
37
+ method == :== ? MSG_ODD : MSG_EVEN)
34
38
  end
35
39
  end
36
40
 
@@ -20,7 +20,7 @@ module Rubocop
20
20
  end
21
21
  end
22
22
 
23
- def autocorrect_action(node)
23
+ def autocorrect(node)
24
24
  @corrections << lambda do |corrector|
25
25
  corrector.replace(node.loc.selector,
26
26
  proper_method_name(node.loc.selector.source))
@@ -3,14 +3,25 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  module Style
6
- # This cop checks for uses of the Ruby 1.8 hash literal syntax,
7
- # when the 1.9 syntax is applicable as well.
6
+ # This cop checks hash literal syntax.
7
+ #
8
+ # It can enforce either the use of the class hash rocket syntax or
9
+ # the use of the newer Ruby 1.9 syntax (when applicable).
8
10
  #
9
11
  # A separate offence is registered for each problematic pair.
10
12
  class HashSyntax < Cop
11
- MSG = 'Ruby 1.8 hash syntax detected'
13
+ MSG_19 = 'Use the new Ruby 1.9 hash syntax.'
14
+ MSG_HASH_ROCKETS = 'Always use hash rockets in hashes.'
12
15
 
13
16
  def on_hash(node)
17
+ case cop_config['EnforcedStyle']
18
+ when 'ruby19' then ruby19_check(node)
19
+ when 'hash_rockets' then hash_rockets_check(node)
20
+ else fail 'Unknown HashSyntax style'
21
+ end
22
+ end
23
+
24
+ def ruby19_check(node)
14
25
  pairs = *node
15
26
 
16
27
  sym_indices = pairs.all? { |p| word_symbol_pair?(p) }
@@ -19,16 +30,34 @@ module Rubocop
19
30
  pairs.each do |pair|
20
31
  if pair.loc.operator && pair.loc.operator.is?('=>')
21
32
  convention(pair,
22
- pair.loc.expression.begin.join(pair.loc.operator))
33
+ pair.loc.expression.begin.join(pair.loc.operator),
34
+ MSG_19)
23
35
  end
24
36
  end
25
37
  end
26
38
  end
27
39
 
28
- def autocorrect_action(node)
40
+ def hash_rockets_check(node)
41
+ pairs = *node
42
+
43
+ pairs.each do |pair|
44
+ if pair.loc.operator && pair.loc.operator.is?(':')
45
+ convention(pair,
46
+ pair.loc.expression.begin.join(pair.loc.operator),
47
+ MSG_HASH_ROCKETS)
48
+ end
49
+ end
50
+ end
51
+
52
+ def autocorrect(node)
29
53
  @corrections << lambda do |corrector|
30
- replacement = node.loc.expression.source[1..-1]
31
- .sub(/\s*=>\s*/, ': ')
54
+ if cop_config['EnforcedStyle'] == 'ruby19'
55
+ replacement = node.loc.expression.source[1..-1]
56
+ .sub(/\s*=>\s*/, ': ')
57
+ else
58
+ replacement = ':' + node.loc.expression.source
59
+ .sub(/:\s*/, ' => ')
60
+ end
32
61
  corrector.replace(node.loc.expression, replacement)
33
62
  end
34
63
  end