rubocop 0.42.0 → 0.43.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/README.md +1 -1
  3. data/assets/output.html.erb +21 -10
  4. data/config/default.yml +32 -2
  5. data/config/disabled.yml +8 -1
  6. data/config/enabled.yml +40 -12
  7. data/lib/rubocop.rb +14 -2
  8. data/lib/rubocop/ast_node.rb +2 -0
  9. data/lib/rubocop/cached_data.rb +13 -11
  10. data/lib/rubocop/cli.rb +5 -5
  11. data/lib/rubocop/config.rb +68 -24
  12. data/lib/rubocop/config_loader.rb +13 -11
  13. data/lib/rubocop/config_loader_resolver.rb +4 -2
  14. data/lib/rubocop/cop/cop.rb +16 -5
  15. data/lib/rubocop/cop/lint/assignment_in_condition.rb +21 -20
  16. data/lib/rubocop/cop/lint/block_alignment.rb +3 -4
  17. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -3
  18. data/lib/rubocop/cop/lint/duplicate_methods.rb +16 -6
  19. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  20. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  21. data/lib/rubocop/cop/lint/end_alignment.rb +4 -6
  22. data/lib/rubocop/cop/lint/eval.rb +1 -1
  23. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  24. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +8 -8
  25. data/lib/rubocop/cop/lint/inherit_exception.rb +22 -7
  26. data/lib/rubocop/cop/lint/literal_in_condition.rb +5 -5
  27. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +3 -5
  28. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  29. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +9 -8
  30. data/lib/rubocop/cop/lint/percent_string_array.rb +17 -6
  31. data/lib/rubocop/cop/lint/percent_symbol_array.rb +4 -4
  32. data/lib/rubocop/cop/lint/rand_one.rb +3 -3
  33. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -3
  34. data/lib/rubocop/cop/lint/shadowed_exception.rb +39 -44
  35. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +2 -2
  36. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -2
  37. data/lib/rubocop/cop/lint/unified_integer.rb +38 -0
  38. data/lib/rubocop/cop/lint/unneeded_disable.rb +51 -38
  39. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +114 -0
  40. data/lib/rubocop/cop/lint/useless_assignment.rb +25 -12
  41. data/lib/rubocop/cop/lint/useless_setter_call.rb +27 -28
  42. data/lib/rubocop/cop/lint/void.rb +2 -4
  43. data/lib/rubocop/cop/mixin/access_modifier_node.rb +5 -5
  44. data/lib/rubocop/cop/mixin/array_hash_indentation.rb +19 -17
  45. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +3 -5
  46. data/lib/rubocop/cop/mixin/configurable_naming.rb +4 -5
  47. data/lib/rubocop/cop/mixin/configurable_numbering.rb +52 -0
  48. data/lib/rubocop/cop/mixin/def_node.rb +28 -0
  49. data/lib/rubocop/cop/mixin/documentation_comment.rb +41 -0
  50. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +18 -13
  51. data/lib/rubocop/cop/mixin/if_node.rb +6 -0
  52. data/lib/rubocop/cop/mixin/match_range.rb +2 -5
  53. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
  54. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +40 -28
  55. data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
  56. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -5
  57. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +14 -4
  58. data/lib/rubocop/cop/mixin/safe_mode.rb +23 -0
  59. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -4
  60. data/lib/rubocop/cop/mixin/space_inside.rb +1 -3
  61. data/lib/rubocop/cop/mixin/statement_modifier.rb +30 -20
  62. data/lib/rubocop/cop/mixin/trailing_comma.rb +19 -17
  63. data/lib/rubocop/cop/performance/case_when_splat.rb +16 -41
  64. data/lib/rubocop/cop/performance/casecmp.rb +28 -16
  65. data/lib/rubocop/cop/performance/count.rb +58 -34
  66. data/lib/rubocop/cop/performance/detect.rb +3 -7
  67. data/lib/rubocop/cop/performance/double_start_end_with.rb +17 -13
  68. data/lib/rubocop/cop/performance/fixed_size.rb +19 -14
  69. data/lib/rubocop/cop/performance/flat_map.rb +16 -9
  70. data/lib/rubocop/cop/performance/hash_each.rb +2 -3
  71. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +4 -6
  72. data/lib/rubocop/cop/performance/redundant_match.rb +4 -1
  73. data/lib/rubocop/cop/performance/redundant_merge.rb +63 -32
  74. data/lib/rubocop/cop/performance/redundant_sort_by.rb +8 -7
  75. data/lib/rubocop/cop/performance/reverse_each.rb +1 -4
  76. data/lib/rubocop/cop/performance/size.rb +21 -8
  77. data/lib/rubocop/cop/performance/sort_with_block.rb +54 -0
  78. data/lib/rubocop/cop/performance/string_replacement.rb +3 -7
  79. data/lib/rubocop/cop/rails/delegate.rb +2 -3
  80. data/lib/rubocop/cop/rails/find_by.rb +4 -8
  81. data/lib/rubocop/cop/rails/not_null_column.rb +45 -0
  82. data/lib/rubocop/cop/rails/request_referer.rb +3 -3
  83. data/lib/rubocop/cop/rails/safe_navigation.rb +89 -0
  84. data/lib/rubocop/cop/rails/save_bang.rb +78 -9
  85. data/lib/rubocop/cop/rails/scope_args.rb +3 -1
  86. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +2 -3
  87. data/lib/rubocop/cop/rails/validation.rb +1 -1
  88. data/lib/rubocop/cop/security/json_load.rb +36 -0
  89. data/lib/rubocop/cop/style/alias.rb +1 -1
  90. data/lib/rubocop/cop/style/align_hash.rb +25 -14
  91. data/lib/rubocop/cop/style/and_or.rb +13 -3
  92. data/lib/rubocop/cop/style/array_join.rb +3 -3
  93. data/lib/rubocop/cop/style/ascii_comments.rb +1 -2
  94. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -2
  95. data/lib/rubocop/cop/style/attr.rb +1 -3
  96. data/lib/rubocop/cop/style/block_comments.rb +2 -6
  97. data/lib/rubocop/cop/style/block_delimiters.rb +35 -21
  98. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +4 -4
  99. data/lib/rubocop/cop/style/case_indentation.rb +1 -3
  100. data/lib/rubocop/cop/style/class_methods.rb +3 -4
  101. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  102. data/lib/rubocop/cop/style/command_literal.rb +15 -8
  103. data/lib/rubocop/cop/style/comment_annotation.rb +1 -2
  104. data/lib/rubocop/cop/style/conditional_assignment.rb +68 -36
  105. data/lib/rubocop/cop/style/copyright.rb +1 -5
  106. data/lib/rubocop/cop/style/def_with_parentheses.rb +3 -5
  107. data/lib/rubocop/cop/style/documentation.rb +28 -56
  108. data/lib/rubocop/cop/style/documentation_method.rb +80 -0
  109. data/lib/rubocop/cop/style/each_for_simple_loop.rb +6 -5
  110. data/lib/rubocop/cop/style/each_with_object.rb +2 -2
  111. data/lib/rubocop/cop/style/else_alignment.rb +10 -9
  112. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -4
  113. data/lib/rubocop/cop/style/empty_else.rb +1 -4
  114. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -3
  115. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -5
  116. data/lib/rubocop/cop/style/encoding.rb +28 -14
  117. data/lib/rubocop/cop/style/even_odd.rb +28 -17
  118. data/lib/rubocop/cop/style/extra_spacing.rb +36 -25
  119. data/lib/rubocop/cop/style/file_name.rb +19 -10
  120. data/lib/rubocop/cop/style/first_parameter_indentation.rb +2 -3
  121. data/lib/rubocop/cop/style/for.rb +12 -8
  122. data/lib/rubocop/cop/style/format_string.rb +1 -1
  123. data/lib/rubocop/cop/style/guard_clause.rb +22 -56
  124. data/lib/rubocop/cop/style/hash_syntax.rb +72 -7
  125. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -19
  126. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -3
  127. data/lib/rubocop/cop/style/indentation_width.rb +30 -16
  128. data/lib/rubocop/cop/style/infinite_loop.rb +16 -13
  129. data/lib/rubocop/cop/style/initial_indentation.rb +23 -18
  130. data/lib/rubocop/cop/style/inline_comment.rb +16 -3
  131. data/lib/rubocop/cop/style/lambda.rb +22 -10
  132. data/lib/rubocop/cop/style/leading_comment_space.rb +12 -1
  133. data/lib/rubocop/cop/style/line_end_concatenation.rb +24 -6
  134. data/lib/rubocop/cop/style/method_call_parentheses.rb +18 -9
  135. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -4
  136. data/lib/rubocop/cop/style/method_def_parentheses.rb +3 -4
  137. data/lib/rubocop/cop/style/method_missing.rb +10 -2
  138. data/lib/rubocop/cop/style/module_function.rb +14 -6
  139. data/lib/rubocop/cop/style/multiline_assignment_layout.rb +2 -5
  140. data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -5
  141. data/lib/rubocop/cop/style/multiline_block_layout.rb +22 -15
  142. data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +9 -0
  143. data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +41 -20
  144. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +6 -6
  145. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +3 -5
  146. data/lib/rubocop/cop/style/mutable_constant.rb +21 -13
  147. data/lib/rubocop/cop/style/negated_if.rb +1 -1
  148. data/lib/rubocop/cop/style/negated_while.rb +3 -3
  149. data/lib/rubocop/cop/style/nested_modifier.rb +2 -4
  150. data/lib/rubocop/cop/style/next.rb +4 -4
  151. data/lib/rubocop/cop/style/non_nil_check.rb +18 -10
  152. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +8 -0
  153. data/lib/rubocop/cop/style/numeric_predicate.rb +9 -9
  154. data/lib/rubocop/cop/style/one_line_conditional.rb +11 -1
  155. data/lib/rubocop/cop/style/op_method.rb +1 -1
  156. data/lib/rubocop/cop/style/option_hash.rb +8 -8
  157. data/lib/rubocop/cop/style/optional_arguments.rb +21 -8
  158. data/lib/rubocop/cop/style/parallel_assignment.rb +51 -35
  159. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  160. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  161. data/lib/rubocop/cop/style/raise_args.rb +2 -2
  162. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  163. data/lib/rubocop/cop/style/redundant_parentheses.rb +26 -15
  164. data/lib/rubocop/cop/style/redundant_return.rb +5 -5
  165. data/lib/rubocop/cop/style/redundant_self.rb +20 -11
  166. data/lib/rubocop/cop/style/regexp_literal.rb +16 -10
  167. data/lib/rubocop/cop/style/rescue_ensure_alignment.rb +8 -6
  168. data/lib/rubocop/cop/style/safe_navigation.rb +125 -0
  169. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  170. data/lib/rubocop/cop/style/semicolon.rb +9 -10
  171. data/lib/rubocop/cop/style/signal_exception.rb +2 -4
  172. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  173. data/lib/rubocop/cop/style/single_line_methods.rb +18 -11
  174. data/lib/rubocop/cop/style/space_after_method_name.rb +2 -3
  175. data/lib/rubocop/cop/style/space_after_not.rb +4 -6
  176. data/lib/rubocop/cop/style/space_around_block_parameters.rb +1 -2
  177. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -3
  178. data/lib/rubocop/cop/style/space_around_operators.rb +21 -16
  179. data/lib/rubocop/cop/style/space_before_block_braces.rb +2 -12
  180. data/lib/rubocop/cop/style/space_before_first_arg.rb +1 -3
  181. data/lib/rubocop/cop/style/space_inside_array_percent_literal.rb +1 -1
  182. data/lib/rubocop/cop/style/space_inside_block_braces.rb +33 -40
  183. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +38 -23
  184. data/lib/rubocop/cop/style/space_inside_percent_literal_delimiters.rb +1 -1
  185. data/lib/rubocop/cop/style/space_inside_string_interpolation.rb +26 -12
  186. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -4
  187. data/lib/rubocop/cop/style/symbol_array.rb +10 -10
  188. data/lib/rubocop/cop/style/symbol_proc.rb +28 -13
  189. data/lib/rubocop/cop/style/ternary_parentheses.rb +35 -5
  190. data/lib/rubocop/cop/style/trailing_blank_lines.rb +2 -4
  191. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +29 -17
  192. data/lib/rubocop/cop/style/trivial_accessors.rb +6 -6
  193. data/lib/rubocop/cop/style/unless_else.rb +2 -6
  194. data/lib/rubocop/cop/style/unneeded_capital_w.rb +8 -4
  195. data/lib/rubocop/cop/style/unneeded_interpolation.rb +4 -5
  196. data/lib/rubocop/cop/style/unneeded_percent_q.rb +13 -7
  197. data/lib/rubocop/cop/style/variable_number.rb +79 -0
  198. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  199. data/lib/rubocop/cop/style/word_array.rb +25 -15
  200. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -0
  201. data/lib/rubocop/cop/util.rb +23 -4
  202. data/lib/rubocop/cop/variable_force.rb +59 -25
  203. data/lib/rubocop/cop/variable_force/locatable.rb +8 -6
  204. data/lib/rubocop/cop/variable_force/variable.rb +2 -2
  205. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
  206. data/lib/rubocop/formatter/disabled_config_formatter.rb +16 -11
  207. data/lib/rubocop/formatter/formatter_set.rb +12 -10
  208. data/lib/rubocop/formatter/worst_offenders_formatter.rb +4 -4
  209. data/lib/rubocop/node_pattern.rb +79 -35
  210. data/lib/rubocop/options.rb +4 -4
  211. data/lib/rubocop/processed_source.rb +9 -5
  212. data/lib/rubocop/remote_config.rb +14 -10
  213. data/lib/rubocop/result_cache.rb +14 -6
  214. data/lib/rubocop/runner.rb +55 -34
  215. data/lib/rubocop/string_util.rb +9 -5
  216. data/lib/rubocop/target_finder.rb +1 -1
  217. data/lib/rubocop/token.rb +1 -1
  218. data/lib/rubocop/version.rb +1 -1
  219. metadata +15 -4
  220. data/lib/rubocop/cop/lint/useless_array_splat.rb +0 -56
  221. data/lib/rubocop/cop/performance/push_splat.rb +0 -47
data/lib/rubocop/cli.rb CHANGED
@@ -19,7 +19,7 @@ module RuboCop
19
19
  # Entry point for the application logic. Here we
20
20
  # do the command line arguments processing and inspect
21
21
  # the target files
22
- # @return [Fixnum] UNIX exit code
22
+ # @return [Integer] UNIX exit code
23
23
  def run(args = ARGV)
24
24
  @options, paths = Options.new.parse(args)
25
25
  act_on_options
@@ -95,10 +95,10 @@ module RuboCop
95
95
  [[formatter, @options[:output_path]]]
96
96
  end
97
97
 
98
- if @options[:auto_gen_config]
99
- @options[:formatters] << [Formatter::DisabledConfigFormatter,
100
- ConfigLoader::AUTO_GENERATED_FILE]
101
- end
98
+ return unless @options[:auto_gen_config]
99
+
100
+ @options[:formatters] << [Formatter::DisabledConfigFormatter,
101
+ ConfigLoader::AUTO_GENERATED_FILE]
102
102
  end
103
103
 
104
104
  def print_available_cops
@@ -11,7 +11,7 @@ module RuboCop
11
11
  # file from which it was read. Several different Configs can be used
12
12
  # during a run of the rubocop program, if files in several
13
13
  # directories are inspected.
14
- class Config < Hash
14
+ class Config
15
15
  include PathUtil
16
16
 
17
17
  COMMON_PARAMS = %w(Exclude Include Severity
@@ -48,11 +48,55 @@ module RuboCop
48
48
  @for_cop = Hash.new do |h, cop|
49
49
  h[cop] = self[Cop::Cop.qualified_cop_name(cop, loaded_path)] || {}
50
50
  end
51
- replace(hash)
51
+ @hash = hash
52
+ end
53
+
54
+ def [](key)
55
+ @hash[key]
56
+ end
57
+
58
+ def []=(key, value)
59
+ @hash[key] = value
60
+ end
61
+
62
+ def delete(key)
63
+ @hash.delete(key)
64
+ end
65
+
66
+ def each(&block)
67
+ @hash.each(&block)
68
+ end
69
+
70
+ def key?(key)
71
+ @hash.key?(key)
72
+ end
73
+
74
+ def keys
75
+ @hash.keys
76
+ end
77
+
78
+ def map(&block)
79
+ @hash.map(&block)
80
+ end
81
+
82
+ def merge(other_hash)
83
+ @hash.merge(other_hash)
84
+ end
85
+
86
+ def to_h
87
+ @hash
88
+ end
89
+
90
+ def to_hash
91
+ @hash
92
+ end
93
+
94
+ def to_s
95
+ @to_s ||= @hash.to_s
52
96
  end
53
97
 
54
98
  def make_excludes_absolute
55
- each_key do |key|
99
+ each do |key, _|
56
100
  validate_section_presence(key)
57
101
  next unless self[key]['Exclude']
58
102
 
@@ -156,7 +200,9 @@ module RuboCop
156
200
  # Returns true if there's a chance that an Include pattern matches hidden
157
201
  # files, false if that's definitely not possible.
158
202
  def possibly_include_hidden?
159
- @possibly_include_hidden ||= patterns_to_include.any? do |s|
203
+ return @possibly_include_hidden if defined?(@possibly_include_hidden)
204
+
205
+ @possibly_include_hidden = patterns_to_include.any? do |s|
160
206
  s.is_a?(Regexp) || s.start_with?('.') || s.include?('/.')
161
207
  end
162
208
  end
@@ -169,11 +215,11 @@ module RuboCop
169
215
  end
170
216
 
171
217
  def patterns_to_include
172
- @patterns_to_include ||= for_all_cops['Include']
218
+ for_all_cops['Include']
173
219
  end
174
220
 
175
221
  def patterns_to_exclude
176
- @patterns_to_exclude ||= for_all_cops['Exclude']
222
+ for_all_cops['Exclude']
177
223
  end
178
224
 
179
225
  def path_relative_to_config(path)
@@ -274,11 +320,11 @@ module RuboCop
274
320
  end
275
321
 
276
322
  def check_obsolete_parameter(cop, parameter, alternative = nil)
277
- if self[cop] && self[cop].key?(parameter)
278
- raise ValidationError, "obsolete parameter #{parameter} (for #{cop}) " \
279
- "found in #{loaded_path}" \
280
- "#{"\n" if alternative}#{alternative}"
281
- end
323
+ return unless self[cop] && self[cop].key?(parameter)
324
+
325
+ raise ValidationError, "obsolete parameter #{parameter} (for #{cop}) " \
326
+ "found in #{loaded_path}" \
327
+ "#{"\n" if alternative}#{alternative}"
282
328
  end
283
329
 
284
330
  def reject_obsolete_cops
@@ -291,23 +337,21 @@ module RuboCop
291
337
  end
292
338
 
293
339
  def check_target_ruby
294
- return unless target_ruby_version
340
+ return if KNOWN_RUBIES.include?(target_ruby_version)
295
341
 
296
- unless KNOWN_RUBIES.include?(target_ruby_version)
297
- msg = "Unknown Ruby version #{target_ruby_version.inspect} found "
342
+ msg = "Unknown Ruby version #{target_ruby_version.inspect} found "
298
343
 
299
- msg +=
300
- case @target_ruby_version_source
301
- when :dot_ruby_version
302
- 'in `.ruby-version`.'
303
- when :rubocop_yml
304
- "in `TargetRubyVersion` parameter (in #{loaded_path})." \
305
- end
344
+ msg +=
345
+ case @target_ruby_version_source
346
+ when :dot_ruby_version
347
+ 'in `.ruby-version`.'
348
+ when :rubocop_yml
349
+ "in `TargetRubyVersion` parameter (in #{loaded_path})." \
350
+ end
306
351
 
307
- msg += "\nKnown versions: #{KNOWN_RUBIES.join(', ')}"
352
+ msg += "\nKnown versions: #{KNOWN_RUBIES.join(', ')}"
308
353
 
309
- raise ValidationError, msg
310
- end
354
+ raise ValidationError, msg
311
355
  end
312
356
  end
313
357
  end
@@ -33,12 +33,6 @@ module RuboCop
33
33
  def load_file(path)
34
34
  path = File.absolute_path(path)
35
35
  hash = load_yaml_configuration(path)
36
-
37
- resolve_inheritance_from_gems(hash, hash.delete('inherit_gem'))
38
- resolve_inheritance(path, hash)
39
- resolve_requires(path, hash)
40
-
41
- hash.delete('inherit_from')
42
36
  config = Config.new(hash, path)
43
37
 
44
38
  config.deprecation_check do |deprecation_message|
@@ -46,6 +40,13 @@ module RuboCop
46
40
  end
47
41
 
48
42
  config.add_missing_namespaces
43
+
44
+ resolve_inheritance_from_gems(config, config.delete('inherit_gem'))
45
+ resolve_inheritance(path, config)
46
+ resolve_requires(path, config)
47
+
48
+ config.delete('inherit_from')
49
+
49
50
  config.validate
50
51
  config.make_excludes_absolute
51
52
  config
@@ -140,7 +141,7 @@ module RuboCop
140
141
 
141
142
  def load_yaml_configuration(absolute_path)
142
143
  yaml_code = IO.read(absolute_path, encoding: 'UTF-8')
143
- hash = yaml_safe_load(yaml_code) || {}
144
+ hash = yaml_safe_load(yaml_code, absolute_path) || {}
144
145
 
145
146
  puts "configuration from #{absolute_path}" if debug?
146
147
 
@@ -151,15 +152,16 @@ module RuboCop
151
152
  hash
152
153
  end
153
154
 
154
- def yaml_safe_load(yaml_code)
155
+ def yaml_safe_load(yaml_code, filename)
155
156
  if YAML.respond_to?(:safe_load) # Ruby 2.1+
156
157
  if defined?(SafeYAML) && SafeYAML.respond_to?(:load)
157
- SafeYAML.load(yaml_code, nil, whitelisted_tags: %w(!ruby/regexp))
158
+ SafeYAML.load(yaml_code, filename,
159
+ whitelisted_tags: %w(!ruby/regexp))
158
160
  else
159
- YAML.safe_load(yaml_code, [Regexp])
161
+ YAML.safe_load(yaml_code, [Regexp], [], false, filename)
160
162
  end
161
163
  else
162
- YAML.load(yaml_code)
164
+ YAML.load(yaml_code, filename)
163
165
  end
164
166
  end
165
167
 
@@ -34,8 +34,10 @@ module RuboCop
34
34
  end
35
35
 
36
36
  hash['inherit_from'] = Array(hash['inherit_from'])
37
- # Put gem configuration first so local configuration overrides it.
38
- hash['inherit_from'].unshift gem_config_path(gem_name, config_path)
37
+ Array(config_path).reverse.each do |path|
38
+ # Put gem configuration first so local configuration overrides it.
39
+ hash['inherit_from'].unshift gem_config_path(gem_name, path)
40
+ end
39
41
  end
40
42
  end
41
43
  end
@@ -24,6 +24,8 @@ module RuboCop
24
24
 
25
25
  def qualified_cop_name(name, origin)
26
26
  @cop_names ||= Set.new(map(&:cop_name))
27
+ return name if @cop_names.include?(name)
28
+
27
29
  basename = File.basename(name)
28
30
  found_ns = types.map(&:capitalize).select do |ns|
29
31
  @cop_names.include?("#{ns}/#{basename}")
@@ -33,8 +35,9 @@ module RuboCop
33
35
  when 0 then name # No namespace found. Deal with it later in caller.
34
36
  when 1 then cop_name_with_namespace(name, origin, basename, found_ns[0])
35
37
  else raise AmbiguousCopName,
36
- "Ambiguous cop name `#{basename}` used in" \
37
- "#{origin} needs namespace qualifier."
38
+ "Ambiguous cop name `#{name}` used in #{origin} needs " \
39
+ 'namespace qualifier. Did you mean ' \
40
+ "#{found_ns.map { |ns| "#{ns}/#{basename}" }.join(' or ')}"
38
41
  end
39
42
  end
40
43
 
@@ -156,10 +159,9 @@ module RuboCop
156
159
  end
157
160
 
158
161
  def add_offense(node, loc, message = nil, severity = nil)
159
- location = loc.is_a?(Symbol) ? node.loc.public_send(loc) : loc
162
+ location = find_location(node, loc)
160
163
 
161
- # Don't include the same location twice for one cop.
162
- return if @offenses.any? { |o| o.location == location }
164
+ return if duplicate_location?(location)
163
165
 
164
166
  severity = custom_severity || severity || default_severity
165
167
 
@@ -172,6 +174,15 @@ module RuboCop
172
174
  yield if block_given? && status != :disabled
173
175
  end
174
176
 
177
+ def find_location(node, loc)
178
+ # Location can be provided as a symbol, e.g.: `:keyword`
179
+ loc.is_a?(Symbol) ? node.loc.public_send(loc) : loc
180
+ end
181
+
182
+ def duplicate_location?(location)
183
+ @offenses.any? { |o| o.location == location }
184
+ end
185
+
175
186
  def correct(node)
176
187
  return :unsupported unless support_autocorrect?
177
188
  return :uncorrected unless autocorrect?
@@ -29,34 +29,35 @@ module RuboCop
29
29
  def check(node)
30
30
  condition, = *node
31
31
 
32
- # assignments inside blocks are not what we're looking for
33
- return if condition.type == :block
32
+ return if condition.block_type?
33
+
34
34
  traverse_node(condition, ASGN_TYPES) do |asgn_node|
35
- if asgn_node.type == :send
36
- _receiver, method_name, *_args = *asgn_node
37
- next :skip_children if method_name !~ /=\z/
38
- end
39
-
40
- # skip safe assignment nodes if safe assignment is allowed
41
- if safe_assignment_allowed? && safe_assignment?(asgn_node)
42
- next :skip_children
43
- end
44
-
45
- # assignment nodes from shorthand ops like ||= don't have operator
46
- if asgn_node.type != :begin && asgn_node.loc.operator
47
- add_offense(asgn_node, :operator)
48
- end
35
+ next :skip_children if skip_children?(asgn_node)
36
+ next if allowed_construct?(asgn_node)
37
+
38
+ add_offense(asgn_node, :operator)
49
39
  end
50
40
  end
51
41
 
42
+ def allowed_construct?(asgn_node)
43
+ asgn_node.begin_type? || conditional_assignment?(asgn_node)
44
+ end
45
+
46
+ def conditional_assignment?(asgn_node)
47
+ !asgn_node.loc.operator
48
+ end
49
+
50
+ def skip_children?(asgn_node)
51
+ safe_assignment_allowed? && safe_assignment?(asgn_node)
52
+ end
53
+
52
54
  # each_node/visit_descendants_with_types with :skip_children
53
55
  def traverse_node(node, types, &block)
54
56
  result = yield node if types.include?(node.type)
55
- # return to skip all descendant nodes
57
+
56
58
  return if result == :skip_children
57
- node.children.each do |child|
58
- traverse_node(child, types, &block) if child.is_a?(Node)
59
- end
59
+
60
+ node.each_child_node { |child| traverse_node(child, types, &block) }
60
61
  end
61
62
  end
62
63
  end
@@ -181,8 +181,7 @@ module RuboCop
181
181
  if delta > 0
182
182
  add_space_before(loc_end, delta)
183
183
  elsif delta < 0
184
- source = node.source_range.source_buffer
185
- remove_space_before(loc_end.begin_pos, -delta, source)
184
+ remove_space_before(loc_end.begin_pos, -delta)
186
185
  end
187
186
  end
188
187
 
@@ -190,8 +189,8 @@ module RuboCop
190
189
  ->(corrector) { corrector.insert_before(loc, ' ' * delta) }
191
190
  end
192
191
 
193
- def remove_space_before(end_pos, delta, source)
194
- range = Parser::Source::Range.new(source, end_pos - delta, end_pos)
192
+ def remove_space_before(end_pos, delta)
193
+ range = range_between(end_pos - delta, end_pos)
195
194
  ->(corrector) { corrector.remove(range) }
196
195
  end
197
196
  end
@@ -32,9 +32,8 @@ module RuboCop
32
32
  _, _, method_def = *node
33
33
  expr = node.source_range
34
34
 
35
- line_start = Parser::Source::Range.new(expr.source_buffer,
36
- expr.begin_pos,
37
- method_def.loc.keyword.end_pos)
35
+ line_start = range_between(expr.begin_pos,
36
+ method_def.loc.keyword.end_pos)
38
37
  align_with = {
39
38
  def: method_def.loc.keyword,
40
39
  start_of_line: line_start
@@ -41,18 +41,28 @@ module RuboCop
41
41
  receiver, name, = *node
42
42
  if receiver.const_type?
43
43
  _, const_name = *receiver
44
- if (qualified = lookup_constant(node, const_name))
45
- found_method(node, "#{qualified}.#{name}")
46
- end
44
+ check_const_receiver(node, name, const_name)
47
45
  elsif receiver.self_type?
48
- if (enclosing = node.parent_module_name)
49
- found_method(node, "#{enclosing}.#{name}")
50
- end
46
+ check_self_receiver(node, name)
51
47
  end
52
48
  end
53
49
 
54
50
  private
55
51
 
52
+ def check_const_receiver(node, name, const_name)
53
+ qualified = lookup_constant(node, const_name)
54
+ return unless qualified
55
+
56
+ found_method(node, "#{qualified}.#{name}")
57
+ end
58
+
59
+ def check_self_receiver(node, name)
60
+ enclosing = node.parent_module_name
61
+ return unless enclosing
62
+
63
+ found_method(node, "#{enclosing}.#{name}")
64
+ end
65
+
56
66
  def message_for_dup(node, method_name)
57
67
  format(MSG, method_name, @definitions[method_name],
58
68
  source_location(node))
@@ -37,7 +37,7 @@ module RuboCop
37
37
 
38
38
  def check_else(node)
39
39
  _cond, _if_branch, else_branch = *node
40
- return unless else_branch && else_branch.type == :begin
40
+ return unless else_branch && else_branch.begin_type?
41
41
 
42
42
  first_else_expr = else_branch.children.first
43
43
  return unless first_else_expr.source_range.line == node.loc.else.line
@@ -13,7 +13,7 @@ module RuboCop
13
13
  MSG = 'Empty interpolation detected.'.freeze
14
14
 
15
15
  def on_dstr(node)
16
- node.children.select { |n| n.type == :begin }.each do |begin_node|
16
+ node.each_child_node(:begin) do |begin_node|
17
17
  add_offense(begin_node, :expression) if begin_node.children.empty?
18
18
  end
19
19
  end
@@ -84,9 +84,7 @@ module RuboCop
84
84
 
85
85
  align_with[:variable] =
86
86
  if !line_break_before_keyword?(expr, inner_node)
87
- Parser::Source::Range.new(expr.source_buffer,
88
- expr.begin_pos,
89
- inner_node.loc.keyword.end_pos)
87
+ range_between(expr.begin_pos, inner_node.loc.keyword.end_pos)
90
88
  else
91
89
  inner_node.loc.keyword
92
90
  end
@@ -130,9 +128,9 @@ module RuboCop
130
128
  buffer = expr.source_buffer
131
129
  source = buffer.source_line(expr.line)
132
130
  range = buffer.line_range(expr.line)
133
- Parser::Source::Range.new(buffer,
134
- range.begin_pos + (source =~ /\S/),
135
- range.begin_pos + (source =~ /\s*\z/))
131
+
132
+ range_between(range.begin_pos + (source =~ /\S/),
133
+ range.begin_pos + (source =~ /\s*\z/))
136
134
  end
137
135
  end
138
136
  end