rubocop 0.89.0 → 0.89.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  4. data/lib/rubocop/comment_config.rb +3 -5
  5. data/lib/rubocop/config.rb +1 -1
  6. data/lib/rubocop/config_loader.rb +1 -1
  7. data/lib/rubocop/config_validator.rb +1 -1
  8. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  9. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  10. data/lib/rubocop/cop/layout/class_structure.rb +3 -7
  11. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  12. data/lib/rubocop/cop/layout/extra_spacing.rb +14 -21
  13. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  14. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -1
  15. data/lib/rubocop/cop/lint/missing_super.rb +9 -9
  16. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +33 -4
  17. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +14 -17
  18. data/lib/rubocop/cop/lint/uri_regexp.rb +9 -25
  19. data/lib/rubocop/cop/migration/department_name.rb +2 -2
  20. data/lib/rubocop/cop/mixin/alignment.rb +2 -1
  21. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  22. data/lib/rubocop/cop/mixin/enforce_superclass.rb +0 -2
  23. data/lib/rubocop/cop/mixin/hash_transform_method.rb +5 -11
  24. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -3
  25. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +1 -2
  26. data/lib/rubocop/cop/mixin/percent_array.rb +2 -6
  27. data/lib/rubocop/cop/mixin/range_help.rb +18 -4
  28. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
  29. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
  30. data/lib/rubocop/cop/style/access_modifier_declarations.rb +5 -8
  31. data/lib/rubocop/cop/style/accessor_grouping.rb +20 -20
  32. data/lib/rubocop/cop/style/alias.rb +34 -33
  33. data/lib/rubocop/cop/style/and_or.rb +9 -11
  34. data/lib/rubocop/cop/style/array_join.rb +6 -8
  35. data/lib/rubocop/cop/style/ascii_comments.rb +4 -4
  36. data/lib/rubocop/cop/style/attr.rb +11 -9
  37. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +2 -5
  38. data/lib/rubocop/cop/style/bare_percent_literals.rb +10 -12
  39. data/lib/rubocop/cop/style/begin_block.rb +2 -2
  40. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +9 -12
  41. data/lib/rubocop/cop/style/block_comments.rb +14 -18
  42. data/lib/rubocop/cop/style/block_delimiters.rb +23 -23
  43. data/lib/rubocop/cop/style/case_like_if.rb +17 -14
  44. data/lib/rubocop/cop/style/class_and_module_children.rb +14 -11
  45. data/lib/rubocop/cop/style/class_check.rb +7 -9
  46. data/lib/rubocop/cop/style/class_methods.rb +7 -11
  47. data/lib/rubocop/cop/style/class_vars.rb +6 -10
  48. data/lib/rubocop/cop/style/collection_methods.rb +11 -17
  49. data/lib/rubocop/cop/style/colon_method_call.rb +6 -7
  50. data/lib/rubocop/cop/style/colon_method_definition.rb +6 -6
  51. data/lib/rubocop/cop/style/command_literal.rb +23 -24
  52. data/lib/rubocop/cop/style/comment_annotation.rb +15 -15
  53. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  54. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  55. data/lib/rubocop/cop/style/constant_visibility.rb +3 -2
  56. data/lib/rubocop/cop/style/copyright.rb +12 -12
  57. data/lib/rubocop/cop/style/date_time.rb +1 -1
  58. data/lib/rubocop/cop/style/def_with_parentheses.rb +8 -10
  59. data/lib/rubocop/cop/style/dir.rb +7 -10
  60. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +7 -9
  61. data/lib/rubocop/cop/style/documentation.rb +2 -4
  62. data/lib/rubocop/cop/style/documentation_method.rb +1 -1
  63. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +12 -15
  64. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  65. data/lib/rubocop/cop/style/each_for_simple_loop.rb +5 -8
  66. data/lib/rubocop/cop/style/each_with_object.rb +16 -17
  67. data/lib/rubocop/cop/style/empty_case_condition.rb +19 -20
  68. data/lib/rubocop/cop/style/empty_else.rb +17 -19
  69. data/lib/rubocop/cop/style/empty_literal.rb +15 -16
  70. data/lib/rubocop/cop/style/empty_method.rb +5 -8
  71. data/lib/rubocop/cop/style/encoding.rb +5 -9
  72. data/lib/rubocop/cop/style/end_block.rb +4 -6
  73. data/lib/rubocop/cop/style/eval_with_location.rb +5 -7
  74. data/lib/rubocop/cop/style/even_odd.rb +7 -11
  75. data/lib/rubocop/cop/style/expand_path_arguments.rb +21 -24
  76. data/lib/rubocop/cop/style/exponential_notation.rb +1 -1
  77. data/lib/rubocop/cop/style/float_division.rb +1 -1
  78. data/lib/rubocop/cop/style/for.rb +11 -15
  79. data/lib/rubocop/cop/style/format_string.rb +17 -19
  80. data/lib/rubocop/cop/style/format_string_token.rb +4 -7
  81. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +19 -41
  82. data/lib/rubocop/cop/style/global_vars.rb +2 -2
  83. data/lib/rubocop/cop/style/guard_clause.rb +3 -4
  84. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  85. data/lib/rubocop/cop/style/hash_each_methods.rb +5 -8
  86. data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
  87. data/lib/rubocop/cop/style/hash_transform_keys.rb +3 -2
  88. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -1
  89. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  90. data/lib/rubocop/cop/style/if_inside_else.rb +2 -2
  91. data/lib/rubocop/cop/style/if_unless_modifier.rb +10 -12
  92. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -7
  93. data/lib/rubocop/cop/style/if_with_semicolon.rb +3 -6
  94. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -1
  95. data/lib/rubocop/cop/style/infinite_loop.rb +23 -23
  96. data/lib/rubocop/cop/style/inline_comment.rb +3 -3
  97. data/lib/rubocop/cop/style/inverse_methods.rb +20 -29
  98. data/lib/rubocop/cop/style/lambda.rb +7 -12
  99. data/lib/rubocop/cop/style/lambda_call.rb +14 -13
  100. data/lib/rubocop/cop/style/line_end_concatenation.rb +19 -16
  101. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -1
  102. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +11 -11
  103. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +4 -8
  104. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +7 -6
  105. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -2
  106. data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -16
  107. data/lib/rubocop/cop/style/min_max.rb +8 -12
  108. data/lib/rubocop/cop/style/missing_else.rb +13 -13
  109. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +1 -1
  110. data/lib/rubocop/cop/style/mixin_grouping.rb +24 -27
  111. data/lib/rubocop/cop/style/mixin_usage.rb +1 -1
  112. data/lib/rubocop/cop/style/module_function.rb +10 -13
  113. data/lib/rubocop/cop/style/multiline_if_modifier.rb +3 -10
  114. data/lib/rubocop/cop/style/multiline_if_then.rb +4 -10
  115. data/lib/rubocop/cop/style/multiline_memoization.rb +12 -10
  116. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  117. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +4 -6
  118. data/lib/rubocop/cop/style/multiline_when_then.rb +7 -9
  119. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  120. data/lib/rubocop/cop/style/mutable_constant.rb +23 -20
  121. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +3 -1
  122. data/lib/rubocop/cop/style/redundant_sort.rb +0 -2
  123. data/lib/rubocop/cop/style/safe_navigation.rb +4 -4
  124. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  125. data/lib/rubocop/cop/util.rb +2 -0
  126. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  127. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  128. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  129. data/lib/rubocop/options.rb +3 -3
  130. data/lib/rubocop/rspec/shared_contexts.rb +1 -5
  131. data/lib/rubocop/runner.rb +1 -1
  132. data/lib/rubocop/version.rb +1 -1
  133. metadata +4 -4
@@ -64,8 +64,9 @@ module RuboCop
64
64
  inner.begin_pos >= outer.begin_pos && inner.end_pos <= outer.end_pos
65
65
  end
66
66
 
67
+ # @deprecated Use processed_source.comment_at_line(line)
67
68
  def end_of_line_comment(line)
68
- processed_source.find_comment { |c| c.loc.line == line }
69
+ processed_source.line_with_comment?(line)
69
70
  end
70
71
  end
71
72
  end
@@ -59,7 +59,7 @@ module RuboCop
59
59
  return if safe_to_ignore?(node)
60
60
 
61
61
  line = processed_source.lines[node.first_line - 1]
62
- return if processed_source.commented?(node.loc.begin)
62
+ return if processed_source.line_with_comment?(node.loc.line)
63
63
  return if line.length <= max
64
64
 
65
65
  extract_first_element_over_column_limit(node, elements, max)
@@ -5,8 +5,6 @@ module RuboCop
5
5
  # Common functionality for enforcing a specific superclass
6
6
  module EnforceSuperclass
7
7
  def self.included(base)
8
- super
9
-
10
8
  base.def_node_matcher :class_definition, <<~PATTERN
11
9
  (class (const _ !:#{base::SUPERCLASS}) #{base::BASE_PATTERN} ...)
12
10
  PATTERN
@@ -26,13 +26,6 @@ module RuboCop
26
26
  end
27
27
  end
28
28
 
29
- def autocorrect(node)
30
- lambda do |corrector|
31
- correction = prepare_correction(node)
32
- execute_correction(corrector, node, correction)
33
- end
34
- end
35
-
36
29
  private
37
30
 
38
31
  # @abstract Implemented with `def_node_matcher`
@@ -61,10 +54,11 @@ module RuboCop
61
54
  # `transform_values` if value transformation uses key.
62
55
  return if captures.transformation_uses_both_args?
63
56
 
64
- add_offense(
65
- node,
66
- message: "Prefer `#{new_method_name}` over `#{match_desc}`."
67
- )
57
+ message = "Prefer `#{new_method_name}` over `#{match_desc}`."
58
+ add_offense(node, message: message) do |corrector|
59
+ correction = prepare_correction(node)
60
+ execute_correction(corrector, node, correction)
61
+ end
68
62
  end
69
63
 
70
64
  # @abstract
@@ -12,9 +12,7 @@ module RuboCop
12
12
 
13
13
  def directive_on_source_line?(line_index)
14
14
  source_line_number = line_index + processed_source.buffer.first_line
15
- comment =
16
- processed_source.comments
17
- .detect { |e| e.location.line == source_line_number }
15
+ comment = processed_source.comment_at_line(source_line_number)
18
16
 
19
17
  return false unless comment
20
18
 
@@ -27,8 +27,7 @@ module RuboCop
27
27
  last_element_line =
28
28
  last_element_range_with_trailing_comma(node).last_line
29
29
 
30
- last_element_commented =
31
- processed_source.comments.any? { |c| c.loc.line == last_element_line }
30
+ last_element_commented = processed_source.comment_at_line(last_element_line)
32
31
 
33
32
  last_element_commented && (node.chained? || node.argument?)
34
33
  end
@@ -28,12 +28,8 @@ module RuboCop
28
28
  end
29
29
 
30
30
  def comments_in_array?(node)
31
- comments = processed_source.comments
32
- array_range = node.source_range.to_a
33
-
34
- comments.any? do |comment|
35
- !(comment.loc.expression.to_a & array_range).empty?
36
- end
31
+ line_span = node.source_range.first_line...node.source_range.last_line
32
+ processed_source.each_comment_in_lines(line_span).any?
37
33
  end
38
34
 
39
35
  def check_percent_array(node)
@@ -46,7 +46,8 @@ module RuboCop
46
46
  end
47
47
 
48
48
  def range_with_surrounding_space(range:, side: :both,
49
- newlines: true, whitespace: false)
49
+ newlines: true, whitespace: false,
50
+ continuations: false)
50
51
  buffer = @processed_source.buffer
51
52
  src = buffer.source
52
53
 
@@ -55,10 +56,13 @@ module RuboCop
55
56
  begin_pos = range.begin_pos
56
57
  if go_left
57
58
  begin_pos =
58
- final_pos(src, begin_pos, -1, newlines, whitespace)
59
+ final_pos(src, begin_pos, -1, continuations, newlines, whitespace)
59
60
  end
60
61
  end_pos = range.end_pos
61
- end_pos = final_pos(src, end_pos, 1, newlines, whitespace) if go_right
62
+ if go_right
63
+ end_pos =
64
+ final_pos(src, end_pos, 1, continuations, newlines, whitespace)
65
+ end
62
66
  Parser::Source::Range.new(buffer, begin_pos, end_pos)
63
67
  end
64
68
 
@@ -101,17 +105,27 @@ module RuboCop
101
105
  end
102
106
  end
103
107
 
104
- def final_pos(src, pos, increment, newlines, whitespace)
108
+ # rubocop:disable Metrics/ParameterLists
109
+ def final_pos(src, pos, increment, continuations, newlines, whitespace)
105
110
  pos = move_pos(src, pos, increment, true, /[ \t]/)
111
+ pos = move_pos_str(src, pos, increment, continuations, "\\\n")
106
112
  pos = move_pos(src, pos, increment, newlines, /\n/)
107
113
  move_pos(src, pos, increment, whitespace, /\s/)
108
114
  end
115
+ # rubocop:enable Metrics/ParameterLists
109
116
 
110
117
  def move_pos(src, pos, step, condition, regexp)
111
118
  offset = step == -1 ? -1 : 0
112
119
  pos += step while condition && regexp.match?(src[pos + offset])
113
120
  pos.negative? ? 0 : pos
114
121
  end
122
+
123
+ def move_pos_str(src, pos, step, condition, needle)
124
+ size = needle.length
125
+ offset = step == -1 ? -size : 0
126
+ pos += size * step while condition && src[pos + offset, size] == needle
127
+ pos.negative? ? 0 : pos
128
+ end
115
129
  end
116
130
  end
117
131
  end
@@ -19,14 +19,14 @@ module RuboCop
19
19
  def non_eligible_node?(node)
20
20
  node.modifier_form? ||
21
21
  node.nonempty_line_count > 3 ||
22
- processed_source.commented?(node.loc.end)
22
+ processed_source.line_with_comment?(node.loc.last_line)
23
23
  end
24
24
 
25
25
  def non_eligible_body?(body)
26
26
  body.nil? ||
27
27
  body.empty_source? ||
28
28
  body.begin_type? ||
29
- processed_source.commented?(body.source_range)
29
+ processed_source.contains_comment?(body.source_range)
30
30
  end
31
31
 
32
32
  def non_eligible_condition?(condition)
@@ -72,7 +72,7 @@ module RuboCop
72
72
  return true if parent.assignment? || parent.operator_keyword?
73
73
  return true if %i[array pair].include?(parent.type)
74
74
 
75
- node.parent.send_type? && !node.parent.parenthesized?
75
+ node.parent.send_type?
76
76
  end
77
77
 
78
78
  def max_line_length
@@ -74,10 +74,8 @@ module RuboCop
74
74
  end
75
75
 
76
76
  def inside_comment?(range, comma_offset)
77
- processed_source.comments.any? do |comment|
78
- comment_offset = comment.loc.expression.begin_pos - range.begin_pos
79
- comment_offset >= 0 && comment_offset < comma_offset
80
- end
77
+ comment = processed_source.comment_at_line(range.line)
78
+ comment && comment.loc.expression.begin_pos < range.begin_pos + comma_offset
81
79
  end
82
80
 
83
81
  # Returns true if the node has round/square/curly brackets.
@@ -62,7 +62,7 @@ module RuboCop
62
62
  # private :bar, :baz
63
63
  #
64
64
  # end
65
- class AccessModifierDeclarations < Cop
65
+ class AccessModifierDeclarations < Base
66
66
  include ConfigurableEnforcedStyle
67
67
 
68
68
  ACCESS_MODIFIERS = %i[private protected public module_function].to_set.freeze
@@ -84,13 +84,10 @@ module RuboCop
84
84
  def on_send(node)
85
85
  return unless access_modifier?(node)
86
86
  return if node.parent.pair_type?
87
- return if cop_config['AllowModifiersOnSymbols'] &&
88
- access_modifier_with_symbol?(node)
87
+ return if cop_config['AllowModifiersOnSymbols'] && access_modifier_with_symbol?(node)
89
88
 
90
89
  if offense?(node)
91
- add_offense(node, location: :selector) do
92
- opposite_style_detected
93
- end
90
+ add_offense(node.loc.selector) if opposite_style_detected
94
91
  else
95
92
  correct_style_detected
96
93
  end
@@ -127,8 +124,8 @@ module RuboCop
127
124
  !access_modifier_is_inlined?(node)
128
125
  end
129
126
 
130
- def message(node)
131
- access_modifier = node.loc.selector.source
127
+ def message(range)
128
+ access_modifier = range.source
132
129
 
133
130
  if group_style?
134
131
  format(GROUP_STYLE_MESSAGE, access_modifier: access_modifier)
@@ -31,10 +31,11 @@ module RuboCop
31
31
  # attr_reader :baz
32
32
  # end
33
33
  #
34
- class AccessorGrouping < Cop
34
+ class AccessorGrouping < Base
35
35
  include ConfigurableEnforcedStyle
36
36
  include RangeHelp
37
37
  include VisibilityHelp
38
+ extend AutoCorrector
38
39
 
39
40
  GROUPED_MSG = 'Group together all `%<accessor>s` attributes.'
40
41
  SEPARATED_MSG = 'Use one attribute per `%<accessor>s`.'
@@ -51,18 +52,27 @@ module RuboCop
51
52
  alias on_sclass on_class
52
53
  alias on_module on_class
53
54
 
54
- def autocorrect(node)
55
- lambda do |corrector|
56
- if (preferred_accessors = preferred_accessors(node))
57
- corrector.replace(node, preferred_accessors)
58
- else
59
- range = range_with_surrounding_space(range: node.loc.expression, side: :left)
60
- corrector.remove(range)
61
- end
55
+ private
56
+
57
+ def check(send_node)
58
+ return if previous_line_comment?(send_node)
59
+ return unless grouped_style? && sibling_accessors(send_node).size > 1 ||
60
+ separated_style? && send_node.arguments.size > 1
61
+
62
+ message = message(send_node)
63
+ add_offense(send_node, message: message) do |corrector|
64
+ autocorrect(corrector, send_node)
62
65
  end
63
66
  end
64
67
 
65
- private
68
+ def autocorrect(corrector, node)
69
+ if (preferred_accessors = preferred_accessors(node))
70
+ corrector.replace(node, preferred_accessors)
71
+ else
72
+ range = range_with_surrounding_space(range: node.loc.expression, side: :left)
73
+ corrector.remove(range)
74
+ end
75
+ end
66
76
 
67
77
  def previous_line_comment?(node)
68
78
  comment_line?(processed_source[node.first_line - 2])
@@ -84,16 +94,6 @@ module RuboCop
84
94
  send_node.macro? && ACCESSOR_METHODS.include?(send_node.method_name)
85
95
  end
86
96
 
87
- def check(send_node)
88
- return if previous_line_comment?(send_node)
89
-
90
- if grouped_style? && sibling_accessors(send_node).size > 1
91
- add_offense(send_node)
92
- elsif separated_style? && send_node.arguments.size > 1
93
- add_offense(send_node)
94
- end
95
- end
96
-
97
97
  def grouped_style?
98
98
  style == :grouped
99
99
  end
@@ -22,8 +22,9 @@ module RuboCop
22
22
  #
23
23
  # # good
24
24
  # alias_method :bar, :foo
25
- class Alias < Cop
25
+ class Alias < Base
26
26
  include ConfigurableEnforcedStyle
27
+ extend AutoCorrector
27
28
 
28
29
  MSG_ALIAS = 'Use `alias_method` instead of `alias`.'
29
30
  MSG_ALIAS_METHOD = 'Use `alias` instead of `alias_method` ' \
@@ -36,31 +37,37 @@ module RuboCop
36
37
  return unless style == :prefer_alias && alias_keyword_possible?(node)
37
38
 
38
39
  msg = format(MSG_ALIAS_METHOD, current: lexical_scope_type(node))
39
- add_offense(node, location: :selector, message: msg)
40
+ add_offense(node.loc.selector, message: msg) do |corrector|
41
+ autocorrect(corrector, node)
42
+ end
40
43
  end
41
44
 
42
45
  def on_alias(node)
43
46
  return unless alias_method_possible?(node)
44
47
 
45
48
  if scope_type(node) == :dynamic || style == :prefer_alias_method
46
- add_offense(node, location: :keyword, message: MSG_ALIAS)
49
+ add_offense(node.loc.keyword, message: MSG_ALIAS) do |corrector|
50
+ autocorrect(corrector, node)
51
+ end
47
52
  elsif node.children.none? { |arg| bareword?(arg) }
48
- add_offense_for_args(node)
53
+ add_offense_for_args(node) do |corrector|
54
+ autocorrect(corrector, node)
55
+ end
49
56
  end
50
57
  end
51
58
 
52
- def autocorrect(node)
59
+ private
60
+
61
+ def autocorrect(corrector, node)
53
62
  if node.send_type?
54
- correct_alias_method_to_alias(node)
63
+ correct_alias_method_to_alias(corrector, node)
55
64
  elsif scope_type(node) == :dynamic || style == :prefer_alias_method
56
- correct_alias_to_alias_method(node)
65
+ correct_alias_to_alias_method(corrector, node)
57
66
  else
58
- correct_alias_with_symbol_args(node)
67
+ correct_alias_with_symbol_args(corrector, node)
59
68
  end
60
69
  end
61
70
 
62
- private
63
-
64
71
  def alias_keyword_possible?(node)
65
72
  scope_type(node) != :dynamic && node.arguments.all?(&:sym_type?)
66
73
  end
@@ -70,14 +77,14 @@ module RuboCop
70
77
  node.children.none?(&:gvar_type?)
71
78
  end
72
79
 
73
- def add_offense_for_args(node)
80
+ def add_offense_for_args(node, &block)
74
81
  existing_args = node.children.map(&:source).join(' ')
75
82
  preferred_args = node.children.map { |a| a.source[1..-1] }.join(' ')
76
83
  arg_ranges = node.children.map(&:source_range)
77
84
  msg = format(MSG_SYMBOL_ARGS,
78
85
  prefer: preferred_args,
79
86
  current: existing_args)
80
- add_offense(node, location: arg_ranges.reduce(&:join), message: msg)
87
+ add_offense(arg_ranges.reduce(&:join), message: msg, &block)
81
88
  end
82
89
 
83
90
  # In this expression, will `self` be the same as the innermost enclosing
@@ -115,31 +122,25 @@ module RuboCop
115
122
  !sym_node.source.start_with?(':')
116
123
  end
117
124
 
118
- def correct_alias_method_to_alias(send_node)
119
- lambda do |corrector|
120
- new, old = *send_node.arguments
121
- replacement = "alias #{identifier(new)} #{identifier(old)}"
122
- corrector.replace(send_node, replacement)
123
- end
125
+ def correct_alias_method_to_alias(corrector, send_node)
126
+ new, old = *send_node.arguments
127
+ replacement = "alias #{identifier(new)} #{identifier(old)}"
128
+
129
+ corrector.replace(send_node, replacement)
124
130
  end
125
131
 
126
- def correct_alias_to_alias_method(node)
127
- lambda do |corrector|
128
- replacement =
129
- 'alias_method ' \
130
- ":#{identifier(node.new_identifier)}, " \
131
- ":#{identifier(node.old_identifier)}"
132
- corrector.replace(node, replacement)
133
- end
132
+ def correct_alias_to_alias_method(corrector, node)
133
+ replacement =
134
+ 'alias_method ' \
135
+ ":#{identifier(node.new_identifier)}, " \
136
+ ":#{identifier(node.old_identifier)}"
137
+
138
+ corrector.replace(node, replacement)
134
139
  end
135
140
 
136
- def correct_alias_with_symbol_args(node)
137
- lambda do |corrector|
138
- corrector.replace(node.new_identifier,
139
- node.new_identifier.source[1..-1])
140
- corrector.replace(node.old_identifier,
141
- node.old_identifier.source[1..-1])
142
- end
141
+ def correct_alias_with_symbol_args(corrector, node)
142
+ corrector.replace(node.new_identifier, node.new_identifier.source[1..-1])
143
+ corrector.replace(node.old_identifier, node.old_identifier.source[1..-1])
143
144
  end
144
145
 
145
146
  def_node_matcher :identifier, <<~PATTERN
@@ -36,9 +36,10 @@ module RuboCop
36
36
  # # good
37
37
  # if foo && bar
38
38
  # end
39
- class AndOr < Cop
39
+ class AndOr < Base
40
40
  include ConfigurableEnforcedStyle
41
41
  include RangeHelp
42
+ extend AutoCorrector
42
43
 
43
44
  MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
44
45
 
@@ -55,8 +56,13 @@ module RuboCop
55
56
  alias on_until on_if
56
57
  alias on_until_post on_if
57
58
 
58
- def autocorrect(node)
59
- lambda do |corrector|
59
+ private
60
+
61
+ def process_logical_operator(node)
62
+ return if node.logical_operator?
63
+
64
+ message = message(node)
65
+ add_offense(node.loc.operator, message: message) do |corrector|
60
66
  node.each_child_node do |expr|
61
67
  if expr.send_type?
62
68
  correct_send(expr, corrector)
@@ -71,20 +77,12 @@ module RuboCop
71
77
  end
72
78
  end
73
79
 
74
- private
75
-
76
80
  def on_conditionals(node)
77
81
  node.condition.each_node(*AST::Node::OPERATOR_KEYWORDS) do |operator|
78
82
  process_logical_operator(operator)
79
83
  end
80
84
  end
81
85
 
82
- def process_logical_operator(node)
83
- return if node.logical_operator?
84
-
85
- add_offense(node, location: :operator)
86
- end
87
-
88
86
  def message(node)
89
87
  format(MSG, prefer: node.alternate_operator, current: node.operator)
90
88
  end