rubocop 0.26.1 → 0.27.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 (155) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -0
  3. data/.rubocop_todo.yml +10 -6
  4. data/.travis.yml +2 -0
  5. data/CHANGELOG.md +30 -0
  6. data/README.md +9 -2
  7. data/assets/logo.png +0 -0
  8. data/assets/output.html.erb +68 -65
  9. data/config/default.yml +42 -7
  10. data/config/disabled.yml +5 -0
  11. data/config/enabled.yml +32 -7
  12. data/lib/rubocop.rb +10 -2
  13. data/lib/rubocop/comment_config.rb +11 -17
  14. data/lib/rubocop/config.rb +20 -16
  15. data/lib/rubocop/config_loader.rb +8 -12
  16. data/lib/rubocop/cop/cop.rb +13 -12
  17. data/lib/rubocop/cop/lint/block_alignment.rb +4 -6
  18. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -2
  19. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  20. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -3
  21. data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
  22. data/lib/rubocop/cop/metrics/abc_size.rb +27 -0
  23. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -4
  24. data/lib/rubocop/cop/metrics/class_length.rb +1 -1
  25. data/lib/rubocop/cop/metrics/line_length.rb +2 -5
  26. data/lib/rubocop/cop/metrics/method_length.rb +2 -2
  27. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +24 -15
  28. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +15 -2
  29. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +63 -0
  30. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  31. data/lib/rubocop/cop/mixin/if_node.rb +3 -1
  32. data/lib/rubocop/cop/mixin/method_complexity.rb +3 -3
  33. data/lib/rubocop/cop/mixin/{on_method.rb → on_method_def.rb} +3 -3
  34. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -2
  35. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  36. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  37. data/lib/rubocop/cop/mixin/string_literals_help.rb +28 -0
  38. data/lib/rubocop/cop/rails/delegate.rb +2 -2
  39. data/lib/rubocop/cop/style/access_modifier_indentation.rb +2 -2
  40. data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
  41. data/lib/rubocop/cop/style/align_hash.rb +16 -12
  42. data/lib/rubocop/cop/style/align_parameters.rb +1 -1
  43. data/lib/rubocop/cop/style/and_or.rb +14 -6
  44. data/lib/rubocop/cop/style/array_join.rb +1 -1
  45. data/lib/rubocop/cop/style/block_comments.rb +16 -8
  46. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -30
  47. data/lib/rubocop/cop/style/case_indentation.rb +20 -12
  48. data/lib/rubocop/cop/style/collection_methods.rb +4 -4
  49. data/lib/rubocop/cop/style/colon_method_call.rb +9 -0
  50. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  51. data/lib/rubocop/cop/style/comment_indentation.rb +22 -22
  52. data/lib/rubocop/cop/style/def_with_parentheses.rb +2 -2
  53. data/lib/rubocop/cop/style/deprecated_hash_methods.rb +1 -1
  54. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  55. data/lib/rubocop/cop/style/else_alignment.rb +93 -0
  56. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
  57. data/lib/rubocop/cop/style/empty_lines.rb +1 -1
  58. data/lib/rubocop/cop/style/empty_lines_around_class_body.rb +34 -0
  59. data/lib/rubocop/cop/style/empty_lines_around_method_body.rb +37 -0
  60. data/lib/rubocop/cop/style/empty_lines_around_module_body.rb +30 -0
  61. data/lib/rubocop/cop/style/encoding.rb +1 -1
  62. data/lib/rubocop/cop/style/format_string.rb +4 -4
  63. data/lib/rubocop/cop/style/indent_array.rb +2 -2
  64. data/lib/rubocop/cop/style/indent_hash.rb +17 -12
  65. data/lib/rubocop/cop/style/indentation_width.rb +27 -19
  66. data/lib/rubocop/cop/style/method_call_parentheses.rb +3 -3
  67. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  68. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -11
  69. data/lib/rubocop/cop/style/method_name.rb +1 -1
  70. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +174 -0
  71. data/lib/rubocop/cop/style/non_nil_check.rb +12 -15
  72. data/lib/rubocop/cop/style/not.rb +1 -1
  73. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +12 -17
  74. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  75. data/lib/rubocop/cop/style/predicate_name.rb +2 -2
  76. data/lib/rubocop/cop/style/redundant_begin.rb +2 -2
  77. data/lib/rubocop/cop/style/redundant_return.rb +3 -3
  78. data/lib/rubocop/cop/style/redundant_self.rb +3 -3
  79. data/lib/rubocop/cop/style/regexp_literal.rb +17 -13
  80. data/lib/rubocop/cop/style/rescue_modifier.rb +2 -2
  81. data/lib/rubocop/cop/style/single_line_methods.rb +7 -4
  82. data/lib/rubocop/cop/style/space_after_method_name.rb +2 -2
  83. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +17 -11
  84. data/lib/rubocop/cop/style/space_before_block_braces.rb +1 -1
  85. data/lib/rubocop/cop/style/space_inside_block_braces.rb +17 -14
  86. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +10 -6
  87. data/lib/rubocop/cop/style/string_literals.rb +13 -16
  88. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +41 -0
  89. data/lib/rubocop/cop/style/trailing_comma.rb +1 -3
  90. data/lib/rubocop/cop/style/trivial_accessors.rb +3 -3
  91. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
  92. data/lib/rubocop/cop/style/unneeded_percent_q.rb +2 -2
  93. data/lib/rubocop/cop/style/word_array.rb +23 -19
  94. data/lib/rubocop/cop/team.rb +13 -26
  95. data/lib/rubocop/cop/util.rb +5 -0
  96. data/lib/rubocop/cop/variable_force/locatable.rb +7 -13
  97. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  98. data/lib/rubocop/formatter/formatter_set.rb +9 -1
  99. data/lib/rubocop/formatter/html_formatter.rb +83 -55
  100. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -2
  101. data/lib/rubocop/formatter/text_util.rb +25 -0
  102. data/lib/rubocop/options.rb +14 -7
  103. data/lib/rubocop/path_util.rb +11 -7
  104. data/lib/rubocop/runner.rb +7 -2
  105. data/lib/rubocop/version.rb +1 -1
  106. data/relnotes/v0.27.0.md +77 -0
  107. data/rubocop.gemspec +1 -1
  108. data/spec/fixtures/html_formatter/expected.html +495 -0
  109. data/spec/fixtures/html_formatter/project/app/controllers/application_controller.rb +5 -0
  110. data/spec/fixtures/html_formatter/project/app/controllers/books_controller.rb +74 -0
  111. data/spec/fixtures/html_formatter/project/app/models/book.rb +5 -0
  112. data/spec/rubocop/cli_spec.rb +56 -13
  113. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +1 -1
  114. data/spec/rubocop/cop/metrics/abc_size_spec.rb +99 -0
  115. data/spec/rubocop/cop/rails/action_filter_spec.rb +1 -0
  116. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +23 -1
  117. data/spec/rubocop/cop/style/align_hash_spec.rb +13 -0
  118. data/spec/rubocop/cop/style/align_parameters_spec.rb +44 -33
  119. data/spec/rubocop/cop/style/blocks_spec.rb +8 -0
  120. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +9 -9
  121. data/spec/rubocop/cop/style/case_indentation_spec.rb +3 -2
  122. data/spec/rubocop/cop/style/colon_method_call_spec.rb +5 -0
  123. data/spec/rubocop/cop/style/comment_indentation_spec.rb +6 -1
  124. data/spec/rubocop/cop/style/else_alignment_spec.rb +437 -0
  125. data/spec/rubocop/cop/style/empty_lines_around_class_body_spec.rb +75 -0
  126. data/spec/rubocop/cop/style/{empty_lines_around_body_spec.rb → empty_lines_around_method_body_spec.rb} +9 -50
  127. data/spec/rubocop/cop/style/empty_lines_around_module_body_spec.rb +79 -0
  128. data/spec/rubocop/cop/style/file_name_spec.rb +1 -1
  129. data/spec/rubocop/cop/style/format_string_spec.rb +12 -0
  130. data/spec/rubocop/cop/style/indent_array_spec.rb +6 -1
  131. data/spec/rubocop/cop/style/indent_hash_spec.rb +2 -1
  132. data/spec/rubocop/cop/style/indentation_width_spec.rb +765 -722
  133. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +414 -0
  134. data/spec/rubocop/cop/style/non_nil_check_spec.rb +86 -55
  135. data/spec/rubocop/cop/style/single_line_methods_spec.rb +5 -1
  136. data/spec/rubocop/cop/style/space_before_block_braces_spec.rb +2 -1
  137. data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +2 -1
  138. data/spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb +63 -0
  139. data/spec/rubocop/cop/style/string_literals_spec.rb +2 -2
  140. data/spec/rubocop/cop/style/word_array_spec.rb +15 -1
  141. data/spec/rubocop/formatter/base_formatter_spec.rb +1 -1
  142. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +0 -1
  143. data/spec/rubocop/formatter/formatter_set_spec.rb +9 -0
  144. data/spec/rubocop/formatter/html_formatter_spec.rb +25 -122
  145. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -1
  146. data/spec/rubocop/runner_spec.rb +1 -1
  147. data/spec/spec_helper.rb +12 -130
  148. data/spec/support/cop_helper.rb +72 -0
  149. data/spec/support/coverage.rb +15 -0
  150. data/spec/support/{offenses_matcher.rb → custom_matchers.rb} +28 -0
  151. data/spec/support/jruby_workaround.rb +15 -0
  152. data/spec/support/{isolated_environment.rb → shared_contexts.rb} +19 -0
  153. metadata +49 -14
  154. data/lib/rubocop/cop/style/empty_lines_around_body.rb +0 -75
  155. data/spec/support/shared_context.rb +0 -20
@@ -1,5 +1,10 @@
1
1
  # These are all the cops that are disabled in the default configuration.
2
2
 
3
+ Style/CollectionMethods:
4
+ Description: 'Preferred collection methods.'
5
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#map-fine-select-reduce-size'
6
+ Enabled: false
7
+
3
8
  Style/Encoding:
4
9
  Description: 'Use UTF-8 as the source file encoding.'
5
10
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#utf-8'
@@ -128,11 +128,6 @@ Style/ClassVars:
128
128
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-class-vars'
129
129
  Enabled: true
130
130
 
131
- Style/CollectionMethods:
132
- Description: 'Preferred collection methods.'
133
- StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#map-fine-select-reduce-size'
134
- Enabled: true
135
-
136
131
  Style/ColonMethodCall:
137
132
  Description: 'Do not use :: for method call.'
138
133
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#double-colons'
@@ -182,6 +177,10 @@ Style/EachWithObject:
182
177
  Description: 'Prefer `each_with_object` over `inject` or `reduce`.'
183
178
  Enabled: true
184
179
 
180
+ Style/ElseAlignment:
181
+ Description: 'Align elses and elsifs correctly.'
182
+ Enabled: true
183
+
185
184
  Style/EmptyLineBetweenDefs:
186
185
  Description: 'Use empty lines between defs.'
187
186
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#empty-lines-between-methods'
@@ -195,8 +194,16 @@ Style/EmptyLinesAroundAccessModifier:
195
194
  Description: "Keep blank lines around access modifiers."
196
195
  Enabled: true
197
196
 
198
- Style/EmptyLinesAroundBody:
199
- Description: "Keeps track of empty lines around expression bodies."
197
+ Style/EmptyLinesAroundClassBody:
198
+ Description: "Keeps track of empty lines around class bodies."
199
+ Enabled: true
200
+
201
+ Style/EmptyLinesAroundModuleBody:
202
+ Description: "Keeps track of empty lines around module bodies."
203
+ Enabled: true
204
+
205
+ Style/EmptyLinesAroundMethodBody:
206
+ Description: "Keeps track of empty lines around method bodies."
200
207
  Enabled: true
201
208
 
202
209
  Style/EmptyLiteral:
@@ -349,6 +356,12 @@ Style/MultilineIfThen:
349
356
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-then'
350
357
  Enabled: true
351
358
 
359
+ Style/MultilineOperationIndentation:
360
+ Description: >-
361
+ Checks indentation of binary operations that span more than
362
+ one line.
363
+ Enabled: true
364
+
352
365
  Style/MultilineTernaryOperator:
353
366
  Description: >-
354
367
  Avoid multi-line ?: (the ternary operator);
@@ -620,6 +633,12 @@ Style/StringLiterals:
620
633
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#consistent-string-literals'
621
634
  Enabled: true
622
635
 
636
+ Style/StringLiteralsInInterpolation:
637
+ Description: >-
638
+ Checks if uses of quotes inside expressions in interpolated
639
+ strings match the configured preference.
640
+ Enabled: true
641
+
623
642
  Style/SymbolProc:
624
643
  Description: 'Use symbols as procs instead of blocks when possible.'
625
644
  Enabled: true
@@ -706,6 +725,12 @@ Style/WordArray:
706
725
 
707
726
  #################### Metrics ################################
708
727
 
728
+ Metrics/AbcSize:
729
+ Description: >-
730
+ A calculated magnitude based on number of assignments,
731
+ branches, and conditions.
732
+ Enabled: true
733
+
709
734
  Metrics/BlockNesting:
710
735
  Description: 'Avoid excessive block nesting'
711
736
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#three-is-the-number-thou-shalt-count'
@@ -46,10 +46,11 @@ require 'rubocop/cop/mixin/configurable_max'
46
46
  require 'rubocop/cop/mixin/code_length'
47
47
  require 'rubocop/cop/mixin/configurable_enforced_style'
48
48
  require 'rubocop/cop/mixin/configurable_naming'
49
+ require 'rubocop/cop/mixin/empty_lines_around_body'
49
50
  require 'rubocop/cop/mixin/end_keyword_alignment'
50
51
  require 'rubocop/cop/mixin/if_node'
51
52
  require 'rubocop/cop/mixin/negative_conditional'
52
- require 'rubocop/cop/mixin/on_method'
53
+ require 'rubocop/cop/mixin/on_method_def'
53
54
  require 'rubocop/cop/mixin/method_complexity'
54
55
  require 'rubocop/cop/mixin/on_normal_if_unless'
55
56
  require 'rubocop/cop/mixin/parser_diagnostic'
@@ -61,6 +62,7 @@ require 'rubocop/cop/mixin/space_after_punctuation'
61
62
  require 'rubocop/cop/mixin/space_before_punctuation'
62
63
  require 'rubocop/cop/mixin/statement_modifier'
63
64
  require 'rubocop/cop/mixin/string_help'
65
+ require 'rubocop/cop/mixin/string_literals_help'
64
66
  require 'rubocop/cop/mixin/unused_argument'
65
67
 
66
68
  require 'rubocop/cop/lint/ambiguous_operator'
@@ -104,6 +106,7 @@ require 'rubocop/cop/lint/void'
104
106
  require 'rubocop/cop/metrics/block_nesting'
105
107
  require 'rubocop/cop/metrics/class_length'
106
108
  require 'rubocop/cop/metrics/cyclomatic_complexity'
109
+ require 'rubocop/cop/metrics/abc_size'
107
110
  require 'rubocop/cop/metrics/line_length'
108
111
  require 'rubocop/cop/metrics/method_length'
109
112
  require 'rubocop/cop/metrics/parameter_lists'
@@ -145,10 +148,13 @@ require 'rubocop/cop/style/documentation'
145
148
  require 'rubocop/cop/style/dot_position'
146
149
  require 'rubocop/cop/style/double_negation'
147
150
  require 'rubocop/cop/style/each_with_object'
151
+ require 'rubocop/cop/style/else_alignment'
148
152
  require 'rubocop/cop/style/empty_line_between_defs'
149
153
  require 'rubocop/cop/style/empty_lines'
150
154
  require 'rubocop/cop/style/empty_lines_around_access_modifier'
151
- require 'rubocop/cop/style/empty_lines_around_body'
155
+ require 'rubocop/cop/style/empty_lines_around_class_body'
156
+ require 'rubocop/cop/style/empty_lines_around_method_body'
157
+ require 'rubocop/cop/style/empty_lines_around_module_body'
152
158
  require 'rubocop/cop/style/empty_literal'
153
159
  require 'rubocop/cop/style/encoding'
154
160
  require 'rubocop/cop/style/end_block'
@@ -181,6 +187,7 @@ require 'rubocop/cop/style/module_function'
181
187
  require 'rubocop/cop/style/multiline_block_chain'
182
188
  require 'rubocop/cop/style/multiline_block_layout'
183
189
  require 'rubocop/cop/style/multiline_if_then'
190
+ require 'rubocop/cop/style/multiline_operation_indentation'
184
191
  require 'rubocop/cop/style/multiline_ternary_operator'
185
192
  require 'rubocop/cop/style/negated_if'
186
193
  require 'rubocop/cop/style/negated_while'
@@ -231,6 +238,7 @@ require 'rubocop/cop/style/space_inside_parens'
231
238
  require 'rubocop/cop/style/space_inside_range_literal'
232
239
  require 'rubocop/cop/style/special_global_vars'
233
240
  require 'rubocop/cop/style/string_literals'
241
+ require 'rubocop/cop/style/string_literals_in_interpolation'
234
242
  require 'rubocop/cop/style/symbol_array'
235
243
  require 'rubocop/cop/style/symbol_proc'
236
244
  require 'rubocop/cop/style/tab'
@@ -32,16 +32,12 @@ module RuboCop
32
32
 
33
33
  each_mentioned_cop do |cop_name, disabled, line, single_line|
34
34
  if single_line
35
- next unless disabled
36
- disabled_line_ranges[cop_name] << (line..line)
35
+ disabled_line_ranges[cop_name] << (line..line) if disabled
36
+ elsif disabled
37
+ disablement_start_line_numbers[cop_name] = line
37
38
  else
38
- if disabled
39
- disablement_start_line_numbers[cop_name] = line
40
- else
41
- start_line = disablement_start_line_numbers.delete(cop_name)
42
- next unless start_line
43
- disabled_line_ranges[cop_name] << (start_line..line)
44
- end
39
+ start_line = disablement_start_line_numbers.delete(cop_name)
40
+ disabled_line_ranges[cop_name] << (start_line..line) if start_line
45
41
  end
46
42
  end
47
43
 
@@ -53,8 +49,6 @@ module RuboCop
53
49
  end
54
50
 
55
51
  def each_mentioned_cop
56
- all_cop_names = nil # For performance improvement
57
-
58
52
  return if processed_source.comments.nil?
59
53
 
60
54
  processed_source.comments.each do |comment|
@@ -63,12 +57,8 @@ module RuboCop
63
57
 
64
58
  switch, cops_string = match.captures
65
59
 
66
- if cops_string == 'all'
67
- all_cop_names ||= Cop::Cop.all.map(&:cop_name)
68
- cop_names = all_cop_names
69
- else
70
- cop_names = cops_string.split(/,\s*/)
71
- end
60
+ cop_names =
61
+ cops_string == 'all' ? all_cop_names : cops_string.split(/,\s*/)
72
62
 
73
63
  disabled = (switch == 'disable')
74
64
  comment_line_number = comment.loc.expression.line
@@ -82,6 +72,10 @@ module RuboCop
82
72
  end
83
73
  end
84
74
 
75
+ def all_cop_names
76
+ @all_cop_names ||= Cop::Cop.all.map(&:cop_name)
77
+ end
78
+
85
79
  def comment_only_line?(line_number)
86
80
  non_comment_token_line_numbers.none? do |non_comment_line_number|
87
81
  non_comment_line_number == line_number
@@ -38,7 +38,7 @@ module RuboCop
38
38
 
39
39
  def add_excludes_from_higher_level(highest_config)
40
40
  return unless highest_config['AllCops'] &&
41
- highest_config['AllCops']['Exclude']
41
+ highest_config['AllCops']['Exclude']
42
42
 
43
43
  self['AllCops'] ||= {}
44
44
  excludes = self['AllCops']['Exclude'] ||= []
@@ -93,13 +93,11 @@ module RuboCop
93
93
  # TODO: This should be a private method
94
94
  def validate
95
95
  # Don't validate RuboCop's own files. Avoids inifinite recursion.
96
- return if @loaded_path.start_with?(File.join(ConfigLoader::RUBOCOP_HOME,
97
- 'config'))
98
-
99
- default_config = ConfigLoader.default_configuration
96
+ return if loaded_path.start_with?(File.join(ConfigLoader::RUBOCOP_HOME,
97
+ 'config'))
100
98
 
101
99
  valid_cop_names, invalid_cop_names = @hash.keys.partition do |key|
102
- default_config.key?(key)
100
+ ConfigLoader.default_configuration.key?(key)
103
101
  end
104
102
 
105
103
  invalid_cop_names.each do |name|
@@ -107,16 +105,7 @@ module RuboCop
107
105
  "unrecognized cop #{name} found in #{loaded_path || self}"
108
106
  end
109
107
 
110
- valid_cop_names.each do |name|
111
- @hash[name].each_key do |param|
112
- next if COMMON_PARAMS.include?(param) ||
113
- default_config[name].key?(param)
114
-
115
- fail ValidationError,
116
- "unrecognized parameter #{name}:#{param} found " \
117
- "in #{loaded_path || self}"
118
- end
119
- end
108
+ validate_parameter_names(valid_cop_names)
120
109
  end
121
110
 
122
111
  def file_to_include?(file)
@@ -156,5 +145,20 @@ module RuboCop
156
145
  Dir.pwd
157
146
  end
158
147
  end
148
+
149
+ private
150
+
151
+ def validate_parameter_names(valid_cop_names)
152
+ valid_cop_names.each do |name|
153
+ @hash[name].each_key do |param|
154
+ next if COMMON_PARAMS.include?(param) ||
155
+ ConfigLoader.default_configuration[name].key?(param)
156
+
157
+ fail ValidationError,
158
+ "unrecognized parameter #{name}:#{param} found " \
159
+ "in #{loaded_path || self}"
160
+ end
161
+ end
162
+ end
159
163
  end
160
164
  end
@@ -24,18 +24,14 @@ module RuboCop
24
24
 
25
25
  def load_file(path)
26
26
  path = File.absolute_path(path)
27
-
28
- # Psych can give an error when reading an empty file, so we use syck in
29
- # Ruby versions where it's available. Also, the problem with empty
30
- # files does not appear in Ruby 2 or in JRuby 1.9 mode.
31
- if RUBY_VERSION < '2.0.0' && RUBY_PLATFORM != 'java'
32
- original_yamler = YAML::ENGINE.yamler
33
- YAML::ENGINE.yamler = 'syck'
34
- end
35
- hash = YAML.load_file(path) || {}
36
- # Restore yamler for applications using RuboCop as a library.
37
- YAML::ENGINE.yamler = original_yamler if original_yamler
38
-
27
+ yaml_code = IO.read(path)
28
+ # At one time, there was a problem with the psych YAML engine under
29
+ # Ruby 1.9.3. YAML.load_file would crash when reading empty .yml files
30
+ # or files that only contained comments and blank lines. This problem
31
+ # is not possible to reproduce now, but we want to avoid it in case
32
+ # it's still there. So we only load the YAML code if we find some real
33
+ # code in there.
34
+ hash = yaml_code =~ /^[A-Z]/i ? YAML.load(yaml_code) : {}
39
35
  puts "configuration from #{path}" if debug?
40
36
 
41
37
  resolve_inheritance(path, hash)
@@ -65,17 +65,18 @@ module RuboCop
65
65
  end
66
66
 
67
67
  case found_ns.size
68
- when 0
69
- name # No namespace found. Deal with it later in caller.
70
- when 1
71
- if name != basename && found_ns.first != File.dirname(name).to_sym
72
- warn "#{origin}: #{name} has the wrong namespace - " \
73
- "should be #{found_ns.first}"
74
- end
75
- "#{found_ns.first}/#{basename}"
76
- else
77
- fail AmbiguousCopName, "`#{basename}` used in #{origin}"
68
+ when 0 then name # No namespace found. Deal with it later in caller.
69
+ when 1 then cop_name_with_namespace(name, origin, basename, found_ns[0])
70
+ else fail AmbiguousCopName, "`#{basename}` used in #{origin}"
71
+ end
72
+ end
73
+
74
+ def self.cop_name_with_namespace(name, origin, basename, found_ns)
75
+ if name != basename && found_ns != File.dirname(name).to_sym
76
+ warn "#{origin}: #{name} has the wrong namespace - should be " \
77
+ "#{found_ns}"
78
78
  end
79
+ "#{found_ns}/#{basename}"
79
80
  end
80
81
 
81
82
  def self.non_rails
@@ -208,9 +209,9 @@ module RuboCop
208
209
  if Severity::NAMES.include?(severity.to_sym)
209
210
  severity.to_sym
210
211
  else
211
- warn "Warning: Invalid severity '#{severity}'. " +
212
+ warn("Warning: Invalid severity '#{severity}'. " +
212
213
  "Valid severities are #{Severity::NAMES.join(', ')}."
213
- .color(:red)
214
+ .color(:red))
214
215
  end
215
216
  end
216
217
  end
@@ -130,7 +130,7 @@ module RuboCop
130
130
 
131
131
  def alt_start_msg(match, start_loc, do_loc, indentation_of_do_line)
132
132
  if start_loc.line == do_loc.line &&
133
- start_loc.column == indentation_of_do_line
133
+ start_loc.column == indentation_of_do_line
134
134
  ''
135
135
  else
136
136
  " or `#{match[0]}` at #{do_loc.line}, #{indentation_of_do_line}"
@@ -161,11 +161,9 @@ module RuboCop
161
161
  delta = start_col - end_col
162
162
  corrector.insert_before(node.loc.end, ' ' * delta)
163
163
  elsif end_col > start_col
164
- delta = start_col - end_col
165
- range_start = starting_position_of_block_end + delta
166
- range_end = range_start - delta
167
-
168
- range = Parser::Source::Range.new(source, range_start, range_end)
164
+ range_start = starting_position_of_block_end + start_col - end_col
165
+ range = Parser::Source::Range.new(source, range_start,
166
+ starting_position_of_block_end)
169
167
  corrector.remove(range)
170
168
  end
171
169
  end
@@ -17,12 +17,12 @@ module RuboCop
17
17
  # private def foo
18
18
  # end
19
19
  class DefEndAlignment < Cop
20
- include OnMethod
20
+ include OnMethodDef
21
21
  include EndKeywordAlignment
22
22
 
23
23
  MSG = '`end` at %d, %d is not aligned with `%s` at %d, %d'
24
24
 
25
- def on_method(node, _method_name, _args, _body)
25
+ def on_method_def(node, _method_name, _args, _body)
26
26
  check_offset_of_node(node)
27
27
  end
28
28
 
@@ -56,7 +56,7 @@ module RuboCop
56
56
  end
57
57
 
58
58
  def predicate?(method_name)
59
- method_name =~ /\w\?$/
59
+ method_name.to_s.end_with?('?')
60
60
  end
61
61
 
62
62
  def offense?(node)
@@ -24,7 +24,7 @@ module RuboCop
24
24
 
25
25
  body_nodes.each do |child_node|
26
26
  check_for_access_modifier(child_node) ||
27
- check_for_instance_method(child_node)
27
+ check_for_instance_method(child_node)
28
28
  end
29
29
 
30
30
  add_offense_for_access_modifier
@@ -41,8 +41,7 @@ module RuboCop
41
41
  end
42
42
 
43
43
  def check_for_instance_method(node)
44
- return unless node.type == :def ||
45
- node.type == :send
44
+ return unless node.type == :def || node.type == :send
46
45
 
47
46
  @access_modifier_node = nil
48
47
  end
@@ -13,7 +13,7 @@ module RuboCop
13
13
  # x.attr = 5
14
14
  # end
15
15
  class UselessSetterCall < Cop
16
- include OnMethod
16
+ include OnMethodDef
17
17
 
18
18
  MSG = 'Useless setter call to local variable `%s`.'
19
19
  ASSIGNMENT_TYPES = [:lvasgn, :ivasgn, :cvasgn, :gvasgn].freeze
@@ -27,7 +27,7 @@ module RuboCop
27
27
 
28
28
  private
29
29
 
30
- def on_method(_node, _method_name, _args, body)
30
+ def on_method_def(_node, _method_name, _args, body)
31
31
  return unless body
32
32
 
33
33
  if body.type == :begin
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Metrics
6
+ # This cop checks that the ABC size of methods is not higher than the
7
+ # configured maximum. The ABC size is based on assignments, branches
8
+ # (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
9
+ class AbcSize < Cop
10
+ include MethodComplexity
11
+
12
+ MSG = 'Assignment Branch Condition size for %s is too high. [%.4g/%.4g]'
13
+ BRANCH_NODES = [:send]
14
+ CONDITION_NODES = CyclomaticComplexity::COUNTED_NODES
15
+
16
+ private
17
+
18
+ def complexity(node)
19
+ a = node.each_node(ASGN_NODES).count
20
+ b = node.each_node(BRANCH_NODES).count
21
+ c = node.each_node(CONDITION_NODES).count
22
+ Math.sqrt(a**2 + b**2 + c**2).round(2)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end