rubocop 0.93.1 → 1.6.1

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 (199) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +36 -16
  3. data/config/default.yml +276 -82
  4. data/config/obsoletion.yml +196 -0
  5. data/exe/rubocop +1 -1
  6. data/lib/rubocop.rb +31 -2
  7. data/lib/rubocop/cli.rb +5 -1
  8. data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
  9. data/lib/rubocop/cli/command/execute_runner.rb +26 -11
  10. data/lib/rubocop/cli/command/suggest_extensions.rb +80 -0
  11. data/lib/rubocop/cli/command/version.rb +1 -1
  12. data/lib/rubocop/comment_config.rb +1 -1
  13. data/lib/rubocop/config.rb +4 -0
  14. data/lib/rubocop/config_loader.rb +34 -8
  15. data/lib/rubocop/config_loader_resolver.rb +12 -6
  16. data/lib/rubocop/config_obsoletion.rb +65 -247
  17. data/lib/rubocop/config_obsoletion/changed_enforced_styles.rb +33 -0
  18. data/lib/rubocop/config_obsoletion/changed_parameter.rb +21 -0
  19. data/lib/rubocop/config_obsoletion/cop_rule.rb +34 -0
  20. data/lib/rubocop/config_obsoletion/extracted_cop.rb +44 -0
  21. data/lib/rubocop/config_obsoletion/parameter_rule.rb +44 -0
  22. data/lib/rubocop/config_obsoletion/removed_cop.rb +41 -0
  23. data/lib/rubocop/config_obsoletion/renamed_cop.rb +34 -0
  24. data/lib/rubocop/config_obsoletion/rule.rb +41 -0
  25. data/lib/rubocop/config_obsoletion/split_cop.rb +27 -0
  26. data/lib/rubocop/config_regeneration.rb +1 -1
  27. data/lib/rubocop/config_validator.rb +25 -10
  28. data/lib/rubocop/cop/autocorrect_logic.rb +21 -6
  29. data/lib/rubocop/cop/badge.rb +9 -24
  30. data/lib/rubocop/cop/base.rb +33 -16
  31. data/lib/rubocop/cop/bundler/duplicated_gem.rb +26 -6
  32. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  33. data/lib/rubocop/cop/commissioner.rb +37 -23
  34. data/lib/rubocop/cop/cop.rb +2 -2
  35. data/lib/rubocop/cop/corrector.rb +3 -1
  36. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  37. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +6 -8
  38. data/lib/rubocop/cop/force.rb +1 -1
  39. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -3
  40. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +4 -5
  41. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  42. data/lib/rubocop/cop/generator.rb +3 -10
  43. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  44. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
  45. data/lib/rubocop/cop/layout/block_alignment.rb +3 -4
  46. data/lib/rubocop/cop/layout/class_structure.rb +22 -3
  47. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  48. data/lib/rubocop/cop/layout/else_alignment.rb +15 -2
  49. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +80 -10
  50. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
  51. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +6 -1
  52. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -1
  53. data/lib/rubocop/cop/layout/end_alignment.rb +3 -3
  54. data/lib/rubocop/cop/layout/end_of_line.rb +5 -5
  55. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -2
  56. data/lib/rubocop/cop/layout/first_argument_indentation.rb +7 -2
  57. data/lib/rubocop/cop/layout/hash_alignment.rb +4 -4
  58. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +12 -0
  59. data/lib/rubocop/cop/layout/line_length.rb +10 -13
  60. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +7 -3
  61. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +24 -18
  62. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  63. data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
  64. data/lib/rubocop/cop/layout/space_inside_parens.rb +35 -13
  65. data/lib/rubocop/cop/layout/trailing_whitespace.rb +37 -13
  66. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -1
  67. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +26 -2
  68. data/lib/rubocop/cop/lint/debugger.rb +17 -28
  69. data/lib/rubocop/cop/lint/duplicate_branch.rb +93 -0
  70. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +2 -12
  71. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +77 -0
  72. data/lib/rubocop/cop/lint/else_layout.rb +29 -3
  73. data/lib/rubocop/cop/lint/empty_block.rb +82 -0
  74. data/lib/rubocop/cop/lint/empty_class.rb +93 -0
  75. data/lib/rubocop/cop/lint/flip_flop.rb +8 -2
  76. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
  77. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +39 -7
  78. data/lib/rubocop/cop/lint/loop.rb +4 -4
  79. data/lib/rubocop/cop/lint/missing_super.rb +7 -4
  80. data/lib/rubocop/cop/lint/nested_percent_literal.rb +14 -0
  81. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +58 -0
  82. data/lib/rubocop/cop/lint/number_conversion.rb +46 -13
  83. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +27 -8
  84. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +19 -16
  85. data/lib/rubocop/cop/lint/shadowed_exception.rb +4 -5
  86. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +13 -0
  87. data/lib/rubocop/cop/lint/to_enum_arguments.rb +86 -0
  88. data/lib/rubocop/cop/lint/to_json.rb +1 -1
  89. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +85 -0
  90. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +199 -0
  91. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
  92. data/lib/rubocop/cop/lint/useless_method_definition.rb +2 -4
  93. data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -1
  94. data/lib/rubocop/cop/metrics/abc_size.rb +25 -1
  95. data/lib/rubocop/cop/metrics/block_length.rb +13 -7
  96. data/lib/rubocop/cop/metrics/method_length.rb +7 -2
  97. data/lib/rubocop/cop/metrics/parameter_lists.rb +68 -2
  98. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +20 -10
  99. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +146 -0
  100. data/lib/rubocop/cop/metrics/utils/repeated_csend_discount.rb +6 -1
  101. data/lib/rubocop/cop/migration/department_name.rb +1 -1
  102. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  103. data/lib/rubocop/cop/mixin/configurable_numbering.rb +4 -3
  104. data/lib/rubocop/cop/mixin/enforce_superclass.rb +9 -1
  105. data/lib/rubocop/cop/mixin/ignored_methods.rb +36 -3
  106. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  107. data/lib/rubocop/cop/mixin/method_complexity.rb +6 -0
  108. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  109. data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -4
  110. data/lib/rubocop/cop/mixin/string_help.rb +4 -1
  111. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -3
  112. data/lib/rubocop/cop/naming/accessor_method_name.rb +15 -1
  113. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +12 -2
  114. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +11 -5
  115. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +67 -18
  116. data/lib/rubocop/cop/naming/predicate_name.rb +2 -1
  117. data/lib/rubocop/cop/naming/variable_number.rb +100 -8
  118. data/lib/rubocop/cop/offense.rb +3 -3
  119. data/lib/rubocop/cop/security/open.rb +12 -10
  120. data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
  121. data/lib/rubocop/cop/style/and_or.rb +11 -3
  122. data/lib/rubocop/cop/style/arguments_forwarding.rb +142 -0
  123. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +0 -4
  124. data/lib/rubocop/cop/style/case_like_if.rb +0 -4
  125. data/lib/rubocop/cop/style/character_literal.rb +10 -11
  126. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -3
  127. data/lib/rubocop/cop/style/collection_compact.rb +91 -0
  128. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +169 -0
  129. data/lib/rubocop/cop/style/documentation.rb +12 -1
  130. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  131. data/lib/rubocop/cop/style/float_division.rb +44 -1
  132. data/lib/rubocop/cop/style/format_string.rb +8 -3
  133. data/lib/rubocop/cop/style/format_string_token.rb +47 -2
  134. data/lib/rubocop/cop/style/hash_syntax.rb +3 -3
  135. data/lib/rubocop/cop/style/identical_conditional_branches.rb +7 -2
  136. data/lib/rubocop/cop/style/if_inside_else.rb +37 -1
  137. data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -3
  138. data/lib/rubocop/cop/style/if_with_semicolon.rb +39 -4
  139. data/lib/rubocop/cop/style/infinite_loop.rb +4 -0
  140. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  141. data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -0
  142. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +10 -13
  143. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +8 -13
  144. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +7 -11
  145. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +11 -2
  146. data/lib/rubocop/cop/style/mixin_grouping.rb +0 -4
  147. data/lib/rubocop/cop/style/multiple_comparison.rb +55 -7
  148. data/lib/rubocop/cop/style/negated_if_else_condition.rb +106 -0
  149. data/lib/rubocop/cop/style/nil_lambda.rb +52 -0
  150. data/lib/rubocop/cop/style/numeric_literals.rb +14 -11
  151. data/lib/rubocop/cop/style/perl_backrefs.rb +86 -9
  152. data/lib/rubocop/cop/style/raise_args.rb +21 -6
  153. data/lib/rubocop/cop/style/redundant_argument.rb +88 -0
  154. data/lib/rubocop/cop/style/redundant_condition.rb +2 -1
  155. data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
  156. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +7 -1
  157. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +24 -8
  158. data/lib/rubocop/cop/style/redundant_self.rb +3 -0
  159. data/lib/rubocop/cop/style/safe_navigation.rb +16 -4
  160. data/lib/rubocop/cop/style/semicolon.rb +3 -0
  161. data/lib/rubocop/cop/style/single_line_block_params.rb +30 -7
  162. data/lib/rubocop/cop/style/sole_nested_conditional.rb +65 -3
  163. data/lib/rubocop/cop/style/special_global_vars.rb +1 -13
  164. data/lib/rubocop/cop/style/static_class.rb +97 -0
  165. data/lib/rubocop/cop/style/string_concatenation.rb +39 -2
  166. data/lib/rubocop/cop/style/string_literals.rb +14 -8
  167. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +4 -3
  168. data/lib/rubocop/cop/style/swap_values.rb +108 -0
  169. data/lib/rubocop/cop/style/symbol_proc.rb +5 -3
  170. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +3 -1
  171. data/lib/rubocop/cop/style/while_until_modifier.rb +9 -0
  172. data/lib/rubocop/cop/team.rb +6 -1
  173. data/lib/rubocop/cop/util.rb +6 -2
  174. data/lib/rubocop/cop/variable_force/branch.rb +1 -1
  175. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  176. data/lib/rubocop/core_ext/hash.rb +20 -0
  177. data/lib/rubocop/ext/regexp_node.rb +36 -11
  178. data/lib/rubocop/ext/regexp_parser.rb +95 -0
  179. data/lib/rubocop/formatter/disabled_config_formatter.rb +21 -6
  180. data/lib/rubocop/formatter/emacs_style_formatter.rb +2 -0
  181. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  182. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +47 -0
  183. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  184. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -0
  185. data/lib/rubocop/formatter/tap_formatter.rb +2 -0
  186. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  187. data/lib/rubocop/lockfile.rb +40 -0
  188. data/lib/rubocop/magic_comment.rb +2 -2
  189. data/lib/rubocop/options.rb +11 -1
  190. data/lib/rubocop/rake_task.rb +2 -2
  191. data/lib/rubocop/rspec/shared_contexts.rb +4 -0
  192. data/lib/rubocop/runner.rb +1 -1
  193. data/lib/rubocop/target_finder.rb +1 -1
  194. data/lib/rubocop/target_ruby.rb +65 -1
  195. data/lib/rubocop/version.rb +56 -6
  196. metadata +50 -9
  197. data/bin/console +0 -10
  198. data/bin/rubocop-profile +0 -32
  199. data/bin/setup +0 -7
@@ -10,37 +10,22 @@ module RuboCop
10
10
  # allow for badge references in source files that omit the department for
11
11
  # RuboCop to infer.
12
12
  class Badge
13
- # Error raised when a badge parse fails.
14
- class InvalidBadge < Error
15
- MSG = 'Invalid badge %<badge>p. ' \
16
- 'Expected `Department/CopName` or `CopName`.'
17
-
18
- def initialize(token)
19
- super(format(MSG, badge: token))
20
- end
21
- end
22
-
23
13
  attr_reader :department, :cop_name
24
14
 
25
15
  def self.for(class_name)
26
- new(*class_name.split('::').last(2))
16
+ parts = class_name.split('::')
17
+ name_deep_enough = parts.length >= 4
18
+ new(name_deep_enough ? parts[2..-1] : parts.last(2))
27
19
  end
28
20
 
29
21
  def self.parse(identifier)
30
- parts = identifier.split('/', 2)
31
-
32
- raise InvalidBadge, identifier if parts.size > 2
33
-
34
- if parts.one?
35
- new(nil, *parts)
36
- else
37
- new(*parts)
38
- end
22
+ new(identifier.split('/'))
39
23
  end
40
24
 
41
- def initialize(department, cop_name)
42
- @department = department.to_sym if department
43
- @cop_name = cop_name
25
+ def initialize(class_name_parts)
26
+ department_parts = class_name_parts[0...-1]
27
+ @department = (department_parts.join('/').to_sym unless department_parts.empty?)
28
+ @cop_name = class_name_parts.last
44
29
  end
45
30
 
46
31
  def ==(other)
@@ -66,7 +51,7 @@ module RuboCop
66
51
  end
67
52
 
68
53
  def with_department(department)
69
- self.class.new(department, cop_name)
54
+ self.class.new([department.to_s.split('/'), cop_name].flatten)
70
55
  end
71
56
  end
72
57
  end
@@ -261,6 +261,21 @@ module RuboCop
261
261
  'they are returned as the result of the investigation'
262
262
  end
263
263
 
264
+ ### Reserved for Commissioner
265
+
266
+ # @api private
267
+ def callbacks_needed
268
+ self.class.callbacks_needed
269
+ end
270
+
271
+ # @api private
272
+ def self.callbacks_needed
273
+ @callbacks_needed ||= public_instance_methods.select do |m|
274
+ m.match?(/^on_|^after_/) &&
275
+ !Base.method_defined?(m) # exclude standard "callbacks" like 'on_begin_investigation'
276
+ end
277
+ end
278
+
264
279
  private
265
280
 
266
281
  ### Reserved for Cop::Cop
@@ -273,13 +288,6 @@ module RuboCop
273
288
  @current_corrector&.merge!(corrector) if corrector
274
289
  end
275
290
 
276
- def correction_strategy
277
- return :unsupported unless correctable?
278
- return :uncorrected unless autocorrect?
279
-
280
- :attempt_correction
281
- end
282
-
283
291
  ### Reserved for Commissioner:
284
292
 
285
293
  def current_offense_locations
@@ -291,7 +299,7 @@ module RuboCop
291
299
  end
292
300
 
293
301
  private_class_method def self.restrict_on_send
294
- @restrict_on_send ||= self::RESTRICT_ON_SEND.to_set.freeze
302
+ @restrict_on_send ||= self::RESTRICT_ON_SEND.to_a.freeze
295
303
  end
296
304
 
297
305
  # Called before any investigation
@@ -326,33 +334,42 @@ module RuboCop
326
334
 
327
335
  # @return [Symbol, Corrector] offense status
328
336
  def correct(range)
329
- status = correction_strategy
330
-
331
337
  if block_given?
332
338
  corrector = Corrector.new(self)
333
339
  yield corrector
334
- if !corrector.empty? && !self.class.support_autocorrect?
340
+ if corrector.empty?
341
+ corrector = nil
342
+ elsif !self.class.support_autocorrect?
335
343
  raise "The Cop #{name} must `extend AutoCorrector` to be able to autocorrect"
336
344
  end
337
345
  end
338
346
 
339
- status = attempt_correction(range, corrector) if status == :attempt_correction
347
+ [use_corrector(range, corrector), corrector]
348
+ end
340
349
 
341
- [status, corrector]
350
+ # @return [Symbol] offense status
351
+ def use_corrector(range, corrector)
352
+ if autocorrect?
353
+ attempt_correction(range, corrector)
354
+ elsif corrector
355
+ :uncorrected
356
+ else
357
+ :unsupported
358
+ end
342
359
  end
343
360
 
344
361
  # @return [Symbol] offense status
345
362
  def attempt_correction(range, corrector)
346
- if corrector && !corrector.empty?
363
+ if corrector
347
364
  status = :corrected
348
365
  elsif disable_uncorrectable?
349
366
  corrector = disable_uncorrectable(range)
350
367
  status = :corrected_with_todo
351
368
  else
352
- return :uncorrected
369
+ return :unsupported
353
370
  end
354
371
 
355
- apply_correction(corrector) if corrector
372
+ apply_correction(corrector)
356
373
  status
357
374
  end
358
375
 
@@ -25,13 +25,23 @@ module RuboCop
25
25
  #
26
26
  # # good
27
27
  # gem 'rubocop', groups: [:development, :test]
28
- class DuplicatedGem < Cop
28
+ #
29
+ # # good - conditional declaration
30
+ # if Dir.exist?(local)
31
+ # gem 'rubocop', path: local
32
+ # elsif ENV['RUBOCOP_VERSION'] == 'master'
33
+ # gem 'rubocop', git: 'https://github.com/rubocop-hq/rubocop.git'
34
+ # else
35
+ # gem 'rubocop', '~> 0.90.0'
36
+ # end
37
+ #
38
+ class DuplicatedGem < Base
29
39
  include RangeHelp
30
40
 
31
41
  MSG = 'Gem `%<gem_name>s` requirements already given on line '\
32
42
  '%<line_of_first_occurrence>d of the Gemfile.'
33
43
 
34
- def investigate(processed_source)
44
+ def on_new_investigation
35
45
  return if processed_source.blank?
36
46
 
37
47
  duplicated_gem_nodes.each do |nodes|
@@ -53,11 +63,21 @@ module RuboCop
53
63
  gem_declarations(processed_source.ast)
54
64
  .group_by(&:first_argument)
55
65
  .values
56
- .select { |nodes| nodes.size > 1 && !condition?(nodes) }
66
+ .select { |nodes| nodes.size > 1 && !conditional_declaration?(nodes) }
57
67
  end
58
68
 
59
- def condition?(nodes)
60
- nodes[0].parent&.if_type? && nodes[0].parent == nodes[1].parent
69
+ def conditional_declaration?(nodes)
70
+ parent = nodes[0].parent
71
+ return false unless parent&.if_type? || parent&.when_type?
72
+
73
+ root_conditional_node = parent.if_type? ? parent : parent.parent
74
+ nodes.all? { |node| within_conditional?(node, root_conditional_node) }
75
+ end
76
+
77
+ def within_conditional?(node, conditional_node)
78
+ conditional_node.branches.any? do |branch|
79
+ branch == node || branch.child_nodes.include?(node)
80
+ end
61
81
  end
62
82
 
63
83
  def register_offense(node, gem_name, line_of_first_occurrence)
@@ -69,7 +89,7 @@ module RuboCop
69
89
  gem_name: gem_name,
70
90
  line_of_first_occurrence: line_of_first_occurrence
71
91
  )
72
- add_offense(node, location: offense_location, message: message)
92
+ add_offense(offense_location, message: message)
73
93
  end
74
94
  end
75
95
  end
@@ -58,7 +58,7 @@ module RuboCop
58
58
  # # Version 2.1 introduces breaking change baz
59
59
  # gem 'bar', '< 2.1'
60
60
  #
61
- class GemComment < Cop
61
+ class GemComment < Base
62
62
  include DefNode
63
63
 
64
64
  MSG = 'Missing gem description comment.'
@@ -7,7 +7,7 @@ module RuboCop
7
7
  class Commissioner
8
8
  include RuboCop::AST::Traversal
9
9
 
10
- RESTRICTED_CALLBACKS = %i[on_send on_csend].freeze
10
+ RESTRICTED_CALLBACKS = %i[on_send on_csend after_send after_csend].freeze
11
11
  private_constant :RESTRICTED_CALLBACKS
12
12
 
13
13
  # How a Commissioner returns the results of the investigation
@@ -45,8 +45,7 @@ module RuboCop
45
45
  @cops = cops
46
46
  @forces = forces
47
47
  @options = options
48
- @callbacks = Hash.new { |h, k| h[k] = cops_callbacks_for(k) }
49
- @restricted_map = {}
48
+ initialize_callbacks
50
49
 
51
50
  reset
52
51
  end
@@ -61,16 +60,18 @@ module RuboCop
61
60
  method_name = :"on_#{node_type}"
62
61
  next unless method_defined?(method_name)
63
62
 
64
- if RESTRICTED_CALLBACKS.include?(method_name)
65
- trigger_restricted = "trigger_restricted_cops(:on_#{node_type}, node)"
66
- end
63
+ # Hacky: Comment-out code as needed
64
+ r = '#' unless RESTRICTED_CALLBACKS.include?(method_name) # has Restricted?
65
+ c = '#' if NO_CHILD_NODES.include?(node_type) # has Children?
67
66
 
68
67
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
69
- def on_#{node_type}(node)
70
- trigger_responding_cops(:on_#{node_type}, node)
71
- #{trigger_restricted}
72
- #{'super(node)' unless NO_CHILD_NODES.include?(node_type)}
73
- end
68
+ def on_#{node_type}(node) # def on_send(node)
69
+ trigger_responding_cops(:on_#{node_type}, node) # trigger_responding_cops(:on_send, node)
70
+ #{r} trigger_restricted_cops(:on_#{node_type}, node) # trigger_restricted_cops(:on_send, node)
71
+ #{c} super(node) # super(node)
72
+ #{c} trigger_responding_cops(:after_#{node_type}, node) # trigger_responding_cops(:after_send, node)
73
+ #{c}#{r} trigger_restricted_cops(:after_#{node_type}, node) # trigger_restricted_cops(:after_send, node)
74
+ end # end
74
75
  RUBY
75
76
  end
76
77
 
@@ -94,9 +95,9 @@ module RuboCop
94
95
  private
95
96
 
96
97
  def trigger_responding_cops(callback, node)
97
- @callbacks[callback].each do |cop|
98
+ @callbacks[callback]&.each do |cop|
98
99
  with_cop_error_handling(cop, node) do
99
- cop.send(callback, node)
100
+ cop.public_send(callback, node)
100
101
  end
101
102
  end
102
103
  end
@@ -105,29 +106,42 @@ module RuboCop
105
106
  @errors = []
106
107
  end
107
108
 
108
- def cops_callbacks_for(callback)
109
- callbacks = @cops.select do |cop|
110
- cop.respond_to?(callback)
111
- end
112
- if RESTRICTED_CALLBACKS.include?(callback)
113
- @restricted_map[callback] = restricted_map(callbacks)
109
+ def initialize_callbacks
110
+ @callbacks = build_callbacks(@cops)
111
+ @restricted_map = restrict_callbacks(@callbacks)
112
+ end
113
+
114
+ def build_callbacks(cops)
115
+ callbacks = {}
116
+ cops.each do |cop|
117
+ cop.callbacks_needed.each do |callback|
118
+ (callbacks[callback] ||= []) << cop
119
+ end
114
120
  end
115
121
  callbacks
116
122
  end
117
123
 
124
+ def restrict_callbacks(callbacks)
125
+ restricted = {}
126
+ RESTRICTED_CALLBACKS.each do |callback|
127
+ restricted[callback] = restricted_map(callbacks[callback])
128
+ end
129
+ restricted
130
+ end
131
+
118
132
  def trigger_restricted_cops(event, node)
119
133
  name = node.method_name
120
- @restricted_map.fetch(event)[name]&.each do |cop|
134
+ @restricted_map[event][name]&.each do |cop|
121
135
  with_cop_error_handling(cop, node) do
122
- cop.send(event, node)
136
+ cop.public_send(event, node)
123
137
  end
124
138
  end
125
139
  end
126
140
 
127
- # Note: mutates `callbacks` in place
141
+ # NOTE: mutates `callbacks` in place
128
142
  def restricted_map(callbacks)
129
143
  map = {}
130
- callbacks.select! do |cop|
144
+ callbacks&.select! do |cop|
131
145
  restrictions = cop.class.send :restrict_on_send
132
146
  restrictions.each do |name|
133
147
  (map[name] ||= []) << cop
@@ -27,7 +27,7 @@ module RuboCop
27
27
  def add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
28
28
  @v0_argument = node_or_range
29
29
  range = find_location(node_or_range, location)
30
- if block.nil? && !autocorrect?
30
+ if block.nil? && !support_autocorrect?
31
31
  super(range, message: message, severity: severity)
32
32
  else
33
33
  super(range, message: message, severity: severity) do |corrector|
@@ -136,7 +136,7 @@ module RuboCop
136
136
  end
137
137
 
138
138
  def correction_lambda
139
- return unless correction_strategy == :attempt_correction && support_autocorrect?
139
+ return unless support_autocorrect?
140
140
 
141
141
  dedup_on_node(@v0_argument) do
142
142
  autocorrect(@v0_argument)
@@ -9,6 +9,8 @@ module RuboCop
9
9
  # The nodes modified by the corrections should be part of the
10
10
  # AST of the source_buffer.
11
11
  class Corrector < ::Parser::Source::TreeRewriter
12
+ NOOP_CONSUMER = ->(diagnostic) {} # noop
13
+
12
14
  # @param source [Parser::Source::Buffer, or anything
13
15
  # leading to one via `(processed_source.)buffer`]
14
16
  #
@@ -23,7 +25,7 @@ module RuboCop
23
25
  )
24
26
 
25
27
  # Don't print warnings to stderr if corrections conflict with each other
26
- diagnostics.consumer = ->(diagnostic) {}
28
+ diagnostics.consumer = NOOP_CONSUMER
27
29
  end
28
30
 
29
31
  alias rewrite process # Legacy
@@ -48,7 +48,7 @@ module RuboCop
48
48
  def autocorrect_multiline_words(node, escape, delimiters)
49
49
  contents = process_multiline_words(node, escape, delimiters)
50
50
  contents << end_content(node.source)
51
- contents.join('')
51
+ contents.join
52
52
  end
53
53
 
54
54
  def autocorrect_words(node, escape, delimiters)
@@ -89,7 +89,7 @@ module RuboCop
89
89
  begin_line_num = previous_line_num - base_line_num + 1
90
90
  end_line_num = node.first_line - base_line_num + 1
91
91
  lines = source_in_lines[begin_line_num...end_line_num]
92
- "\n#{(lines.join("\n").split(node.source).first || '')}"
92
+ "\n#{lines.join("\n").split(node.source).first || ''}"
93
93
  end
94
94
 
95
95
  def fix_escaped_content(word_node, escape, delimiters)
@@ -7,16 +7,14 @@ module RuboCop
7
7
  extend Util
8
8
 
9
9
  class << self
10
- def correct(node, style)
10
+ def correct(corrector, node, style)
11
11
  return if node.dstr_type?
12
12
 
13
- lambda do |corrector|
14
- str = node.str_content
15
- if style == :single_quotes
16
- corrector.replace(node, to_string_literal(str))
17
- else
18
- corrector.replace(node, str.inspect)
19
- end
13
+ str = node.str_content
14
+ if style == :single_quotes
15
+ corrector.replace(node, to_string_literal(str))
16
+ else
17
+ corrector.replace(node, str.inspect)
20
18
  end
21
19
  end
22
20
  end
@@ -31,7 +31,7 @@ module RuboCop
31
31
  cops.each do |cop|
32
32
  next unless cop.respond_to?(method_name)
33
33
 
34
- cop.send(method_name, *args)
34
+ cop.public_send(method_name, *args)
35
35
  end
36
36
  end
37
37
 
@@ -34,7 +34,7 @@ module RuboCop
34
34
  # spec.add_runtime_dependency('parallel', '~> 1.10')
35
35
  # spec.add_runtime_dependency('parser', '>= 2.3.3.1', '< 3.0')
36
36
  # end
37
- class DuplicatedAssignment < Cop
37
+ class DuplicatedAssignment < Base
38
38
  include RangeHelp
39
39
 
40
40
  MSG = '`%<assignment>s` method calls already given on line '\
@@ -54,7 +54,7 @@ module RuboCop
54
54
  (lvar #match_block_variable_name?) #assignment_method? ...)
55
55
  PATTERN
56
56
 
57
- def investigate(processed_source)
57
+ def on_new_investigation
58
58
  return if processed_source.blank?
59
59
 
60
60
  duplicated_assignment_method_nodes.each do |nodes|
@@ -96,7 +96,7 @@ module RuboCop
96
96
  assignment: assignment,
97
97
  line_of_first_occurrence: line_of_first_occurrence
98
98
  )
99
- add_offense(node, location: offense_location, message: message)
99
+ add_offense(offense_location, message: message)
100
100
  end
101
101
  end
102
102
  end
@@ -45,7 +45,7 @@ module RuboCop
45
45
  # Gem::Specification.new do |spec|
46
46
  # spec.required_ruby_version = '~> 2.5'
47
47
  # end
48
- class RequiredRubyVersion < Cop
48
+ class RequiredRubyVersion < Base
49
49
  include RangeHelp
50
50
 
51
51
  NOT_EQUAL_MSG = '`required_ruby_version` (%<required_ruby_version>s, ' \
@@ -64,7 +64,7 @@ module RuboCop
64
64
  PATTERN
65
65
 
66
66
  # rubocop:disable Metrics/AbcSize
67
- def investigate(processed_source)
67
+ def on_new_investigation
68
68
  version_def = required_ruby_version(processed_source.ast).first
69
69
 
70
70
  if version_def
@@ -72,13 +72,12 @@ module RuboCop
72
72
  return if !ruby_version || ruby_version == target_ruby_version.to_s
73
73
 
74
74
  add_offense(
75
- processed_source.ast,
76
- location: version_def.loc.expression,
75
+ version_def.loc.expression,
77
76
  message: not_equal_message(ruby_version, target_ruby_version)
78
77
  )
79
78
  else
80
79
  range = source_range(processed_source.buffer, 1, 0)
81
- add_offense(nil, location: range, message: MISSING_MSG)
80
+ add_offense(range, message: MISSING_MSG)
82
81
  end
83
82
  end
84
83
  # rubocop:enable Metrics/AbcSize