rubocop 0.72.0 → 0.73.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 (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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eba7ed97b0ff87530eaaac338ed31f332f89dd4c86ca8afc3046d8141d3f1c03
4
- data.tar.gz: 23a2c2e2c59efb26294495c94b33e1849feef60b8afe26e98064ff01e98cbccf
3
+ metadata.gz: 65d141c7453c4d515c8e6a54e88cbc82305193f9fdd078148939550973c06eef
4
+ data.tar.gz: 78246e2ffa87ef9a0206e73227f323bfd9d1fdf97f565660c12f52854d2e75f0
5
5
  SHA512:
6
- metadata.gz: 72a13a6ca2580bc1613ee8cb5a2432f564fb6f53f68558bd716046d4f5c28b73a57535d9eb4c96542f9cbaa5f29ee1226ec71e1b66a342ac4810106b69f3ef5d
7
- data.tar.gz: aa480f20807bf7d31c878eeade72cf09a4963174b7490738f77cc8a8cf5a59b2a40b2f20dbc969719863aed1c672136ef32b7173f5d231c3f387dc25a62f77ac
6
+ metadata.gz: 48f6c20451589a69ebf28b1dacb70193d9baa0b6a5c6a5f2c065c00df2a7df284575705ac65027f4884b0673d1b62babf862ad2c9bdb461f909c4b38cb15a138
7
+ data.tar.gz: ea5877d8c872db2adf5f45e6613d01881227b7a22394c8e661fb41e78b5a9d75fb2662471a1876f287718092120ef2953105f27392c2a741273f9dd131544b29
data/README.md CHANGED
@@ -52,7 +52,7 @@ haven't reached version 1.0 yet). To prevent an unwanted RuboCop update you
52
52
  might want to use a conservative version lock in your `Gemfile`:
53
53
 
54
54
  ```rb
55
- gem 'rubocop', '~> 0.72.0', require: false
55
+ gem 'rubocop', '~> 0.73.0', require: false
56
56
  ```
57
57
 
58
58
  ## Quickstart
@@ -816,6 +816,8 @@ Layout/LeadingCommentSpace:
816
816
  StyleGuide: '#hash-space'
817
817
  Enabled: true
818
818
  VersionAdded: '0.49'
819
+ VersionChanged: '0.73'
820
+ AllowDoxygenCommentStyle: false
819
821
 
820
822
  Layout/MultilineArrayBraceLayout:
821
823
  Description: >-
@@ -2555,6 +2557,11 @@ Style/DocumentationMethod:
2555
2557
  - 'test/**/*'
2556
2558
  RequireForNonPublicMethods: false
2557
2559
 
2560
+ Style/DoubleCopDisableDirective:
2561
+ Description: 'Checks for double rubocop:disable comments on a single line.'
2562
+ Enabled: true
2563
+ VersionAdded: '0.73'
2564
+
2558
2565
  Style/DoubleNegation:
2559
2566
  Description: 'Checks for uses of double negation (!!).'
2560
2567
  StyleGuide: '#no-bang-bang'
@@ -3041,6 +3048,12 @@ Style/MultilineTernaryOperator:
3041
3048
  Enabled: true
3042
3049
  VersionAdded: '0.9'
3043
3050
 
3051
+ Style/MultilineWhenThen:
3052
+ Description: 'Do not use then for multi-line when statement.'
3053
+ StyleGuide: '#no-then'
3054
+ Enabled: true
3055
+ VersionAdded: '0.72'
3056
+
3044
3057
  Style/MultipleComparison:
3045
3058
  Description: >-
3046
3059
  Avoid comparing a variable with multiple items in a conditional,
@@ -414,6 +414,7 @@ require_relative 'rubocop/cop/style/def_with_parentheses'
414
414
  require_relative 'rubocop/cop/style/dir'
415
415
  require_relative 'rubocop/cop/style/documentation_method'
416
416
  require_relative 'rubocop/cop/style/documentation'
417
+ require_relative 'rubocop/cop/style/double_cop_disable_directive'
417
418
  require_relative 'rubocop/cop/style/double_negation'
418
419
  require_relative 'rubocop/cop/style/each_for_simple_loop'
419
420
  require_relative 'rubocop/cop/style/each_with_object'
@@ -466,6 +467,7 @@ require_relative 'rubocop/cop/style/multiline_if_modifier'
466
467
  require_relative 'rubocop/cop/style/multiline_method_signature'
467
468
  require_relative 'rubocop/cop/style/multiline_memoization'
468
469
  require_relative 'rubocop/cop/style/multiline_ternary_operator'
470
+ require_relative 'rubocop/cop/style/multiline_when_then'
469
471
  require_relative 'rubocop/cop/style/multiple_comparison'
470
472
  require_relative 'rubocop/cop/style/mutable_constant'
471
473
  require_relative 'rubocop/cop/style/negated_if'
@@ -302,7 +302,7 @@ module RuboCop
302
302
 
303
303
  ## Destructuring
304
304
 
305
- def_node_matcher :receiver, <<-PATTERN
305
+ def_node_matcher :receiver, <<~PATTERN
306
306
  {(send $_ ...) (block (send $_ ...) ...)}
307
307
  PATTERN
308
308
 
@@ -321,7 +321,7 @@ module RuboCop
321
321
  end
322
322
  end
323
323
 
324
- def_node_matcher :defined_module0, <<-PATTERN
324
+ def_node_matcher :defined_module0, <<~PATTERN
325
325
  {(class (const $_ $_) ...)
326
326
  (module (const $_ $_) ...)
327
327
  (casgn $_ $_ (send (const nil? {:Class :Module}) :new ...))
@@ -367,7 +367,7 @@ module RuboCop
367
367
  end
368
368
 
369
369
  # Some cops treat the shovel operator as a kind of assignment.
370
- def_node_matcher :assignment_or_similar?, <<-PATTERN
370
+ def_node_matcher :assignment_or_similar?, <<~PATTERN
371
371
  {assignment? (send _recv :<< ...)}
372
372
  PATTERN
373
373
 
@@ -479,11 +479,11 @@ module RuboCop
479
479
  irange_type? || erange_type?
480
480
  end
481
481
 
482
- def_node_matcher :guard_clause?, <<-PATTERN
482
+ def_node_matcher :guard_clause?, <<~PATTERN
483
483
  [{(send nil? {:raise :fail} ...) return break next} single_line?]
484
484
  PATTERN
485
485
 
486
- def_node_matcher :proc?, <<-PATTERN
486
+ def_node_matcher :proc?, <<~PATTERN
487
487
  {(block (send nil? :proc) ...)
488
488
  (block (send (const nil? :Proc) :new) ...)
489
489
  (send (const nil? :Proc) :new)}
@@ -492,12 +492,12 @@ module RuboCop
492
492
  def_node_matcher :lambda?, '(block (send nil? :lambda) ...)'
493
493
  def_node_matcher :lambda_or_proc?, '{lambda? proc?}'
494
494
 
495
- def_node_matcher :class_constructor?, <<-PATTERN
495
+ def_node_matcher :class_constructor?, <<~PATTERN
496
496
  { (send (const nil? {:Class :Module}) :new ...)
497
497
  (block (send (const nil? {:Class :Module}) :new ...) ...)}
498
498
  PATTERN
499
499
 
500
- def_node_matcher :module_definition?, <<-PATTERN
500
+ def_node_matcher :module_definition?, <<~PATTERN
501
501
  {class module (casgn _ _ class_constructor?)}
502
502
  PATTERN
503
503
 
@@ -638,7 +638,7 @@ module RuboCop
638
638
  end
639
639
  end
640
640
 
641
- def_node_matcher :new_class_or_module_block?, <<-PATTERN
641
+ def_node_matcher :new_class_or_module_block?, <<~PATTERN
642
642
  ^(casgn _ _ (block (send (const _ {:Class :Module}) :new) ...))
643
643
  PATTERN
644
644
  end
@@ -220,13 +220,24 @@ module RuboCop
220
220
 
221
221
  private
222
222
 
223
- def_node_matcher :macro_scope?, <<-PATTERN
223
+ def_node_matcher :macro_scope?, <<~PATTERN
224
224
  {^{({sclass class module block} ...) class_constructor?}
225
- ^^{({sclass class module block} ... ({begin if} ...)) class_constructor?}
225
+ ^^#ascend_macro_scope?
226
226
  ^#macro_kwbegin_wrapper?
227
227
  #root_node?}
228
228
  PATTERN
229
229
 
230
+ def_node_matcher :wrapped_macro_scope?, <<~PATTERN
231
+ {({sclass class module block} ... ({begin if} ...)) class_constructor?}
232
+ PATTERN
233
+
234
+ def ascend_macro_scope?(ancestor)
235
+ return true if wrapped_macro_scope?(ancestor)
236
+ return false if root_node?(ancestor)
237
+
238
+ ascend_macro_scope?(ancestor.parent)
239
+ end
240
+
230
241
  # Check if a node's parent is a kwbegin wrapper within a macro scope
231
242
  #
232
243
  # @param parent [Node] parent of the node being checked
@@ -245,15 +256,15 @@ module RuboCop
245
256
  node.parent.nil?
246
257
  end
247
258
 
248
- def_node_matcher :adjacent_def_modifier?, <<-PATTERN
259
+ def_node_matcher :adjacent_def_modifier?, <<~PATTERN
249
260
  (send nil? _ ({def defs} ...))
250
261
  PATTERN
251
262
 
252
- def_node_matcher :bare_access_modifier_declaration?, <<-PATTERN
263
+ def_node_matcher :bare_access_modifier_declaration?, <<~PATTERN
253
264
  (send nil? {:public :protected :private :module_function})
254
265
  PATTERN
255
266
 
256
- def_node_matcher :non_bare_access_modifier_declaration?, <<-PATTERN
267
+ def_node_matcher :non_bare_access_modifier_declaration?, <<~PATTERN
257
268
  (send nil? {:public :protected :private :module_function} _)
258
269
  PATTERN
259
270
  end
@@ -35,7 +35,7 @@ module RuboCop
35
35
  end
36
36
 
37
37
  ONE_CHILD_NODE.each do |type|
38
- module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
38
+ module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
39
39
  def on_#{type}(node)
40
40
  if (child = node.children[0])
41
41
  send(:"on_\#{child.type}", child)
@@ -45,7 +45,7 @@ module RuboCop
45
45
  end
46
46
 
47
47
  MANY_CHILD_NODES.each do |type|
48
- module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
48
+ module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
49
49
  def on_#{type}(node)
50
50
  node.children.each { |child| send(:"on_\#{child.type}", child) }
51
51
  nil
@@ -55,7 +55,7 @@ module RuboCop
55
55
 
56
56
  SECOND_CHILD_ONLY.each do |type|
57
57
  # Guard clause is for nodes nested within mlhs
58
- module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
58
+ module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
59
59
  def on_#{type}(node)
60
60
  if (child = node.children[1])
61
61
  send(:"on_\#{child.type}", child)
@@ -5,17 +5,25 @@ module RuboCop
5
5
  # This module encapsulates the logic for autocorrect behavior for a cop.
6
6
  module AutocorrectLogic
7
7
  def autocorrect?
8
- autocorrect_requested? && support_autocorrect? && autocorrect_enabled?
8
+ autocorrect_requested? && correctable? && autocorrect_enabled?
9
9
  end
10
10
 
11
11
  def autocorrect_requested?
12
12
  @options.fetch(:auto_correct, false)
13
13
  end
14
14
 
15
+ def correctable?
16
+ support_autocorrect? || disable_uncorrectable?
17
+ end
18
+
15
19
  def support_autocorrect?
16
20
  respond_to?(:autocorrect)
17
21
  end
18
22
 
23
+ def disable_uncorrectable?
24
+ @options[:disable_uncorrectable] == true
25
+ end
26
+
19
27
  def autocorrect_enabled?
20
28
  # allow turning off autocorrect on a cop by cop basis
21
29
  return true unless cop_config
@@ -28,6 +36,68 @@ module RuboCop
28
36
 
29
37
  true
30
38
  end
39
+
40
+ def disable_offense(node)
41
+ range = node.location.expression
42
+ eol_comment = " # rubocop:disable #{cop_name}"
43
+ needed_line_length = range.column +
44
+ (range.source_line + eol_comment).length
45
+ if needed_line_length <= max_line_length
46
+ disable_offense_at_end_of_line(range_of_first_line(range),
47
+ eol_comment)
48
+ else
49
+ disable_offense_before_and_after(range_by_lines(range))
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def range_of_first_line(range)
56
+ begin_of_first_line = range.begin_pos - range.column
57
+ end_of_first_line = begin_of_first_line + range.source_line.length
58
+
59
+ Parser::Source::Range.new(range.source_buffer,
60
+ begin_of_first_line,
61
+ end_of_first_line)
62
+ end
63
+
64
+ # Expand the given range to include all of any lines it covers. Does not
65
+ # include newline at end of the last line.
66
+ def range_by_lines(range)
67
+ begin_of_first_line = range.begin_pos - range.column
68
+
69
+ last_line = range.source_buffer.source_line(range.last_line)
70
+ last_line_offset = last_line.length - range.last_column
71
+ end_of_last_line = range.end_pos + last_line_offset
72
+
73
+ Parser::Source::Range.new(range.source_buffer,
74
+ begin_of_first_line,
75
+ end_of_last_line)
76
+ end
77
+
78
+ def max_line_length
79
+ config.for_cop('Metrics/LineLength')['Max'] || 80
80
+ end
81
+
82
+ def disable_offense_at_end_of_line(range, eol_comment)
83
+ ->(corrector) { corrector.insert_after(range, eol_comment) }
84
+ end
85
+
86
+ def disable_offense_before_and_after(range_by_lines)
87
+ lambda do |corrector|
88
+ range_with_newline = range_by_lines.resize(range_by_lines.size + 1)
89
+ leading_whitespace = range_by_lines.source_line[/^\s*/]
90
+
91
+ corrector.insert_before(
92
+ range_with_newline,
93
+ "#{leading_whitespace}# rubocop:disable #{cop_name}\n"
94
+ )
95
+ corrector.insert_after(
96
+ range_with_newline,
97
+ "#{leading_whitespace}# rubocop:enable #{cop_name}\n"
98
+ )
99
+ end
100
+ end
31
101
  end
32
102
  end
33
103
  end
@@ -33,7 +33,7 @@ module RuboCop
33
33
  "Please change your source to 'https://rubygems.org' " \
34
34
  "if possible, or 'http://rubygems.org' if not."
35
35
 
36
- def_node_matcher :insecure_protocol_source?, <<-PATTERN
36
+ def_node_matcher :insecure_protocol_source?, <<~PATTERN
37
37
  (send nil? :source
38
38
  (sym ${:gemcutter :rubygems :rubyforge}))
39
39
  PATTERN
@@ -64,7 +64,7 @@ module RuboCop
64
64
  declarations.to_a[node_index - 1]
65
65
  end
66
66
 
67
- def_node_search :gem_declarations, <<-PATTERN
67
+ def_node_search :gem_declarations, <<~PATTERN
68
68
  (:send nil? :gem str ...)
69
69
  PATTERN
70
70
  end
@@ -7,8 +7,6 @@ module RuboCop
7
7
  class Commissioner
8
8
  include RuboCop::AST::Traversal
9
9
 
10
- CopError = Struct.new(:error, :line, :column)
11
-
12
10
  attr_reader :errors
13
11
 
14
12
  def initialize(cops, forces = [], options = {})
@@ -62,7 +60,7 @@ module RuboCop
62
60
  end
63
61
 
64
62
  def reset_errors
65
- @errors = Hash.new { |hash, k| hash[k] = [] }
63
+ @errors = []
66
64
  end
67
65
 
68
66
  def remove_irrelevant_cops(filename)
@@ -131,12 +129,8 @@ module RuboCop
131
129
  rescue StandardError => e
132
130
  raise e if @options[:raise_error]
133
131
 
134
- if node
135
- line = node.first_line
136
- column = node.loc.column
137
- end
138
- error = CopError.new(e, line, column)
139
- @errors[cop] << error
132
+ err = ErrorWithAnalyzedFileLocation.new(cause: e, node: node, cop: cop)
133
+ @errors << err
140
134
  end
141
135
  end
142
136
  end
@@ -31,6 +31,16 @@ module RuboCop
31
31
  include IgnoredNode
32
32
  include AutocorrectLogic
33
33
 
34
+ Correction = Struct.new(:lambda, :node, :cop) do
35
+ def call(corrector)
36
+ lambda.call(corrector)
37
+ rescue StandardError => e
38
+ raise ErrorWithAnalyzedFileLocation.new(
39
+ cause: e, node: node, cop: cop
40
+ )
41
+ end
42
+ end
43
+
34
44
  attr_reader :config, :offenses, :corrections
35
45
  attr_accessor :processed_source # TODO: Bad design.
36
46
 
@@ -141,18 +151,38 @@ module RuboCop
141
151
  end
142
152
 
143
153
  def correct(node)
144
- return :unsupported unless support_autocorrect?
145
- return :uncorrected unless autocorrect?
146
- return :already_corrected if @corrected_nodes.key?(node)
154
+ reason = reason_to_not_correct(node)
155
+ return reason if reason
147
156
 
148
157
  @corrected_nodes[node] = true
149
- correction = autocorrect(node)
150
- return :uncorrected unless correction
158
+ if support_autocorrect?
159
+ correction = autocorrect(node)
160
+ return :uncorrected unless correction
151
161
 
152
- @corrections << correction
162
+ @corrections << Correction.new(correction, node, self)
163
+ elsif disable_uncorrectable?
164
+ disable_uncorrectable(node)
165
+ end
153
166
  :corrected
154
167
  end
155
168
 
169
+ def reason_to_not_correct(node)
170
+ return :unsupported unless correctable?
171
+ return :uncorrected unless autocorrect?
172
+ return :already_corrected if @corrected_nodes.key?(node)
173
+
174
+ nil
175
+ end
176
+
177
+ def disable_uncorrectable(node)
178
+ @disabled_lines ||= {}
179
+ line = node.location.line
180
+ return if @disabled_lines.key?(line)
181
+
182
+ @disabled_lines[line] = true
183
+ @corrections << Correction.new(disable_offense(node), node, self)
184
+ end
185
+
156
186
  def config_to_allow_offenses
157
187
  Formatter::DisabledConfigFormatter
158
188
  .config_to_allow_offenses[cop_name] ||= {}
@@ -57,16 +57,15 @@ module RuboCop
57
57
  #
58
58
  # @return [String]
59
59
  def rewrite
60
- # rubocop:disable Lint/HandleExceptions
61
60
  @corrections.each do |correction|
62
61
  begin
63
62
  @source_rewriter.transaction do
64
63
  correction.call(self)
65
64
  end
66
- rescue ::Parser::ClobberingError
65
+ rescue ErrorWithAnalyzedFileLocation => e
66
+ raise e unless e.cause.is_a?(::Parser::ClobberingError)
67
67
  end
68
68
  end
69
- # rubocop:enable Lint/HandleExceptions
70
69
 
71
70
  @source_rewriter.process
72
71
  end
@@ -9,9 +9,9 @@ module RuboCop
9
9
  offense_style, range = node
10
10
  lambda do |corrector|
11
11
  case offense_style
12
- when :no_empty_lines then
12
+ when :no_empty_lines
13
13
  corrector.remove(range)
14
- when :empty_lines then
14
+ when :empty_lines
15
15
  corrector.insert_before(range, "\n")
16
16
  end
17
17
  end