rubocop 0.72.0 → 0.73.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +13 -0
  4. data/lib/rubocop.rb +2 -0
  5. data/lib/rubocop/ast/node.rb +8 -8
  6. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +16 -5
  7. data/lib/rubocop/ast/traversal.rb +3 -3
  8. data/lib/rubocop/cop/autocorrect_logic.rb +71 -1
  9. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
  10. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  11. data/lib/rubocop/cop/commissioner.rb +3 -9
  12. data/lib/rubocop/cop/cop.rb +36 -6
  13. data/lib/rubocop/cop/corrector.rb +2 -3
  14. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +2 -2
  15. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
  16. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  17. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
  18. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  19. data/lib/rubocop/cop/generator.rb +1 -1
  20. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -2
  21. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
  22. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -2
  23. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  24. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +2 -2
  25. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -2
  26. data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
  27. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  28. data/lib/rubocop/cop/layout/indent_first_argument.rb +1 -1
  29. data/lib/rubocop/cop/layout/indent_heredoc.rb +3 -2
  30. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  31. data/lib/rubocop/cop/layout/leading_comment_space.rb +28 -0
  32. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +18 -4
  33. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +14 -2
  34. data/lib/rubocop/cop/layout/tab.rb +10 -22
  35. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -1
  36. data/lib/rubocop/cop/lint/debugger.rb +3 -3
  37. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -3
  38. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  39. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  40. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  41. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  42. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  43. data/lib/rubocop/cop/lint/multiple_compare.rb +1 -1
  44. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -3
  45. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  46. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +1 -1
  47. data/lib/rubocop/cop/lint/number_conversion.rb +2 -2
  48. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  49. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  50. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
  51. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -5
  52. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  53. data/lib/rubocop/cop/lint/unified_integer.rb +1 -1
  54. data/lib/rubocop/cop/lint/unneeded_require_statement.rb +1 -1
  55. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  56. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  57. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -1
  58. data/lib/rubocop/cop/lint/uri_regexp.rb +2 -2
  59. data/lib/rubocop/cop/lint/useless_access_modifier.rb +6 -6
  60. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  61. data/lib/rubocop/cop/metrics/class_length.rb +1 -1
  62. data/lib/rubocop/cop/metrics/module_length.rb +1 -1
  63. data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -1
  64. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  65. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -1
  66. data/lib/rubocop/cop/mixin/enforce_superclass.rb +4 -4
  67. data/lib/rubocop/cop/mixin/method_complexity.rb +1 -1
  68. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -3
  69. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  70. data/lib/rubocop/cop/naming/constant_name.rb +2 -2
  71. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  72. data/lib/rubocop/cop/security/eval.rb +1 -1
  73. data/lib/rubocop/cop/security/json_load.rb +1 -1
  74. data/lib/rubocop/cop/security/marshal_load.rb +1 -1
  75. data/lib/rubocop/cop/security/open.rb +1 -1
  76. data/lib/rubocop/cop/security/yaml_load.rb +1 -1
  77. data/lib/rubocop/cop/style/alias.rb +1 -1
  78. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  79. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  80. data/lib/rubocop/cop/style/constant_visibility.rb +1 -1
  81. data/lib/rubocop/cop/style/date_time.rb +3 -3
  82. data/lib/rubocop/cop/style/dir.rb +1 -1
  83. data/lib/rubocop/cop/style/documentation_method.rb +1 -1
  84. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +49 -0
  85. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  86. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  87. data/lib/rubocop/cop/style/eval_with_location.rb +2 -2
  88. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  89. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  90. data/lib/rubocop/cop/style/float_division.rb +4 -4
  91. data/lib/rubocop/cop/style/format_string.rb +7 -7
  92. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  93. data/lib/rubocop/cop/style/inverse_methods.rb +2 -2
  94. data/lib/rubocop/cop/style/min_max.rb +1 -1
  95. data/lib/rubocop/cop/style/mixin_usage.rb +1 -1
  96. data/lib/rubocop/cop/style/multiline_when_then.rb +55 -0
  97. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  98. data/lib/rubocop/cop/style/mutable_constant.rb +3 -3
  99. data/lib/rubocop/cop/style/numeric_predicate.rb +3 -3
  100. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  101. data/lib/rubocop/cop/style/or_assignment.rb +2 -2
  102. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -1
  103. data/lib/rubocop/cop/style/random_with_offset.rb +6 -6
  104. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -2
  105. data/lib/rubocop/cop/style/redundant_exception.rb +2 -2
  106. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  107. data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -2
  108. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -1
  109. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
  110. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  111. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  112. data/lib/rubocop/cop/style/sample.rb +1 -1
  113. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  114. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -2
  115. data/lib/rubocop/cop/style/strip.rb +1 -1
  116. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -3
  117. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  118. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  119. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  120. data/lib/rubocop/cop/style/unneeded_sort.rb +1 -1
  121. data/lib/rubocop/cop/style/unpack_first.rb +1 -1
  122. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -5
  123. data/lib/rubocop/cop/team.rb +15 -14
  124. data/lib/rubocop/error.rb +23 -0
  125. data/lib/rubocop/node_pattern.rb +2 -2
  126. data/lib/rubocop/options.rb +17 -0
  127. data/lib/rubocop/rspec/shared_contexts.rb +12 -0
  128. data/lib/rubocop/target_finder.rb +6 -4
  129. data/lib/rubocop/version.rb +1 -1
  130. metadata +4 -2
@@ -60,7 +60,7 @@ module RuboCop
60
60
 
61
61
  private
62
62
 
63
- def_node_matcher :control_op_condition, <<-PATTERN
63
+ def_node_matcher :control_op_condition, <<~PATTERN
64
64
  (begin $_ ...)
65
65
  PATTERN
66
66
 
@@ -27,7 +27,7 @@ module RuboCop
27
27
  MSG = 'Prefer ranges when generating random numbers instead of ' \
28
28
  'integers with offsets.'
29
29
 
30
- def_node_matcher :integer_op_rand?, <<-PATTERN
30
+ def_node_matcher :integer_op_rand?, <<~PATTERN
31
31
  (send
32
32
  int {:+ :-}
33
33
  (send
@@ -36,7 +36,7 @@ module RuboCop
36
36
  {int irange erange}))
37
37
  PATTERN
38
38
 
39
- def_node_matcher :rand_op_integer?, <<-PATTERN
39
+ def_node_matcher :rand_op_integer?, <<~PATTERN
40
40
  (send
41
41
  (send
42
42
  {nil? (const nil? :Random) (const nil? :Kernel)}
@@ -46,7 +46,7 @@ module RuboCop
46
46
  int)
47
47
  PATTERN
48
48
 
49
- def_node_matcher :rand_modified?, <<-PATTERN
49
+ def_node_matcher :rand_modified?, <<~PATTERN
50
50
  (send
51
51
  (send
52
52
  {nil? (const nil? :Random) (const nil? :Kernel)}
@@ -80,7 +80,7 @@ module RuboCop
80
80
 
81
81
  private
82
82
 
83
- def_node_matcher :random_call, <<-PATTERN
83
+ def_node_matcher :random_call, <<~PATTERN
84
84
  {(send (send $_ _ $_) ...)
85
85
  (send _ _ (send $_ _ $_))}
86
86
  PATTERN
@@ -128,7 +128,7 @@ module RuboCop
128
128
  end
129
129
  end
130
130
 
131
- def_node_matcher :namespace, <<-PATTERN
131
+ def_node_matcher :namespace, <<~PATTERN
132
132
  {$nil? (const nil? $_)}
133
133
  PATTERN
134
134
 
@@ -149,7 +149,7 @@ module RuboCop
149
149
  end
150
150
  end
151
151
 
152
- def_node_matcher :to_int, <<-PATTERN
152
+ def_node_matcher :to_int, <<~PATTERN
153
153
  (int $_)
154
154
  PATTERN
155
155
  end
@@ -53,11 +53,11 @@ module RuboCop
53
53
  format(MSG, msg: msg)
54
54
  end
55
55
 
56
- def_node_matcher :redundant_condition?, <<-RUBY
56
+ def_node_matcher :redundant_condition?, <<~RUBY
57
57
  (if (send _ {:#{COMPARISON_OPERATORS.join(' :')}} _) true false)
58
58
  RUBY
59
59
 
60
- def_node_matcher :redundant_condition_inverted?, <<-RUBY
60
+ def_node_matcher :redundant_condition_inverted?, <<~RUBY
61
61
  (if (send _ {:#{COMPARISON_OPERATORS.join(' :')}} _) false true)
62
62
  RUBY
63
63
 
@@ -47,11 +47,11 @@ module RuboCop
47
47
  end
48
48
  end
49
49
 
50
- def_node_matcher :exploded?, <<-PATTERN
50
+ def_node_matcher :exploded?, <<~PATTERN
51
51
  (send nil? ${:raise :fail} (const nil? :RuntimeError) $_)
52
52
  PATTERN
53
53
 
54
- def_node_matcher :compact?, <<-PATTERN
54
+ def_node_matcher :compact?, <<~PATTERN
55
55
  (send nil? {:raise :fail} $(send (const nil? :RuntimeError) :new $_))
56
56
  PATTERN
57
57
  end
@@ -51,7 +51,7 @@ module RuboCop
51
51
  end
52
52
  end
53
53
 
54
- def_node_matcher :operation_produces_immutable_object?, <<-PATTERN
54
+ def_node_matcher :operation_produces_immutable_object?, <<~PATTERN
55
55
  {
56
56
  (begin (send {float int} {:+ :- :* :** :/ :% :<<} _))
57
57
  (begin (send !(str _) {:+ :- :* :** :/ :%} {float int}))
@@ -203,11 +203,11 @@ module RuboCop
203
203
  first_send_argument?(node) || first_super_argument?(node)
204
204
  end
205
205
 
206
- def_node_matcher :first_send_argument?, <<-PATTERN
206
+ def_node_matcher :first_send_argument?, <<~PATTERN
207
207
  ^(send _ _ equal?(%0) ...)
208
208
  PATTERN
209
209
 
210
- def_node_matcher :first_super_argument?, <<-PATTERN
210
+ def_node_matcher :first_super_argument?, <<~PATTERN
211
211
  ^(super equal?(%0) ...)
212
212
  PATTERN
213
213
 
@@ -20,7 +20,7 @@ module RuboCop
20
20
 
21
21
  MSG = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'
22
22
 
23
- def_node_matcher :redundant_sort_by, <<-PATTERN
23
+ def_node_matcher :redundant_sort_by, <<~PATTERN
24
24
  (block $(send _ :sort_by) (args (arg $_x)) (lvar _x))
25
25
  PATTERN
26
26
 
@@ -80,11 +80,11 @@ module RuboCop
80
80
  MSG_EXPLICIT = 'Avoid rescuing without specifying ' \
81
81
  'an error class.'
82
82
 
83
- def_node_matcher :rescue_without_error_class?, <<-PATTERN
83
+ def_node_matcher :rescue_without_error_class?, <<~PATTERN
84
84
  (resbody nil? _ _)
85
85
  PATTERN
86
86
 
87
- def_node_matcher :rescue_standard_error?, <<-PATTERN
87
+ def_node_matcher :rescue_standard_error?, <<~PATTERN
88
88
  (resbody $(array (const nil? :StandardError)) _ _)
89
89
  PATTERN
90
90
 
@@ -80,7 +80,7 @@ module RuboCop
80
80
  end
81
81
 
82
82
  def_node_matcher :chained_send?, '(send !nil? ...)'
83
- def_node_matcher :define_method?, <<-PATTERN
83
+ def_node_matcher :define_method?, <<~PATTERN
84
84
  (send _ {:define_method :define_singleton_method} _)
85
85
  PATTERN
86
86
  end
@@ -69,7 +69,7 @@ module RuboCop
69
69
 
70
70
  # if format: (if checked_variable body nil)
71
71
  # unless format: (if checked_variable nil body)
72
- def_node_matcher :modifier_if_safe_navigation_candidate, <<-PATTERN
72
+ def_node_matcher :modifier_if_safe_navigation_candidate, <<~PATTERN
73
73
  {
74
74
  (if {
75
75
  (send $_ {:nil? :!})
@@ -30,7 +30,7 @@ module RuboCop
30
30
  class Sample < Cop
31
31
  MSG = 'Use `%<correct>s` instead of `%<incorrect>s`.'
32
32
 
33
- def_node_matcher :sample_candidate?, <<-PATTERN
33
+ def_node_matcher :sample_candidate?, <<~PATTERN
34
34
  (send $(send _ :shuffle $...) ${:first :last :[] :at :slice} $...)
35
35
  PATTERN
36
36
 
@@ -20,7 +20,7 @@ module RuboCop
20
20
  MSG =
21
21
  'Use `warn` instead of `%<bad>s` to allow such output to be disabled.'
22
22
 
23
- def_node_matcher :stderr_puts?, <<-PATTERN
23
+ def_node_matcher :stderr_puts?, <<~PATTERN
24
24
  (send
25
25
  {(gvar #stderr_gvar?) (const nil? :STDERR)}
26
26
  :puts $_
@@ -15,11 +15,11 @@ module RuboCop
15
15
  class StringHashKeys < Cop
16
16
  MSG = 'Prefer symbols instead of strings as hash keys.'
17
17
 
18
- def_node_matcher :string_hash_key?, <<-PATTERN
18
+ def_node_matcher :string_hash_key?, <<~PATTERN
19
19
  (pair (str _) _)
20
20
  PATTERN
21
21
 
22
- def_node_matcher :receive_environments_method?, <<-PATTERN
22
+ def_node_matcher :receive_environments_method?, <<~PATTERN
23
23
  {
24
24
  ^^(send (const {nil? cbase} :IO) :popen ...)
25
25
  ^^(send (const {nil? cbase} :Open3)
@@ -18,7 +18,7 @@ module RuboCop
18
18
 
19
19
  MSG = 'Use `strip` instead of `%<methods>s`.'
20
20
 
21
- def_node_matcher :lstrip_rstrip, <<-PATTERN
21
+ def_node_matcher :lstrip_rstrip, <<~PATTERN
22
22
  {(send $(send _ $:rstrip) $:lstrip)
23
23
  (send $(send _ $:lstrip) $:rstrip)}
24
24
  PATTERN
@@ -29,9 +29,9 @@ module RuboCop
29
29
  add_offense(node, location: node.parent_class.source_range)
30
30
  end
31
31
 
32
- def_node_matcher :struct_constructor?, <<-PATTERN
33
- {(send (const nil? :Struct) :new ...)
34
- (block (send (const nil? :Struct) :new ...) ...)}
32
+ def_node_matcher :struct_constructor?, <<~PATTERN
33
+ {(send (const nil? :Struct) :new ...)
34
+ (block (send (const nil? :Struct) :new ...) ...)}
35
35
  PATTERN
36
36
  end
37
37
  end
@@ -20,7 +20,7 @@ module RuboCop
20
20
  SUPER_TYPES = %i[super zsuper].freeze
21
21
 
22
22
  def_node_matcher :proc_node?, '(send (const nil? :Proc) :new)'
23
- def_node_matcher :symbol_proc?, <<-PATTERN
23
+ def_node_matcher :symbol_proc?, <<~PATTERN
24
24
  (block
25
25
  ${(send ...) (super ...) zsuper}
26
26
  $(args (arg _var))
@@ -167,7 +167,7 @@ module RuboCop
167
167
  (child.send_type? && child.prefix_not?)
168
168
  end
169
169
 
170
- def_node_matcher :method_name, <<-PATTERN
170
+ def_node_matcher :method_name, <<~PATTERN
171
171
  {($:defined? (send nil? _) ...)
172
172
  (send {_ nil?} $_ _ ...)}
173
173
  PATTERN
@@ -118,7 +118,7 @@ module RuboCop
118
118
  !allowed_method?(node) && !allowed_writer?(node.method_name)
119
119
  end
120
120
 
121
- def_node_matcher :looks_like_trivial_writer?, <<-PATTERN
121
+ def_node_matcher :looks_like_trivial_writer?, <<~PATTERN
122
122
  {(def _ (args (arg ...)) (ivasgn _ (lvar _)))
123
123
  (defs _ _ (args (arg ...)) (ivasgn _ (lvar _)))}
124
124
  PATTERN
@@ -55,7 +55,7 @@ module RuboCop
55
55
  MSG = 'Use `%<suggestion>s` instead of '\
56
56
  '`%<sorter>s...%<accessor_source>s`.'
57
57
 
58
- def_node_matcher :unneeded_sort?, <<-MATCHER
58
+ def_node_matcher :unneeded_sort?, <<~MATCHER
59
59
  {
60
60
  (send $(send _ $:sort ...) ${:last :first})
61
61
  (send $(send _ $:sort ...) ${:[] :at :slice} {(int 0) (int -1)})
@@ -25,7 +25,7 @@ module RuboCop
25
25
  MSG = 'Use `%<receiver>s.unpack1(%<format>s)` instead of '\
26
26
  '`%<receiver>s.unpack(%<format>s)%<method>s`.'
27
27
 
28
- def_node_matcher :unpack_and_first_element?, <<-PATTERN
28
+ def_node_matcher :unpack_and_first_element?, <<~PATTERN
29
29
  {
30
30
  (send $(send (...) :unpack $(...)) :first)
31
31
  (send $(send (...) :unpack $(...)) {:[] :slice :at} (int 0))
@@ -73,14 +73,14 @@ module RuboCop
73
73
  )
74
74
  end
75
75
 
76
- def_node_matcher :zero_length_predicate, <<-PATTERN
76
+ def_node_matcher :zero_length_predicate, <<~PATTERN
77
77
  {(send (send (...) ${:length :size}) $:== (int $0))
78
78
  (send (int $0) $:== (send (...) ${:length :size}))
79
79
  (send (send (...) ${:length :size}) $:< (int $1))
80
80
  (send (int $1) $:> (send (...) ${:length :size}))}
81
81
  PATTERN
82
82
 
83
- def_node_matcher :nonzero_length_predicate, <<-PATTERN
83
+ def_node_matcher :nonzero_length_predicate, <<~PATTERN
84
84
  {(send (send (...) ${:length :size}) ${:> :!=} (int $0))
85
85
  (send (int $0) ${:< :!=} (send (...) ${:length :size}))}
86
86
  PATTERN
@@ -92,14 +92,14 @@ module RuboCop
92
92
  "!#{other_receiver(node).source}.empty?"
93
93
  end
94
94
 
95
- def_node_matcher :zero_length_receiver, <<-PATTERN
95
+ def_node_matcher :zero_length_receiver, <<~PATTERN
96
96
  {(send (send $_ _) :== (int 0))
97
97
  (send (int 0) :== (send $_ _))
98
98
  (send (send $_ _) :< (int 1))
99
99
  (send (int 1) :> (send $_ _))}
100
100
  PATTERN
101
101
 
102
- def_node_matcher :other_receiver, <<-PATTERN
102
+ def_node_matcher :other_receiver, <<~PATTERN
103
103
  {(send (send $_ _) _ _)
104
104
  (send _ _ (send $_ _))}
105
105
  PATTERN
@@ -107,7 +107,7 @@ module RuboCop
107
107
  # Some collection like objects in the Ruby standard library
108
108
  # implement `#size`, but not `#empty`. We ignore those to
109
109
  # reduce false positives.
110
- def_node_matcher :non_polymorphic_collection?, <<-PATTERN
110
+ def_node_matcher :non_polymorphic_collection?, <<~PATTERN
111
111
  {(send (send (send (const nil? :File) :stat _) ...) ...)
112
112
  (send (send (send (const nil? {:Tempfile :StringIO}) {:new :open} ...) ...) ...)}
113
113
  PATTERN
@@ -81,6 +81,9 @@ module RuboCop
81
81
  File.open(filename, 'w') { |f| f.write(new_source) }
82
82
  end
83
83
  @updated_source_file = true
84
+ rescue RuboCop::ErrorWithAnalyzedFileLocation => e
85
+ process_errors(buffer.name, [e])
86
+ raise e.cause
84
87
  end
85
88
 
86
89
  private
@@ -103,8 +106,8 @@ module RuboCop
103
106
 
104
107
  other = investigate(other_cops, processed_source)
105
108
 
106
- errors = autocorrect.errors.merge(other.errors)
107
- process_commissioner_errors(processed_source.path, errors)
109
+ errors = [*autocorrect.errors, *other.errors]
110
+ process_errors(processed_source.path, errors)
108
111
 
109
112
  autocorrect.offenses.concat(other.offenses)
110
113
  end
@@ -149,19 +152,17 @@ module RuboCop
149
152
  end
150
153
  end
151
154
 
152
- def process_commissioner_errors(file, file_errors)
153
- file_errors.each do |cop, errors|
154
- errors.each do |cop_error|
155
- e = cop_error.error
156
- line = ":#{cop_error.line}" if cop_error.line
157
- column = ":#{cop_error.column}" if cop_error.column
158
- location = "#{file}#{line}#{column}"
155
+ def process_errors(file, errors)
156
+ errors.each do |error|
157
+ line = ":#{error.line}" if error.line
158
+ column = ":#{error.column}" if error.column
159
+ location = "#{file}#{line}#{column}"
160
+ cause = error.cause
159
161
 
160
- if e.is_a?(Warning)
161
- handle_warning(e, location)
162
- else
163
- handle_error(e, location, cop)
164
- end
162
+ if cause.is_a?(Warning)
163
+ handle_warning(cause, location)
164
+ else
165
+ handle_error(cause, location, error.cop)
165
166
  end
166
167
  end
167
168
  end
@@ -8,4 +8,27 @@ module RuboCop
8
8
  class Error < StandardError; end
9
9
 
10
10
  class ValidationError < Error; end
11
+
12
+ # A wrapper to display errored location of analyzed file.
13
+ class ErrorWithAnalyzedFileLocation < Error
14
+ def initialize(cause:, node:, cop:)
15
+ @cause = cause
16
+ @cop = cop
17
+ @location = node.is_a?(RuboCop::AST::Node) ? node.loc : node
18
+ end
19
+
20
+ attr_reader :cause, :cop
21
+
22
+ def line
23
+ @location&.line
24
+ end
25
+
26
+ def column
27
+ @location&.column
28
+ end
29
+
30
+ def message
31
+ "cause: #{cause.inspect}"
32
+ end
33
+ end
11
34
  end
@@ -612,7 +612,7 @@ module RuboCop
612
612
  end
613
613
 
614
614
  def emit_method_code
615
- <<-RUBY
615
+ <<~RUBY
616
616
  return unless #{@match_code}
617
617
  block_given? ? #{emit_yield_capture} : (return #{emit_retval})
618
618
  RUBY
@@ -746,7 +746,7 @@ module RuboCop
746
746
 
747
747
  def node_search_body(method_name, trailing_params, prelude, match_code,
748
748
  on_match)
749
- <<-RUBY
749
+ <<~RUBY
750
750
  def #{method_name}(node0#{trailing_params})
751
751
  #{prelude}
752
752
  node0.each_node do |node|
@@ -109,6 +109,8 @@ module RuboCop
109
109
  @validator.validate_exclude_limit_option
110
110
  end
111
111
 
112
+ option(opts, '--disable-uncorrectable')
113
+
112
114
  option(opts, '--no-offense-counts') do
113
115
  @options[:no_offense_counts] = true
114
116
  end
@@ -253,6 +255,7 @@ module RuboCop
253
255
  @options = options
254
256
  end
255
257
 
258
+ # rubocop:disable Metrics/AbcSize
256
259
  def validate_compatibility # rubocop:disable Metrics/MethodLength
257
260
  if only_includes_unneeded_disable?
258
261
  raise OptionArgumentError, 'Lint/UnneededCopDisableDirective can not ' \
@@ -270,6 +273,7 @@ module RuboCop
270
273
  '--display-only-fail-level-offenses'
271
274
  end
272
275
  validate_auto_gen_config
276
+ validate_auto_correct
273
277
  validate_parallel
274
278
 
275
279
  return if incompatible_options.size <= 1
@@ -277,6 +281,7 @@ module RuboCop
277
281
  raise OptionArgumentError, 'Incompatible cli options: ' \
278
282
  "#{incompatible_options.inspect}"
279
283
  end
284
+ # rubocop:enable Metrics/AbcSize
280
285
 
281
286
  def validate_auto_gen_config
282
287
  return if @options.key?(:auto_gen_config)
@@ -292,6 +297,15 @@ module RuboCop
292
297
  end
293
298
  end
294
299
 
300
+ def validate_auto_correct
301
+ return if @options.key?(:auto_correct)
302
+ return unless @options.key?(:disable_uncorrectable)
303
+
304
+ raise OptionArgumentError,
305
+ format('--%<flag>s can only be used together with --auto-correct.',
306
+ flag: '--disable-uncorrectable')
307
+ end
308
+
295
309
  def validate_parallel
296
310
  return unless @options.key?(:parallel)
297
311
 
@@ -378,6 +392,9 @@ module RuboCop
378
392
  exclude_limit: ['Used together with --auto-gen-config to',
379
393
  'set the limit for how many Exclude',
380
394
  "properties to generate. Default is #{MAX_EXCL}."],
395
+ disable_uncorrectable: ['Used with --auto-correct to annotate any',
396
+ 'offenses that do not support autocorrect',
397
+ 'with `rubocop:disable` comments.'],
381
398
  force_exclusion: ['Force excluding files specified in the',
382
399
  'configuration `Exclude` even if they are',
383
400
  'explicitly passed as arguments.'],