rubocop 0.79.0 → 0.83.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +5 -5
  4. data/config/default.yml +184 -50
  5. data/lib/rubocop.rb +15 -5
  6. data/lib/rubocop/ast/builder.rb +2 -0
  7. data/lib/rubocop/ast/node.rb +12 -19
  8. data/lib/rubocop/ast/node/array_node.rb +13 -0
  9. data/lib/rubocop/ast/node/block_node.rb +5 -1
  10. data/lib/rubocop/ast/node/case_match_node.rb +56 -0
  11. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +8 -0
  12. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  13. data/lib/rubocop/ast/node/send_node.rb +4 -0
  14. data/lib/rubocop/ast/traversal.rb +20 -9
  15. data/lib/rubocop/cli.rb +11 -5
  16. data/lib/rubocop/comment_config.rb +6 -1
  17. data/lib/rubocop/config.rb +40 -10
  18. data/lib/rubocop/config_loader.rb +43 -33
  19. data/lib/rubocop/config_loader_resolver.rb +28 -1
  20. data/lib/rubocop/config_obsoletion.rb +4 -1
  21. data/lib/rubocop/config_validator.rb +18 -1
  22. data/lib/rubocop/cop/badge.rb +5 -5
  23. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
  24. data/lib/rubocop/cop/corrector.rb +48 -24
  25. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  26. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
  27. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  28. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
  29. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  30. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  31. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
  32. data/lib/rubocop/cop/generator.rb +3 -2
  33. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
  34. data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
  35. data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
  36. data/lib/rubocop/cop/layout/condition_position.rb +12 -2
  37. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  38. data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
  39. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  40. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +68 -0
  41. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  42. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -10
  43. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  44. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  45. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  46. data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
  47. data/lib/rubocop/cop/layout/leading_comment_space.rb +34 -3
  48. data/lib/rubocop/cop/layout/line_length.rb +36 -4
  49. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  50. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  51. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  52. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +13 -4
  53. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
  54. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
  55. data/lib/rubocop/cop/layout/space_around_operators.rb +37 -2
  56. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  57. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  58. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
  59. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  60. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
  61. data/lib/rubocop/cop/layout/trailing_whitespace.rb +2 -2
  62. data/lib/rubocop/cop/lint/ambiguous_operator.rb +38 -0
  63. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +14 -0
  64. data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
  65. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  66. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -5
  67. data/lib/rubocop/cop/lint/empty_when.rb +29 -6
  68. data/lib/rubocop/cop/lint/ensure_return.rb +18 -1
  69. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  70. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  71. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  72. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  73. data/lib/rubocop/cop/lint/literal_as_condition.rb +10 -13
  74. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  75. data/lib/rubocop/cop/lint/loop.rb +6 -4
  76. data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
  77. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  78. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  79. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  80. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +21 -9
  81. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
  82. data/lib/rubocop/cop/lint/raise_exception.rb +75 -0
  83. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -6
  84. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +12 -7
  85. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  86. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
  87. data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
  88. data/lib/rubocop/cop/lint/suppressed_exception.rb +12 -28
  89. data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
  90. data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
  91. data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
  92. data/lib/rubocop/cop/lint/useless_access_modifier.rb +12 -0
  93. data/lib/rubocop/cop/lint/useless_assignment.rb +3 -2
  94. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +5 -0
  95. data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -0
  96. data/lib/rubocop/cop/migration/department_name.rb +36 -10
  97. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  98. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
  99. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +16 -1
  100. data/lib/rubocop/cop/mixin/hash_transform_method.rb +178 -0
  101. data/lib/rubocop/cop/mixin/line_length_help.rb +2 -1
  102. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
  103. data/lib/rubocop/cop/mixin/parser_diagnostic.rb +1 -1
  104. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -22
  105. data/lib/rubocop/cop/mixin/target_ruby_version.rb +5 -1
  106. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -10
  107. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  108. data/lib/rubocop/cop/naming/constant_name.rb +2 -1
  109. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  110. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  111. data/lib/rubocop/cop/naming/method_name.rb +26 -0
  112. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  113. data/lib/rubocop/cop/registry.rb +11 -4
  114. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
  115. data/lib/rubocop/cop/style/alias.rb +4 -4
  116. data/lib/rubocop/cop/style/and_or.rb +5 -6
  117. data/lib/rubocop/cop/style/array_join.rb +1 -1
  118. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  119. data/lib/rubocop/cop/style/case_equality.rb +24 -1
  120. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  121. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  122. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
  123. data/lib/rubocop/cop/style/copyright.rb +1 -1
  124. data/lib/rubocop/cop/style/dir.rb +1 -1
  125. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
  126. data/lib/rubocop/cop/style/documentation.rb +43 -5
  127. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
  128. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  129. data/lib/rubocop/cop/style/each_with_object.rb +3 -3
  130. data/lib/rubocop/cop/style/empty_method.rb +1 -5
  131. data/lib/rubocop/cop/style/end_block.rb +6 -0
  132. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  133. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  134. data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
  135. data/lib/rubocop/cop/style/format_string.rb +2 -2
  136. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
  137. data/lib/rubocop/cop/style/guard_clause.rb +25 -2
  138. data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
  139. data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
  140. data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
  141. data/lib/rubocop/cop/style/hash_transform_values.rb +80 -0
  142. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
  143. data/lib/rubocop/cop/style/if_with_semicolon.rb +16 -0
  144. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  145. data/lib/rubocop/cop/style/lambda.rb +3 -2
  146. data/lib/rubocop/cop/style/lambda_call.rb +1 -21
  147. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +5 -0
  148. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +5 -4
  149. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  150. data/lib/rubocop/cop/style/module_function.rb +58 -12
  151. data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
  152. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  153. data/lib/rubocop/cop/style/multiline_when_then.rb +16 -1
  154. data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
  155. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
  156. data/lib/rubocop/cop/style/next.rb +2 -2
  157. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  158. data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
  159. data/lib/rubocop/cop/style/not.rb +1 -1
  160. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  161. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  162. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  163. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  164. data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
  165. data/lib/rubocop/cop/style/or_assignment.rb +4 -3
  166. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  167. data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
  168. data/lib/rubocop/cop/style/proc.rb +1 -1
  169. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  170. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  171. data/lib/rubocop/cop/style/redundant_condition.rb +18 -6
  172. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  173. data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
  174. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  175. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  176. data/lib/rubocop/cop/style/redundant_return.rb +5 -7
  177. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  178. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  179. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  180. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  181. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  182. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  183. data/lib/rubocop/cop/style/slicing_with_range.rb +39 -0
  184. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  185. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
  186. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
  187. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  188. data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
  189. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
  190. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +35 -22
  191. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
  192. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +88 -0
  193. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
  194. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  195. data/lib/rubocop/cop/style/unpack_first.rb +0 -4
  196. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  197. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  198. data/lib/rubocop/cop/style/word_array.rb +1 -1
  199. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
  200. data/lib/rubocop/cop/util.rb +24 -0
  201. data/lib/rubocop/cop/variable_force.rb +4 -1
  202. data/lib/rubocop/cop/variable_force/assignment.rb +1 -0
  203. data/lib/rubocop/cop/variable_force/scope.rb +1 -0
  204. data/lib/rubocop/cop/variable_force/variable.rb +1 -0
  205. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  206. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  207. data/lib/rubocop/formatter/junit_formatter.rb +74 -0
  208. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  209. data/lib/rubocop/formatter/tap_formatter.rb +1 -1
  210. data/lib/rubocop/name_similarity.rb +12 -9
  211. data/lib/rubocop/node_pattern.rb +96 -10
  212. data/lib/rubocop/options.rb +18 -5
  213. data/lib/rubocop/processed_source.rb +1 -4
  214. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  215. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  216. data/lib/rubocop/rspec/shared_contexts.rb +0 -4
  217. data/lib/rubocop/runner.rb +7 -2
  218. data/lib/rubocop/target_finder.rb +6 -4
  219. data/lib/rubocop/target_ruby.rb +2 -2
  220. data/lib/rubocop/version.rb +1 -1
  221. metadata +35 -25
  222. data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
  223. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
  224. data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
  225. data/lib/rubocop/string_util.rb +0 -14
@@ -113,7 +113,8 @@ module RuboCop
113
113
  def each_mentioned_cop
114
114
  each_directive do |comment, cop_names, disabled|
115
115
  comment_line_number = comment.loc.expression.line
116
- single_line = !comment_only_line?(comment_line_number)
116
+ single_line = !comment_only_line?(comment_line_number) ||
117
+ directive_on_comment_line?(comment)
117
118
 
118
119
  cop_names.each do |cop_name|
119
120
  yield qualified_cop_name(cop_name), disabled, comment_line_number,
@@ -122,6 +123,10 @@ module RuboCop
122
123
  end
123
124
  end
124
125
 
126
+ def directive_on_comment_line?(comment)
127
+ comment.text[1..-1].match(COMMENT_DIRECTIVE_REGEXP)
128
+ end
129
+
125
130
  def each_directive
126
131
  return if processed_source.comments.nil?
127
132
 
@@ -2,6 +2,9 @@
2
2
 
3
3
  require 'pathname'
4
4
 
5
+ # FIXME: Moving Rails department code to RuboCop Rails will remove
6
+ # the following rubocop:disable comment.
7
+ # rubocop:disable Metrics/ClassLength
5
8
  module RuboCop
6
9
  # This class represents the configuration of the RuboCop application
7
10
  # and all its cops. A Config is associated with a YAML configuration
@@ -13,6 +16,8 @@ module RuboCop
13
16
  include FileFinder
14
17
  extend Forwardable
15
18
 
19
+ CopConfig = Struct.new(:name, :metadata)
20
+
16
21
  DEFAULT_RAILS_VERSION = 5.0
17
22
  attr_reader :loaded_path
18
23
 
@@ -42,7 +47,7 @@ module RuboCop
42
47
  end
43
48
 
44
49
  def_delegators :@hash, :[], :[]=, :delete, :each, :key?, :keys, :each_key,
45
- :map, :merge, :to_h, :to_hash
50
+ :map, :merge, :to_h, :to_hash, :transform_values
46
51
  def_delegators :@validator, :validate, :target_ruby_version
47
52
 
48
53
  def to_s
@@ -111,6 +116,14 @@ module RuboCop
111
116
  @for_all_cops ||= self['AllCops'] || {}
112
117
  end
113
118
 
119
+ def disabled_new_cops?
120
+ for_all_cops['NewCops'] == 'disable'
121
+ end
122
+
123
+ def enabled_new_cops?
124
+ for_all_cops['NewCops'] == 'enable'
125
+ end
126
+
114
127
  def file_to_include?(file)
115
128
  relative_file_path = path_relative_to_config(file)
116
129
 
@@ -215,6 +228,18 @@ module RuboCop
215
228
  nil
216
229
  end
217
230
 
231
+ def pending_cops
232
+ keys.each_with_object([]) do |qualified_cop_name, pending_cops|
233
+ department = department_of(qualified_cop_name)
234
+ next if department && department['Enabled'] == false
235
+
236
+ cop_metadata = self[qualified_cop_name]
237
+ next unless cop_metadata['Enabled'] == 'pending'
238
+
239
+ pending_cops << CopConfig.new(qualified_cop_name, cop_metadata)
240
+ end
241
+ end
242
+
218
243
  private
219
244
 
220
245
  def target_rails_version_from_bundler_lock_file
@@ -235,17 +260,22 @@ module RuboCop
235
260
  end
236
261
 
237
262
  def enable_cop?(qualified_cop_name, cop_options)
238
- cop_department, cop_name = qualified_cop_name.split('/')
239
- department = cop_name.nil?
240
-
241
- unless department
242
- department_options = self[cop_department]
243
- if department_options && department_options['Enabled'] == false
244
- return false
245
- end
263
+ department = department_of(qualified_cop_name)
264
+ cop_enabled = cop_options.fetch('Enabled') do
265
+ !for_all_cops['DisabledByDefault']
246
266
  end
267
+ return true if cop_enabled == 'override_department'
268
+ return false if department && department['Enabled'] == false
269
+
270
+ cop_enabled
271
+ end
272
+
273
+ def department_of(qualified_cop_name)
274
+ cop_department, cop_name = qualified_cop_name.split('/')
275
+ return nil if cop_name.nil?
247
276
 
248
- cop_options.fetch('Enabled') { !for_all_cops['DisabledByDefault'] }
277
+ self[cop_department]
249
278
  end
250
279
  end
251
280
  end
281
+ # rubocop:enable Metrics/ClassLength
@@ -24,7 +24,7 @@ module RuboCop
24
24
  include FileFinder
25
25
 
26
26
  attr_accessor :debug, :auto_gen_config, :ignore_parent_exclusion,
27
- :options_config
27
+ :options_config, :disable_pending_cops, :enable_pending_cops
28
28
  attr_writer :default_configuration
29
29
 
30
30
  alias debug? debug
@@ -36,7 +36,7 @@ module RuboCop
36
36
  FileFinder.root_level = nil
37
37
  end
38
38
 
39
- def load_file(file)
39
+ def load_file(file) # rubocop:disable Metrics/AbcSize
40
40
  path = File.absolute_path(file.is_a?(RemoteConfig) ? file.file : file)
41
41
 
42
42
  hash = load_yaml_configuration(path)
@@ -46,6 +46,7 @@ module RuboCop
46
46
 
47
47
  add_missing_namespaces(path, hash)
48
48
 
49
+ resolver.override_department_setting_for_cops({}, hash)
49
50
  resolver.resolve_inheritance_from_gems(hash)
50
51
  resolver.resolve_inheritance(path, hash, file, debug?)
51
52
 
@@ -55,7 +56,10 @@ module RuboCop
55
56
  end
56
57
 
57
58
  def add_missing_namespaces(path, hash)
58
- hash.keys.each do |key|
59
+ # Using `hash.each_key` will cause the
60
+ # `can't add a new key into hash during iteration` error
61
+ hash_keys = hash.keys
62
+ hash_keys.each do |key|
59
63
  q = Cop::Cop.qualified_cop_name(key, path)
60
64
  next if q == key
61
65
 
@@ -76,30 +80,35 @@ module RuboCop
76
80
  # user's home directory is checked. If there's no .rubocop.yml
77
81
  # there either, the path to the default file is returned.
78
82
  def configuration_file_for(target_dir)
79
- find_project_dotfile(target_dir) ||
80
- find_user_dotfile ||
81
- find_user_xdg_config ||
82
- DEFAULT_FILE
83
+ find_project_dotfile(target_dir) || find_user_dotfile ||
84
+ find_user_xdg_config || DEFAULT_FILE
83
85
  end
84
86
 
85
87
  def configuration_from_file(config_file)
86
- config = load_file(config_file)
87
- return config if config_file == DEFAULT_FILE
88
+ return ConfigLoader.default_configuration if config_file == DEFAULT_FILE
88
89
 
90
+ config = load_file(config_file)
89
91
  if ignore_parent_exclusion?
90
92
  print 'Ignoring AllCops/Exclude from parent folders' if debug?
91
93
  else
92
94
  add_excludes_from_files(config, config_file)
93
95
  end
96
+
94
97
  merge_with_default(config, config_file).tap do |merged_config|
95
- warn_on_pending_cops(merged_config)
98
+ unless possible_new_cops?(config)
99
+ warn_on_pending_cops(merged_config.pending_cops)
100
+ end
96
101
  end
97
102
  end
98
103
 
104
+ def possible_new_cops?(config)
105
+ disable_pending_cops || enable_pending_cops ||
106
+ config.disabled_new_cops? || config.enabled_new_cops?
107
+ end
108
+
99
109
  def add_excludes_from_files(config, config_file)
100
- found_files =
101
- find_files_upwards(DOTFILE, config_file) +
102
- [find_user_dotfile, find_user_xdg_config].compact
110
+ found_files = find_files_upwards(DOTFILE, config_file) +
111
+ [find_user_dotfile, find_user_xdg_config].compact
103
112
 
104
113
  return if found_files.empty?
105
114
  return if PathUtil.relative_path(found_files.last) ==
@@ -116,20 +125,20 @@ module RuboCop
116
125
  end
117
126
  end
118
127
 
119
- def warn_on_pending_cops(config)
120
- pending_cops = config.keys.select do |key|
121
- config[key]['Enabled'] == 'pending'
122
- end
123
-
124
- return if pending_cops.none?
128
+ def warn_on_pending_cops(pending_cops)
129
+ return if pending_cops.empty?
125
130
 
126
131
  warn Rainbow('The following cops were added to RuboCop, but are not ' \
127
132
  'configured. Please set Enabled to either `true` or ' \
128
133
  '`false` in your `.rubocop.yml` file:').yellow
129
134
 
130
135
  pending_cops.each do |cop|
131
- warn Rainbow(" - #{cop}").yellow
136
+ version = cop.metadata['VersionAdded'] || 'N/A'
137
+
138
+ warn Rainbow(" - #{cop.name} (#{version})").yellow
132
139
  end
140
+
141
+ warn Rainbow('For more information: https://docs.rubocop.org/en/latest/versioning/').yellow
133
142
  end
134
143
 
135
144
  # Merges the given configuration with the default one. If
@@ -198,7 +207,9 @@ module RuboCop
198
207
  def write_config_file(file_name, file_string, rubocop_yml_contents)
199
208
  File.open(file_name, 'w') do |f|
200
209
  f.write "inherit_from:#{file_string}\n"
201
- f.write "\n#{rubocop_yml_contents}" if rubocop_yml_contents =~ /\S/
210
+ if /\S/.match?(rubocop_yml_contents)
211
+ f.write "\n#{rubocop_yml_contents}"
212
+ end
202
213
  end
203
214
  end
204
215
 
@@ -207,7 +218,10 @@ module RuboCop
207
218
  end
208
219
 
209
220
  def load_yaml_configuration(absolute_path)
210
- yaml_code = read_file(absolute_path)
221
+ file_contents = read_file(absolute_path)
222
+ yaml_code = Dir.chdir(File.dirname(absolute_path)) do
223
+ ERB.new(file_contents).result
224
+ end
211
225
  check_duplication(yaml_code, absolute_path)
212
226
  hash = yaml_safe_load(yaml_code, absolute_path) || {}
213
227
 
@@ -231,8 +245,7 @@ module RuboCop
231
245
  "#{smart_path}:#{line1}: " \
232
246
  "`#{value}` is concealed by line #{line2}"
233
247
  else
234
- "#{smart_path}: " \
235
- "`#{value}` is concealed by duplicate"
248
+ "#{smart_path}: `#{value}` is concealed by duplicate"
236
249
  end
237
250
  warn Rainbow(message).yellow
238
251
  end
@@ -250,17 +263,14 @@ module RuboCop
250
263
 
251
264
  def yaml_safe_load(yaml_code, filename)
252
265
  if defined?(SafeYAML) && SafeYAML.respond_to?(:load)
253
- SafeYAML.load(yaml_code, filename,
254
- whitelisted_tags: %w[!ruby/regexp])
266
+ SafeYAML.load(yaml_code, filename, whitelisted_tags: %w[!ruby/regexp])
255
267
  # Ruby 2.6+
256
268
  elsif Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0')
257
- YAML.safe_load(
258
- yaml_code,
259
- permitted_classes: [Regexp, Symbol],
260
- permitted_symbols: [],
261
- aliases: true,
262
- filename: filename
263
- )
269
+ YAML.safe_load(yaml_code,
270
+ permitted_classes: [Regexp, Symbol],
271
+ permitted_symbols: [],
272
+ aliases: true,
273
+ filename: filename)
264
274
  else
265
275
  YAML.safe_load(yaml_code, [Regexp, Symbol], [], true, filename)
266
276
  end
@@ -17,10 +17,12 @@ module RuboCop
17
17
  end
18
18
  end
19
19
 
20
+ # rubocop:disable Metrics/MethodLength
20
21
  def resolve_inheritance(path, hash, file, debug)
21
22
  inherited_files = Array(hash['inherit_from'])
22
23
  base_configs(path, inherited_files, file)
23
24
  .reverse.each_with_index do |base_config, index|
25
+ override_department_setting_for_cops(base_config, hash)
24
26
  base_config.each do |k, v|
25
27
  next unless v.is_a?(Hash)
26
28
 
@@ -34,6 +36,7 @@ module RuboCop
34
36
  end
35
37
  end
36
38
  end
39
+ # rubocop:enable Metrics/MethodLength
37
40
 
38
41
  def resolve_inheritance_from_gems(hash)
39
42
  gems = hash.delete('inherit_gem')
@@ -100,8 +103,32 @@ module RuboCop
100
103
  end
101
104
  # rubocop:enable Metrics/AbcSize
102
105
 
106
+ # An `Enabled: true` setting in user configuration for a cop overrides an
107
+ # `Enabled: false` setting for its department.
108
+ def override_department_setting_for_cops(base_hash, derived_hash)
109
+ derived_hash.each_key do |key|
110
+ next unless key =~ %r{(.*)/.*}
111
+
112
+ department = Regexp.last_match(1)
113
+ next unless disabled?(derived_hash, department) ||
114
+ disabled?(base_hash, department)
115
+
116
+ # The `override_department` setting for the `Enabled` parameter is an
117
+ # internal setting that's not documented in the manual. It will cause a
118
+ # cop to be enabled later, when logic surrounding enabled/disabled it
119
+ # run, even though its department is disabled.
120
+ if derived_hash[key]['Enabled']
121
+ derived_hash[key]['Enabled'] = 'override_department'
122
+ end
123
+ end
124
+ end
125
+
103
126
  private
104
127
 
128
+ def disabled?(hash, department)
129
+ hash[department] && hash[department]['Enabled'] == false
130
+ end
131
+
105
132
  def duplicate_setting?(base_hash, derived_hash, key, inherited_file)
106
133
  return false if inherited_file.nil? # Not inheritance resolving merge
107
134
  return false if inherited_file.start_with?('..') # Legitimate override
@@ -181,7 +208,7 @@ module RuboCop
181
208
  end
182
209
 
183
210
  def transform(config)
184
- Hash[config.map { |cop, params| [cop, yield(params)] }]
211
+ config.transform_values { |params| yield(params) }
185
212
  end
186
213
 
187
214
  def gem_config_path(gem_name, relative_config_path)
@@ -17,8 +17,10 @@ module RuboCop
17
17
  'Layout/IndentHash' => 'Layout/FirstHashElementIndentation',
18
18
  'Layout/IndentHeredoc' => 'Layout/HeredocIndentation',
19
19
  'Layout/LeadingBlankLines' => 'Layout/LeadingEmptyLines',
20
+ 'Layout/Tab' => 'Layout/IndentationStyle',
20
21
  'Layout/TrailingBlankLines' => 'Layout/TrailingEmptyLines',
21
22
  'Lint/DuplicatedKey' => 'Lint/DuplicateHashKey',
23
+ 'Lint/EndInMethod' => 'Style/EndBlock',
22
24
  'Lint/HandleExceptions' => 'Lint/SuppressedException',
23
25
  'Lint/MultipleCompare' => 'Lint/MultipleComparison',
24
26
  'Lint/StringConversionInInterpolation' => 'Lint/RedundantStringCoercion',
@@ -69,7 +71,8 @@ module RuboCop
69
71
  'Style/TrailingCommaInHashLiteral',
70
72
  'Style/TrailingCommaInLiteral' => 'Style/TrailingCommaInArrayLiteral ' \
71
73
  'and/or ' \
72
- 'Style/TrailingCommaInHashLiteral'
74
+ 'Style/TrailingCommaInHashLiteral',
75
+ 'Style/BracesAroundHashParameters' => nil
73
76
  }.map do |old_name, other_cops|
74
77
  if other_cops
75
78
  more = ". Please use #{other_cops} instead".gsub(%r{[A-Z]\w+/\w+},
@@ -13,6 +13,7 @@ module RuboCop
13
13
  INTERNAL_PARAMS = %w[Description StyleGuide
14
14
  VersionAdded VersionChanged VersionRemoved
15
15
  Reference Safe SafeAutoCorrect].freeze
16
+ NEW_COPS_VALUES = %w[pending disable enable].freeze
16
17
 
17
18
  def_delegators :@config, :smart_loaded_path, :for_all_cops
18
19
 
@@ -22,6 +23,7 @@ module RuboCop
22
23
  @target_ruby = TargetRuby.new(config)
23
24
  end
24
25
 
26
+ # rubocop:disable Metrics/AbcSize
25
27
  def validate
26
28
  check_cop_config_value(@config)
27
29
  reject_conflicting_safe_settings
@@ -37,11 +39,13 @@ module RuboCop
37
39
 
38
40
  alert_about_unrecognized_cops(invalid_cop_names)
39
41
  check_target_ruby
42
+ validate_new_cops_parameter
40
43
  validate_parameter_names(valid_cop_names)
41
44
  validate_enforced_styles(valid_cop_names)
42
45
  validate_syntax_cop
43
46
  reject_mutually_exclusive_defaults
44
47
  end
48
+ # rubocop:enable Metrics/AbcSize
45
49
 
46
50
  def target_ruby_version
47
51
  target_ruby.version
@@ -107,6 +111,18 @@ module RuboCop
107
111
  'It\'s not possible to disable this cop.'
108
112
  end
109
113
 
114
+ def validate_new_cops_parameter
115
+ new_cop_parameter = @config.for_all_cops['NewCops']
116
+ return if new_cop_parameter.nil? ||
117
+ NEW_COPS_VALUES.include?(new_cop_parameter)
118
+
119
+ message = "invalid #{new_cop_parameter} for `NewCops` found in" \
120
+ "#{smart_loaded_path}\n" \
121
+ "Valid choices are: #{NEW_COPS_VALUES.join(', ')}"
122
+
123
+ raise ValidationError, message
124
+ end
125
+
110
126
  def validate_parameter_names(valid_cop_names)
111
127
  valid_cop_names.each do |name|
112
128
  validate_section_presence(name)
@@ -190,7 +206,8 @@ module RuboCop
190
206
  SafeAutoCorrect
191
207
  AutoCorrect].include?(key) && value.is_a?(String)
192
208
 
193
- next if key == 'Enabled' && value == 'pending'
209
+ next if key == 'Enabled' &&
210
+ %w[pending override_department].include?(value)
194
211
 
195
212
  raise ValidationError, msg_not_boolean(parent, key, value)
196
213
  end
@@ -4,11 +4,11 @@ module RuboCop
4
4
  module Cop
5
5
  # Identifier of all cops containing a department and cop name.
6
6
  #
7
- # All cops are identified by their badge. For example, the badge
8
- # for `RuboCop::Cop::Layout::Tab` is `Layout/Tab`. Badges can be
9
- # parsed as either `Department/CopName` or just `CopName` to allow
10
- # for badge references in source files that omit the department
11
- # for RuboCop to infer.
7
+ # All cops are identified by their badge. For example, the badge for
8
+ # `RuboCop::Cop::Layout::IndentationStyle` is `Layout/IndentationStyle`.
9
+ # Badges can be parsed as either `Department/CopName` or just `CopName` to
10
+ # allow for badge references in source files that omit the department for
11
+ # RuboCop to infer.
12
12
  class Badge
13
13
  # Error raised when a badge parse fails.
14
14
  class InvalidBadge < Error
@@ -53,7 +53,7 @@ module RuboCop
53
53
  def autocorrect(node)
54
54
  lambda do |corrector|
55
55
  corrector.replace(
56
- node.first_argument.loc.expression, "'https://rubygems.org'"
56
+ node.first_argument, "'https://rubygems.org'"
57
57
  )
58
58
  end
59
59
  end
@@ -72,18 +72,18 @@ module RuboCop
72
72
 
73
73
  # Removes the source range.
74
74
  #
75
- # @param [Parser::Source::Range] range
76
- def remove(range)
77
- validate_range range
75
+ # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
76
+ def remove(node_or_range)
77
+ range = to_range(node_or_range)
78
78
  @source_rewriter.remove(range)
79
79
  end
80
80
 
81
81
  # Inserts new code before the given source range.
82
82
  #
83
- # @param [Parser::Source::Range] range
83
+ # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
84
84
  # @param [String] content
85
- def insert_before(range, content)
86
- validate_range range
85
+ def insert_before(node_or_range, content)
86
+ range = to_range(node_or_range)
87
87
  # TODO: Fix Cops using bad ranges instead
88
88
  if range.end_pos > @source_buffer.source.size
89
89
  range = range.with(end_pos: @source_buffer.source.size)
@@ -94,28 +94,38 @@ module RuboCop
94
94
 
95
95
  # Inserts new code after the given source range.
96
96
  #
97
- # @param [Parser::Source::Range] range
97
+ # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
98
98
  # @param [String] content
99
- def insert_after(range, content)
100
- validate_range range
99
+ def insert_after(node_or_range, content)
100
+ range = to_range(node_or_range)
101
101
  @source_rewriter.insert_after(range, content)
102
102
  end
103
103
 
104
+ # Wraps the given source range with the given before and after texts
105
+ #
106
+ # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
107
+ # @param [String] before
108
+ # @param [String] after
109
+ def wrap(node_or_range, before, after)
110
+ range = to_range(node_or_range)
111
+ @source_rewriter.wrap(range, before, after)
112
+ end
113
+
104
114
  # Replaces the code of the source range `range` with `content`.
105
115
  #
106
- # @param [Parser::Source::Range] range
116
+ # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
107
117
  # @param [String] content
108
- def replace(range, content)
109
- validate_range range
118
+ def replace(node_or_range, content)
119
+ range = to_range(node_or_range)
110
120
  @source_rewriter.replace(range, content)
111
121
  end
112
122
 
113
123
  # Removes `size` characters prior to the source range.
114
124
  #
115
- # @param [Parser::Source::Range] range
125
+ # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
116
126
  # @param [Integer] size
117
- def remove_preceding(range, size)
118
- validate_range range
127
+ def remove_preceding(node_or_range, size)
128
+ range = to_range(node_or_range)
119
129
  to_remove = Parser::Source::Range.new(range.source_buffer,
120
130
  range.begin_pos - size,
121
131
  range.begin_pos)
@@ -126,10 +136,10 @@ module RuboCop
126
136
  # If `size` is greater than the size of `range`, the removed region can
127
137
  # overrun the end of `range`.
128
138
  #
129
- # @param [Parser::Source::Range] range
139
+ # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
130
140
  # @param [Integer] size
131
- def remove_leading(range, size)
132
- validate_range range
141
+ def remove_leading(node_or_range, size)
142
+ range = to_range(node_or_range)
133
143
  to_remove = Parser::Source::Range.new(range.source_buffer,
134
144
  range.begin_pos,
135
145
  range.begin_pos + size)
@@ -140,10 +150,10 @@ module RuboCop
140
150
  # If `size` is greater than the size of `range`, the removed region can
141
151
  # overrun the beginning of `range`.
142
152
  #
143
- # @param [Parser::Source::Range] range
153
+ # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
144
154
  # @param [Integer] size
145
- def remove_trailing(range, size)
146
- validate_range range
155
+ def remove_trailing(node_or_range, size)
156
+ range = to_range(node_or_range)
147
157
  to_remove = Parser::Source::Range.new(range.source_buffer,
148
158
  range.end_pos - size,
149
159
  range.end_pos)
@@ -153,11 +163,25 @@ module RuboCop
153
163
  private
154
164
 
155
165
  # :nodoc:
156
- def validate_range(range)
157
- buffer = range.source_buffer
166
+ def to_range(node_or_range)
167
+ range = case node_or_range
168
+ when ::RuboCop::AST::Node, ::Parser::Source::Comment
169
+ node_or_range.loc.expression
170
+ when ::Parser::Source::Range
171
+ node_or_range
172
+ else
173
+ raise TypeError,
174
+ 'Expected a Parser::Source::Range, Comment or ' \
175
+ "Rubocop::AST::Node, got #{node_or_range.class}"
176
+ end
177
+ validate_buffer(range.source_buffer)
178
+ range
179
+ end
180
+
181
+ def validate_buffer(buffer)
158
182
  return if buffer == @source_buffer
159
183
 
160
- unless buffer.is_a?(Parser::Source::Buffer)
184
+ unless buffer.is_a?(::Parser::Source::Buffer)
161
185
  # actually this should be enforced by parser gem
162
186
  raise 'Corrector expected range source buffer to be a ' \
163
187
  "Parser::Source::Buffer, but got #{buffer.class}"