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
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ # Common functionality for checking if nodes.
6
+ module IfNode
7
+ def modifier_if?(node)
8
+ node.loc.end.nil?
9
+ end
10
+
11
+ def ternary_op?(node)
12
+ node.loc.respond_to?(:question)
13
+ end
14
+
15
+ def elsif?(node)
16
+ node.loc.respond_to?(:keyword) && node.loc.keyword &&
17
+ node.loc.keyword.is?('elsif')
18
+ end
19
+
20
+ def if_else?(node)
21
+ node.loc.respond_to?(:else) && node.loc.else
22
+ end
23
+ end
24
+ end
25
+ end
@@ -17,7 +17,7 @@ module Rubocop
17
17
  # We won't check modifier or ternary conditionals.
18
18
  if node.loc.expression.source =~ /\A(if|unless)\b/
19
19
  if offending_line(node)
20
- convention(node, :expression, error_message)
20
+ add_offence(node, :expression, error_message)
21
21
  end
22
22
  end
23
23
  end
@@ -13,11 +13,19 @@ module Rubocop
13
13
  # end
14
14
  # end
15
15
  class IndentationWidth < Cop
16
+ include CheckMethods
17
+ include CheckAssignment
18
+ include IfNode
19
+
16
20
  CORRECT_INDENTATION = 2
17
- MSG = "Use #{CORRECT_INDENTATION} (not %d) spaces for indentation."
21
+
22
+ def on_begin(node)
23
+ check_consistent(node)
24
+ end
18
25
 
19
26
  def on_kwbegin(node)
20
- node.children.each { |c| check_indentation(node.loc.end, c) }
27
+ check_indentation(node.loc.end, node.children.first)
28
+ check_consistent(node)
21
29
  end
22
30
 
23
31
  def on_block(node)
@@ -37,13 +45,7 @@ module Rubocop
37
45
  members.each { |m| check_indentation(node.loc.keyword, m) }
38
46
  end
39
47
 
40
- def on_def(node)
41
- _method_name, _args, body = *node
42
- check_indentation(node.loc.keyword, body)
43
- end
44
-
45
- def on_defs(node)
46
- _scope, _method_name, _args, body = *node
48
+ def check(node, _method_name, _args, body)
47
49
  check_indentation(node.loc.keyword, body)
48
50
  end
49
51
 
@@ -54,7 +56,9 @@ module Rubocop
54
56
 
55
57
  def on_while(node)
56
58
  _condition, body = *node
57
- check_indentation(node.loc.keyword, body)
59
+ if node.loc.keyword.begin_pos == node.loc.expression.begin_pos
60
+ check_indentation(node.loc.keyword, body)
61
+ end
58
62
  end
59
63
 
60
64
  alias_method :on_until, :on_while
@@ -76,7 +80,8 @@ module Rubocop
76
80
  end
77
81
  end
78
82
 
79
- def on_if(node)
83
+ def on_if(node, offset = 0)
84
+ return if ignored_node?(node)
80
85
  return if ternary_op?(node)
81
86
  return if modifier_if?(node)
82
87
 
@@ -86,60 +91,89 @@ module Rubocop
86
91
  else _condition, body = *node
87
92
  end
88
93
 
89
- check_if(node, body, else_clause) if body
94
+ check_if(node, body, else_clause, offset) if body
90
95
  end
91
96
 
92
97
  private
93
98
 
94
- def check_if(node, body, else_clause)
99
+ def check_assignment(node, rhs)
100
+ # If there are method calls chained to the right hand side of the
101
+ # assignment, we let rhs be the receiver of those method calls before
102
+ # we check its indentation.
103
+ rhs = first_part_of_call_chain(rhs)
104
+
105
+ if rhs && rhs.type == :if
106
+ on_if(rhs, rhs.loc.column - node.loc.column)
107
+ ignore_node(rhs)
108
+ end
109
+ end
110
+
111
+ def check_if(node, body, else_clause, offset)
95
112
  return if ternary_op?(node)
96
113
  # Don't check if expression is on same line as "then" keyword.
97
- check_indentation(node.loc.keyword, body)
114
+ check_indentation(node.loc.keyword, body, offset)
98
115
  if else_clause
99
116
  if elsif?(else_clause)
100
117
  _condition, inner_body, inner_else_clause = *else_clause
101
- check_if(else_clause, inner_body, inner_else_clause)
118
+ check_if(else_clause, inner_body, inner_else_clause, offset)
102
119
  else
103
- check_indentation(node.loc.keyword, else_clause)
120
+ check_indentation(node.loc.else, else_clause)
104
121
  end
105
122
  end
106
123
  end
107
124
 
108
- def modifier_if?(node)
109
- node.loc.end.nil?
110
- end
111
-
112
- def ternary_op?(node)
113
- node.loc.respond_to?(:question)
114
- end
115
-
116
- def elsif?(node)
117
- node.loc.respond_to?(:keyword) && node.loc.keyword &&
118
- node.loc.keyword.is?('elsif')
119
- end
120
-
121
- def check_indentation(base_loc, body_node)
125
+ def check_indentation(base_loc, body_node, offset = 0)
122
126
  return unless body_node
123
127
  return if body_node.loc.line == base_loc.line
128
+ return if starts_with_access_modifier?(body_node)
129
+
124
130
  # Don't check indentation if the line doesn't start with the body.
125
131
  # For example lines like "else do_something".
126
132
  first_char_pos_on_line = body_node.loc.expression.source_line =~ /\S/
127
133
  return unless body_node.loc.column == first_char_pos_on_line
128
134
 
129
135
  indentation = body_node.loc.column - base_loc.column
130
- if indentation != CORRECT_INDENTATION
131
- expr = body_node.loc.expression
132
- begin_pos, end_pos = if indentation >= 0
133
- [expr.begin_pos - indentation,
134
- expr.begin_pos]
135
- else
136
- [expr.begin_pos,
137
- expr.begin_pos - indentation]
138
- end
139
- convention(nil,
140
- Parser::Source::Range.new(expr.source_buffer,
141
- begin_pos, end_pos),
142
- sprintf(MSG, indentation))
136
+ return if indentation == CORRECT_INDENTATION ||
137
+ indentation + offset == CORRECT_INDENTATION
138
+
139
+ expr = body_node.loc.expression
140
+ begin_pos, end_pos =
141
+ if indentation >= 0
142
+ [expr.begin_pos - indentation, expr.begin_pos]
143
+ else
144
+ [expr.begin_pos, expr.begin_pos - indentation]
145
+ end
146
+
147
+ add_offence(nil,
148
+ Parser::Source::Range.new(expr.source_buffer,
149
+ begin_pos, end_pos),
150
+ sprintf("Use #{CORRECT_INDENTATION} (not %d) spaces " +
151
+ 'for indentation.', indentation))
152
+ end
153
+
154
+ def starts_with_access_modifier?(body_node)
155
+ body_node.type == :begin &&
156
+ AccessModifierIndentation.modifier_node?(body_node.children.first)
157
+ end
158
+
159
+ def check_consistent(node)
160
+ children_to_check = node.children.reject do |child|
161
+ # Don't check nodes that have special indentation and will be
162
+ # checked by the AccessModifierIndentation cop.
163
+ AccessModifierIndentation.modifier_node?(child)
164
+ end
165
+
166
+ children_to_check.map(&:loc).each_cons(2) do |child1, child2|
167
+ if child2.line > child1.line && child2.column != child1.column
168
+ expr = child2.expression
169
+ indentation = expr.source_line =~ /\S/
170
+ end_pos = expr.begin_pos
171
+ begin_pos = end_pos - indentation
172
+ add_offence(nil,
173
+ Parser::Source::Range.new(expr.source_buffer,
174
+ begin_pos, end_pos),
175
+ 'Inconsistent indentation detected.')
176
+ end
143
177
  end
144
178
  end
145
179
  end
@@ -24,9 +24,9 @@ module Rubocop
24
24
  lambda_length = lambda_length(node)
25
25
 
26
26
  if selector != '->' && lambda_length == 0
27
- convention(block_method, :expression, SINGLE_MSG)
27
+ add_offence(block_method, :expression, SINGLE_MSG)
28
28
  elsif selector == '->' && lambda_length > 0
29
- convention(block_method, :expression, MULTI_MSG)
29
+ add_offence(block_method, :expression, MULTI_MSG)
30
30
  end
31
31
  end
32
32
  end
@@ -13,6 +13,8 @@ module Rubocop
13
13
  # # good
14
14
  # lambda.call(x, y)
15
15
  class LambdaCall < Cop
16
+ include ConfigurableEnforcedStyle
17
+
16
18
  def on_send(node)
17
19
  _receiver, selector, = *node
18
20
 
@@ -21,9 +23,9 @@ module Rubocop
21
23
 
22
24
  if style == :call && node.loc.selector.nil?
23
25
  # lambda.() does not have a selector
24
- convention(node, :expression)
26
+ add_offence(node, :expression)
25
27
  elsif style == :braces && node.loc.selector
26
- convention(node, :expression)
28
+ add_offence(node, :expression)
27
29
  end
28
30
  end
29
31
 
@@ -48,16 +50,6 @@ module Rubocop
48
50
  'Prefer the use of `lambda.(...)` over `lambda.call(...)`.'
49
51
  end
50
52
  end
51
-
52
- private
53
-
54
- def style
55
- case cop_config['EnforcedStyle']
56
- when 'call' then :call
57
- when 'braces' then :braces
58
- else fail 'Unknown style selected!'
59
- end
60
- end
61
53
  end
62
54
  end
63
55
  end
@@ -14,7 +14,7 @@ module Rubocop
14
14
  processed_source.comments.each do |comment|
15
15
  if comment.text =~ /^#+[^#\s:+-]/
16
16
  unless comment.text.start_with?('#!') && comment.loc.line == 1
17
- convention(comment, :expression)
17
+ add_offence(comment, :expression)
18
18
  end
19
19
  end
20
20
  end
@@ -6,17 +6,21 @@ module Rubocop
6
6
  # This cop checks the length of lines in the source code.
7
7
  # The maximum length is configurable.
8
8
  class LineLength < Cop
9
+ include ConfigurableMax
10
+
9
11
  MSG = 'Line is too long. [%d/%d]'
10
12
 
11
13
  def investigate(processed_source)
12
14
  processed_source.lines.each_with_index do |line, index|
13
15
  if line.length > max
14
16
  message = sprintf(MSG, line.length, max)
15
- convention(nil,
16
- source_range(processed_source.buffer,
17
- processed_source[0...index], max,
18
- line.length - max),
19
- message)
17
+ add_offence(nil,
18
+ source_range(processed_source.buffer,
19
+ processed_source[0...index], max,
20
+ line.length - max),
21
+ message) do
22
+ self.max = line.length
23
+ end
20
24
  end
21
25
  end
22
26
  end
@@ -13,7 +13,7 @@ module Rubocop
13
13
  # methods starting with a capital letter should be skipped
14
14
  return if method_name =~ /\A[A-Z]/
15
15
 
16
- convention(node, :begin) if args.empty? && node.loc.begin
16
+ add_offence(node, :begin) if args.empty? && node.loc.begin
17
17
  end
18
18
 
19
19
  def autocorrect(node)
@@ -32,7 +32,7 @@ module Rubocop
32
32
  range = Parser::Source::Range.new(receiver.loc.end.source_buffer,
33
33
  receiver.loc.end.begin_pos,
34
34
  node.loc.expression.end_pos)
35
- convention(nil, range)
35
+ add_offence(nil, range)
36
36
  end
37
37
  end
38
38
  end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # This cops checks for parentheses around the arguments in method
7
+ # definitions. Both instance and class/singleton methods are checked.
8
+ class MethodDefParentheses < Cop
9
+ include CheckMethods
10
+ include ConfigurableEnforcedStyle
11
+
12
+ def check(node, _method_name, args, _body)
13
+ if style == :require_parentheses &&
14
+ arguments?(args) &&
15
+ !parentheses?(args)
16
+ add_offence(node,
17
+ args.loc.expression,
18
+ 'Use def with parentheses when there are parameters.')
19
+ elsif style == :require_no_parentheses && parentheses?(args)
20
+ add_offence(args,
21
+ :expression,
22
+ 'Use def without parentheses.')
23
+ end
24
+ end
25
+
26
+ def autocorrect(node)
27
+ @corrections << lambda do |corrector|
28
+ if style == :require_parentheses
29
+ corrector.insert_after(node.children[1].loc.expression, ')')
30
+ expression = node.loc.expression
31
+ replacement = expression.source.sub(/(def\s+\S+)\s+/, '\1(')
32
+ corrector.replace(expression, replacement)
33
+ elsif style == :require_no_parentheses
34
+ corrector.replace(node.loc.begin, ' ')
35
+ corrector.remove(node.loc.end)
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def arguments?(args)
43
+ args.children.size > 0
44
+ end
45
+
46
+ def parentheses?(args)
47
+ args.loc.begin
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -7,34 +7,21 @@ module Rubocop
7
7
  # Comment lines can optionally be ignored.
8
8
  # The maximum allowed length is configurable.
9
9
  class MethodLength < Cop
10
- MSG = 'Method has too many lines. [%d/%d]'
10
+ include CheckMethods
11
+ include CodeLength
11
12
 
12
- def on_def(node)
13
- check(node)
14
- end
15
-
16
- def on_defs(node)
17
- check(node)
18
- end
19
-
20
- def max_length
21
- cop_config['Max']
22
- end
13
+ private
23
14
 
24
- def count_comments?
25
- cop_config['CountComments']
15
+ def message
16
+ 'Method has too many lines. [%d/%d]'
26
17
  end
27
18
 
28
- private
19
+ def code_length(node)
20
+ lines = node.loc.expression.source.lines.to_a[1..-2] || []
29
21
 
30
- def check(node)
31
- method_length = Util.source_length(node.loc.expression.source,
32
- count_comments?)
22
+ lines.reject! { |line| irrelevant_line(line) }
33
23
 
34
- if method_length > max_length
35
- message = sprintf(MSG, method_length, max_length)
36
- convention(node, :keyword, message)
37
- end
24
+ lines.size
38
25
  end
39
26
  end
40
27
  end
@@ -22,7 +22,7 @@ module Rubocop
22
22
 
23
23
  if body && body.type == :begin
24
24
  body.children.each do |body_node|
25
- convention(body_node, :expression) if body_node == TARGET_NODE
25
+ add_offence(body_node, :expression) if body_node == TARGET_NODE
26
26
  end
27
27
  end
28
28
  end
@@ -30,7 +30,7 @@ module Rubocop
30
30
  Parser::Source::Range.new(end_kw_loc.source_buffer,
31
31
  end_kw_loc.begin_pos,
32
32
  method.loc.expression.end_pos)
33
- convention(nil, range)
33
+ add_offence(nil, range)
34
34
  # Done. If there are more blocks in the chain, they will be
35
35
  # found by subsequent calls to on_block.
36
36
  break
@@ -29,13 +29,23 @@ module Rubocop
29
29
  end
30
30
  right_after_cond =
31
31
  Parser::Source::Range.new(next_thing.source_buffer,
32
- condition.loc.expression.end.end_pos,
32
+ end_position(condition),
33
33
  next_thing.begin_pos)
34
34
  if right_after_cond.source =~ /\A\s*then\s*(#.*)?\s*\n/
35
35
  node.loc.expression.begin.line
36
36
  end
37
37
  end
38
38
 
39
+ def end_position(conditional_node)
40
+ node = if conditional_node.type == :match_current_line
41
+ conditional_node.children.first
42
+ else
43
+ conditional_node
44
+ end
45
+
46
+ node.loc.expression.end.end_pos
47
+ end
48
+
39
49
  def error_message
40
50
  'Never use then for multi-line if/unless.'
41
51
  end