rubocop 0.81.0 → 0.82.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 (142) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +59 -13
  4. data/lib/rubocop.rb +4 -2
  5. data/lib/rubocop/ast/node.rb +1 -1
  6. data/lib/rubocop/ast/node/array_node.rb +13 -0
  7. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +8 -0
  8. data/lib/rubocop/cli.rb +10 -4
  9. data/lib/rubocop/config.rb +9 -1
  10. data/lib/rubocop/config_loader.rb +24 -15
  11. data/lib/rubocop/config_loader_resolver.rb +1 -1
  12. data/lib/rubocop/config_obsoletion.rb +1 -0
  13. data/lib/rubocop/config_validator.rb +16 -0
  14. data/lib/rubocop/cop/badge.rb +5 -5
  15. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
  16. data/lib/rubocop/cop/corrector.rb +48 -24
  17. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  18. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
  19. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  20. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
  21. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  22. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  23. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
  24. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
  25. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  26. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  27. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -10
  28. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  29. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  30. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  31. data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
  32. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  33. data/lib/rubocop/cop/layout/line_length.rb +2 -2
  34. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  35. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
  36. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
  37. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  38. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  39. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
  40. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
  41. data/lib/rubocop/cop/lint/boolean_symbol.rb +2 -2
  42. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  43. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  44. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  45. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  46. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  47. data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
  48. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  49. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  50. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
  51. data/lib/rubocop/cop/lint/raise_exception.rb +42 -6
  52. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  53. data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
  54. data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
  55. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  56. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  57. data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -3
  58. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  59. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  60. data/lib/rubocop/cop/naming/constant_name.rb +2 -1
  61. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  62. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  63. data/lib/rubocop/cop/registry.rb +11 -4
  64. data/lib/rubocop/cop/style/alias.rb +4 -4
  65. data/lib/rubocop/cop/style/and_or.rb +5 -6
  66. data/lib/rubocop/cop/style/array_join.rb +1 -1
  67. data/lib/rubocop/cop/style/case_equality.rb +24 -1
  68. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  69. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
  70. data/lib/rubocop/cop/style/copyright.rb +1 -1
  71. data/lib/rubocop/cop/style/dir.rb +1 -1
  72. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
  73. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
  74. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  75. data/lib/rubocop/cop/style/each_with_object.rb +3 -3
  76. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  77. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  78. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  79. data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
  80. data/lib/rubocop/cop/style/format_string.rb +2 -2
  81. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -3
  82. data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
  83. data/lib/rubocop/cop/style/hash_transform_values.rb +0 -3
  84. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
  85. data/lib/rubocop/cop/style/lambda.rb +2 -2
  86. data/lib/rubocop/cop/style/lambda_call.rb +2 -2
  87. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  88. data/lib/rubocop/cop/style/module_function.rb +2 -2
  89. data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
  90. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  91. data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
  92. data/lib/rubocop/cop/style/next.rb +2 -2
  93. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  94. data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
  95. data/lib/rubocop/cop/style/not.rb +1 -1
  96. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  97. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  98. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  99. data/lib/rubocop/cop/style/one_line_conditional.rb +1 -1
  100. data/lib/rubocop/cop/style/or_assignment.rb +1 -1
  101. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  102. data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
  103. data/lib/rubocop/cop/style/proc.rb +1 -1
  104. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  105. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  106. data/lib/rubocop/cop/style/redundant_condition.rb +3 -4
  107. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  108. data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
  109. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  110. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  111. data/lib/rubocop/cop/style/redundant_return.rb +5 -7
  112. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  113. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  114. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  115. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  116. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  117. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  118. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
  119. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
  120. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  121. data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
  122. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -2
  123. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +2 -1
  124. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +3 -0
  125. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  126. data/lib/rubocop/cop/style/unpack_first.rb +0 -4
  127. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  128. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  129. data/lib/rubocop/cop/style/word_array.rb +1 -1
  130. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
  131. data/lib/rubocop/formatter/formatter_set.rb +0 -1
  132. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  133. data/lib/rubocop/options.rb +7 -1
  134. data/lib/rubocop/processed_source.rb +0 -3
  135. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  136. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  137. data/lib/rubocop/rspec/shared_contexts.rb +0 -4
  138. data/lib/rubocop/runner.rb +1 -1
  139. data/lib/rubocop/target_ruby.rb +2 -2
  140. data/lib/rubocop/version.rb +1 -1
  141. metadata +7 -5
  142. data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
@@ -21,7 +21,7 @@ module RuboCop
21
21
  def_node_matcher :uri_regexp_with_argument?, <<~PATTERN
22
22
  (send
23
23
  (const ${nil? cbase} :URI) :regexp
24
- (str $_))
24
+ ${(str _) (array ...)})
25
25
  PATTERN
26
26
 
27
27
  def_node_matcher :uri_regexp_without_argument?, <<~PATTERN
@@ -32,7 +32,7 @@ module RuboCop
32
32
  def on_send(node)
33
33
  uri_regexp_with_argument?(node) do |double_colon, arg|
34
34
  register_offense(
35
- node, top_level: double_colon ? '::' : '', arg: "('#{arg}')"
35
+ node, top_level: double_colon ? '::' : '', arg: "(#{arg.source})"
36
36
  )
37
37
  end
38
38
 
@@ -51,10 +51,10 @@ module RuboCop
51
51
  double_colon, arg = captured_values
52
52
 
53
53
  top_level = double_colon ? '::' : ''
54
- argument = arg ? "('#{arg}')" : ''
54
+ argument = arg ? "(#{arg.source})" : ''
55
55
 
56
56
  corrector.replace(
57
- node.loc.expression,
57
+ node,
58
58
  "#{top_level}URI::DEFAULT_PARSER.make_regexp#{argument}"
59
59
  )
60
60
  end
@@ -161,7 +161,7 @@ module RuboCop
161
161
 
162
162
  def set_new_body_expression(transforming_body_expr, corrector)
163
163
  corrector.replace(
164
- block_node.body.loc.expression,
164
+ block_node.body,
165
165
  transforming_body_expr.loc.expression.source
166
166
  )
167
167
  end
@@ -63,7 +63,7 @@ module RuboCop
63
63
  end
64
64
 
65
65
  def tab_indentation_width
66
- config.for_cop('Layout/Tab')['IndentationWidth']
66
+ config.for_cop('Layout/IndentationStyle')['IndentationWidth']
67
67
  end
68
68
 
69
69
  def uri_regexp
@@ -58,12 +58,13 @@ module RuboCop
58
58
  end
59
59
 
60
60
  def indentation_multiplier
61
- return 1 if config.for_cop('Layout/Tab')['Enabled']
61
+ return 1 if config.for_cop('Layout/IndentationStyle')['Enabled']
62
62
 
63
63
  default_configuration = RuboCop::ConfigLoader.default_configuration
64
- config.for_cop('Layout/Tab')['IndentationWidth'] ||
64
+ config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
65
65
  config.for_cop('Layout/IndentationWidth')['Width'] ||
66
- default_configuration.for_cop('Layout/Tab')['IndentationWidth'] ||
66
+ default_configuration
67
+ .for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
67
68
  default_configuration.for_cop('Layout/IndentationWidth')['Width']
68
69
  end
69
70
  end
@@ -135,7 +135,7 @@ module RuboCop
135
135
 
136
136
  def avoid_comma(kind, comma_begin_pos, extra_info)
137
137
  range = range_between(comma_begin_pos, comma_begin_pos + 1)
138
- article = kind =~ /array/ ? 'an' : 'a'
138
+ article = /array/.match?(kind) ? 'an' : 'a'
139
139
  msg = format(
140
140
  MSG,
141
141
  command: 'Avoid',
@@ -22,7 +22,7 @@ module RuboCop
22
22
  MSG = 'Use CamelCase for classes and modules.'
23
23
 
24
24
  def on_class(node)
25
- return unless node.loc.name.source =~ /_/
25
+ return unless /_/.match?(node.loc.name.source)
26
26
 
27
27
  add_offense(node, location: :name)
28
28
  end
@@ -45,8 +45,9 @@ module RuboCop
45
45
  # SomeClass = Class.new(...)
46
46
  # SomeClass = Struct.new(...)
47
47
  return if allowed_assignment?(value)
48
+ return if SNAKE_CASE.match?(const_name)
48
49
 
49
- add_offense(node, location: :name) if const_name !~ SNAKE_CASE
50
+ add_offense(node, location: :name)
50
51
  end
51
52
 
52
53
  private
@@ -39,7 +39,7 @@ module RuboCop
39
39
  def meaningful_delimiters?(node)
40
40
  delimiters = delimiter_string(node)
41
41
 
42
- return false unless delimiters =~ /\w/
42
+ return false unless /\w/.match?(delimiters)
43
43
 
44
44
  forbidden_delimiters.none? do |forbidden_delimiter|
45
45
  delimiters =~ Regexp.new(forbidden_delimiter)
@@ -73,7 +73,7 @@ module RuboCop
73
73
  node.body&.each_descendant(:lvar) do |var|
74
74
  next unless var.children.first == offending_name
75
75
 
76
- corrector.replace(var.loc.expression, preferred_name)
76
+ corrector.replace(var, preferred_name)
77
77
  end
78
78
  end
79
79
  end
@@ -22,12 +22,13 @@ module RuboCop
22
22
 
23
23
  # Registry that tracks all cops by their badge and department.
24
24
  class Registry
25
- def initialize(cops = [])
25
+ def initialize(cops = [], options = {})
26
26
  @registry = {}
27
27
  @departments = {}
28
28
  @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] }
29
29
 
30
30
  cops.each { |cop| enlist(cop) }
31
+ @options = options
31
32
  end
32
33
 
33
34
  def enlist(cop)
@@ -147,9 +148,8 @@ module RuboCop
147
148
  def enabled?(cop, config, only_safe)
148
149
  cfg = config.for_cop(cop)
149
150
 
150
- # cfg['Enabled'] might be a string `pending`, which is considered
151
- # disabled
152
- cop_enabled = cfg.fetch('Enabled') == true
151
+ cop_enabled = cfg.fetch('Enabled') == true ||
152
+ enabled_pending_cop?(cfg, config)
153
153
 
154
154
  if only_safe
155
155
  cop_enabled && cfg.fetch('Safe', true)
@@ -158,6 +158,13 @@ module RuboCop
158
158
  end
159
159
  end
160
160
 
161
+ def enabled_pending_cop?(cop_cfg, config)
162
+ return false if @options[:disable_pending_cops]
163
+
164
+ cop_cfg.fetch('Enabled') == 'pending' &&
165
+ (@options[:enable_pending_cops] || config.enabled_new_cops?)
166
+ end
167
+
161
168
  def names
162
169
  cops.map(&:cop_name)
163
170
  end
@@ -115,7 +115,7 @@ module RuboCop
115
115
  lambda do |corrector|
116
116
  new, old = *send_node.arguments
117
117
  replacement = "alias #{identifier(new)} #{identifier(old)}"
118
- corrector.replace(send_node.source_range, replacement)
118
+ corrector.replace(send_node, replacement)
119
119
  end
120
120
  end
121
121
 
@@ -125,15 +125,15 @@ module RuboCop
125
125
  'alias_method ' \
126
126
  ":#{identifier(node.new_identifier)}, " \
127
127
  ":#{identifier(node.old_identifier)}"
128
- corrector.replace(node.source_range, replacement)
128
+ corrector.replace(node, replacement)
129
129
  end
130
130
  end
131
131
 
132
132
  def correct_alias_with_symbol_args(node)
133
133
  lambda do |corrector|
134
- corrector.replace(node.new_identifier.source_range,
134
+ corrector.replace(node.new_identifier,
135
135
  node.new_identifier.source[1..-1])
136
- corrector.replace(node.old_identifier.source_range,
136
+ corrector.replace(node.old_identifier,
137
137
  node.old_identifier.source[1..-1])
138
138
  end
139
139
  end
@@ -97,12 +97,12 @@ module RuboCop
97
97
  return unless correctable_send?(node)
98
98
 
99
99
  corrector.replace(whitespace_before_arg(node), '(')
100
- corrector.insert_after(node.last_argument.source_range, ')')
100
+ corrector.insert_after(node.last_argument, ')')
101
101
  end
102
102
 
103
103
  def correct_setter(node, corrector)
104
- corrector.insert_before(node.receiver.source_range, '(')
105
- corrector.insert_after(node.last_argument.source_range, ')')
104
+ corrector.insert_before(node.receiver, '(')
105
+ corrector.insert_after(node.last_argument, ')')
106
106
  end
107
107
 
108
108
  # ! is a special case:
@@ -124,8 +124,7 @@ module RuboCop
124
124
  def correct_other(node, corrector)
125
125
  return if node.source_range.begin.is?('(')
126
126
 
127
- corrector.insert_before(node.source_range, '(')
128
- corrector.insert_after(node.source_range, ')')
127
+ corrector.wrap(node, '(', ')')
129
128
  end
130
129
 
131
130
  def correctable_send?(node)
@@ -137,7 +136,7 @@ module RuboCop
137
136
  end_paren = begin_paren
138
137
  # Increment position of parenthesis, unless message is a predicate
139
138
  # method followed by a non-whitespace char (e.g. is_a?String).
140
- end_paren += 1 unless node.source =~ /\?\S/
139
+ end_paren += 1 unless /\?\S/.match?(node.source)
141
140
  range_between(begin_paren, end_paren)
142
141
  end
143
142
  end
@@ -30,7 +30,7 @@ module RuboCop
30
30
  array, join_arg = join_candidate?(node).map(&:source)
31
31
 
32
32
  lambda do |corrector|
33
- corrector.replace(node.source_range, "#{array}.join(#{join_arg})")
33
+ corrector.replace(node, "#{array}.join(#{join_arg})")
34
34
  end
35
35
  end
36
36
  end
@@ -16,14 +16,37 @@ module RuboCop
16
16
  # (1..100).include?(7)
17
17
  # some_string =~ /something/
18
18
  #
19
+ # @example AllowOnConstant
20
+ # # Style/CaseEquality:
21
+ # # AllowOnConstant: true
22
+ #
23
+ # # bad
24
+ # (1..100) === 7
25
+ # /something/ === some_string
26
+ #
27
+ # # good
28
+ # Array === something
29
+ # (1..100).include?(7)
30
+ # some_string =~ /something/
31
+ #
19
32
  class CaseEquality < Cop
20
33
  MSG = 'Avoid the use of the case equality operator `===`.'
21
34
 
22
- def_node_matcher :case_equality?, '(send _ :=== _)'
35
+ def_node_matcher :case_equality?, '(send #const? :=== _)'
23
36
 
24
37
  def on_send(node)
25
38
  case_equality?(node) { add_offense(node, location: :selector) }
26
39
  end
40
+
41
+ private
42
+
43
+ def const?(node)
44
+ if cop_config.fetch('AllowOnConstant', false)
45
+ !node.const_type?
46
+ else
47
+ true
48
+ end
49
+ end
27
50
  end
28
51
  end
29
52
  end
@@ -33,9 +33,9 @@ module RuboCop
33
33
  # special character like \n
34
34
  # or ' which needs to use "" or be escaped.
35
35
  if string.length == 2 || string == "'"
36
- corrector.replace(node.source_range, %("#{string}"))
36
+ corrector.replace(node, %("#{string}"))
37
37
  elsif string.length == 1 # normal character
38
- corrector.replace(node.source_range, "'#{string}'")
38
+ corrector.replace(node, "'#{string}'")
39
39
  end
40
40
  end
41
41
  end
@@ -458,7 +458,7 @@ module RuboCop
458
458
  def correct_if_branches(corrector, cop, node)
459
459
  if_branch, elsif_branches, else_branch = extract_tail_branches(node)
460
460
 
461
- corrector.insert_before(node.source_range, lhs(if_branch))
461
+ corrector.insert_before(node, lhs(if_branch))
462
462
  replace_branch_assignment(corrector, if_branch)
463
463
  correct_branches(corrector, elsif_branches)
464
464
  replace_branch_assignment(corrector, else_branch)
@@ -475,13 +475,13 @@ module RuboCop
475
475
  source
476
476
  end
477
477
 
478
- corrector.replace(branch.source_range, replacement)
478
+ corrector.replace(branch, replacement)
479
479
  end
480
480
 
481
481
  def correct_branches(corrector, branches)
482
482
  branches.each do |branch|
483
483
  *_, assignment = *branch
484
- corrector.replace(branch.source_range, assignment.source)
484
+ corrector.replace(branch, assignment.source)
485
485
  end
486
486
  end
487
487
  end
@@ -494,7 +494,7 @@ module RuboCop
494
494
 
495
495
  def correct(node)
496
496
  lambda do |corrector|
497
- corrector.replace(node.source_range, correction(node))
497
+ corrector.replace(node, correction(node))
498
498
  end
499
499
  end
500
500
 
@@ -547,7 +547,7 @@ module RuboCop
547
547
  end
548
548
 
549
549
  def move_branch_inside_condition(corrector, branch, assignment)
550
- corrector.insert_before(branch.loc.expression, assignment.source)
550
+ corrector.insert_before(branch, assignment.source)
551
551
  end
552
552
  end
553
553
  end
@@ -589,7 +589,7 @@ module RuboCop
589
589
  def move_branch_inside_condition(corrector, branch, condition,
590
590
  assignment, column)
591
591
  branch_assignment = tail(branch)
592
- corrector.insert_before(branch_assignment.loc.expression,
592
+ corrector.insert_before(branch_assignment,
593
593
  assignment.source)
594
594
 
595
595
  remove_whitespace_in_branches(corrector, branch, condition, column)
@@ -611,7 +611,7 @@ module RuboCop
611
611
  when_branches, else_branch = extract_tail_branches(node)
612
612
 
613
613
  lambda do |corrector|
614
- corrector.insert_before(node.source_range, lhs(else_branch))
614
+ corrector.insert_before(node, lhs(else_branch))
615
615
  correct_branches(corrector, when_branches)
616
616
  replace_branch_assignment(corrector, else_branch)
617
617
 
@@ -652,7 +652,7 @@ module RuboCop
652
652
  def move_branch_inside_condition(corrector, branch, condition,
653
653
  assignment, column)
654
654
  branch_assignment = tail(branch)
655
- corrector.insert_before(branch_assignment.loc.expression,
655
+ corrector.insert_before(branch_assignment,
656
656
  assignment.source)
657
657
 
658
658
  remove_whitespace_in_branches(corrector, branch, condition, column)
@@ -55,7 +55,7 @@ module RuboCop
55
55
  raise Warning, AUTOCORRECT_EMPTY_WARNING if autocorrect_notice.empty?
56
56
 
57
57
  regex = Regexp.new(notice)
58
- return if autocorrect_notice =~ regex
58
+ return if autocorrect_notice&.match?(regex)
59
59
 
60
60
  raise Warning, "AutocorrectNotice '#{autocorrect_notice}' must " \
61
61
  "match Notice /#{notice}/"
@@ -33,7 +33,7 @@ module RuboCop
33
33
 
34
34
  def autocorrect(node)
35
35
  lambda do |corrector|
36
- corrector.replace(node.source_range, '__dir__')
36
+ corrector.replace(node, '__dir__')
37
37
  end
38
38
  end
39
39
 
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Lint/RedundantCopDisableDirective
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Style
8
+ # Detects comments to enable/disable RuboCop.
9
+ # This is useful if want to make sure that every RuboCop error gets fixed
10
+ # and not quickly disabled with a comment.
11
+ #
12
+ # @example
13
+ # # bad
14
+ # # rubocop:disable Metrics/AbcSize
15
+ # def f
16
+ # end
17
+ # # rubocop:enable Metrics/AbcSize
18
+ #
19
+ # # good
20
+ # def fixed_method_name_and_no_rubocop_comments
21
+ # end
22
+ #
23
+ class DisableCopsWithinSourceCodeDirective < Cop
24
+ # rubocop:enable Lint/RedundantCopDisableDirective
25
+ MSG = 'Comment to disable/enable RuboCop.'
26
+
27
+ def investigate(processed_source)
28
+ processed_source.comments.each do |comment|
29
+ next unless rubocop_directive_comment?(comment)
30
+
31
+ add_offense(comment)
32
+ end
33
+ end
34
+
35
+ def autocorrect(comment)
36
+ lambda do |corrector|
37
+ corrector.replace(comment, '')
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def rubocop_directive_comment?(comment)
44
+ comment.text =~ CommentConfig::COMMENT_DIRECTIVE_REGEXP
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -45,7 +45,7 @@ module RuboCop
45
45
  end
46
46
 
47
47
  lambda do |corrector|
48
- corrector.replace(comment.loc.expression,
48
+ corrector.replace(comment,
49
49
  comment.text[/#{prefix} \S+/])
50
50
  end
51
51
  end
@@ -42,7 +42,7 @@ module RuboCop
42
42
 
43
43
  max += 1 if range_type == :irange
44
44
 
45
- corrector.replace(node.send_node.source_range,
45
+ corrector.replace(node.send_node,
46
46
  "#{max - min}.times")
47
47
  end
48
48
  end
@@ -48,15 +48,15 @@ module RuboCop
48
48
 
49
49
  first_arg, second_arg = *node.arguments
50
50
 
51
- corrector.replace(first_arg.loc.expression, second_arg.source)
52
- corrector.replace(second_arg.loc.expression, first_arg.source)
51
+ corrector.replace(first_arg, second_arg.source)
52
+ corrector.replace(second_arg, first_arg.source)
53
53
 
54
54
  return_value = return_value(node.body)
55
55
 
56
56
  if return_value_occupies_whole_line?(return_value)
57
57
  corrector.remove(whole_line_expression(return_value))
58
58
  else
59
- corrector.remove(return_value.loc.expression)
59
+ corrector.remove(return_value)
60
60
  end
61
61
  end
62
62
  end