Skip to content

Commit

Permalink
Use Cop::Base API for some Style cops
Browse files Browse the repository at this point in the history
Follow #7868.

This PR uses `Cop::Base` API for some `Style` cops.
  • Loading branch information
koic committed Sep 22, 2020
1 parent 7a10d42 commit fd6d273
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 96 deletions.
15 changes: 7 additions & 8 deletions lib/rubocop/cop/style/commented_keyword.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ module Style
# class X # :nodoc:
# y
# end
class CommentedKeyword < Cop
class CommentedKeyword < Base
MSG = 'Do not place comments on the same line as the ' \
'`%<keyword>s` keyword.'

def investigate(processed_source)
def on_new_investigation
processed_source.comments.each do |comment|
add_offense(comment) if offensive?(comment)
next unless (match = line(comment).match(/(?<keyword>\S+).*#/))

if offensive?(comment)
add_offense(comment, message: format(MSG, keyword: match[:keyword]))
end
end
end

Expand All @@ -62,11 +66,6 @@ def offensive?(comment)
ALLOWED_COMMENT_REGEXES.none? { |r| r.match?(line) }
end

def message(comment)
keyword = line(comment).match(/(\S+).*#/)[1]
format(MSG, keyword: keyword)
end

def line(comment)
comment.location.expression.source_line
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/style/multiline_block_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Style
# alive_threads.map do |t|
# t.object_id
# end
class MultilineBlockChain < Cop
class MultilineBlockChain < Base
include RangeHelp

MSG = 'Avoid multi-line chains of blocks.'
Expand All @@ -36,7 +36,7 @@ def on_block(node)
range = range_between(receiver.loc.end.begin_pos,
node.send_node.source_range.end_pos)

add_offense(nil, location: range)
add_offense(range)

# Done. If there are more blocks in the chain, they will be
# found by subsequent calls to on_block.
Expand Down
9 changes: 4 additions & 5 deletions lib/rubocop/cop/style/redundant_conditional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ module Style
#
# # good
# x != y
class RedundantConditional < Cop
class RedundantConditional < Base
include Alignment
extend AutoCorrector

operators = RuboCop::AST::Node::COMPARISON_OPERATORS.to_a
COMPARISON_OPERATOR_MATCHER = "{:#{operators.join(' :')}}"
Expand All @@ -36,11 +37,9 @@ class RedundantConditional < Cop
def on_if(node)
return unless offense?(node)

add_offense(node)
end
message = message(node)

def autocorrect(node)
lambda do |corrector|
add_offense(node, message: message) do |corrector|
corrector.replace(node, replacement_condition(node))
end
end
Expand Down
20 changes: 9 additions & 11 deletions lib/rubocop/cop/style/redundant_percent_q.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module Style
# time = "8 o'clock"
# question = '"What did you say?"'
#
class RedundantPercentQ < Cop
class RedundantPercentQ < Base
extend AutoCorrector

MSG = 'Use `%<q_type>s` only for strings that contain both ' \
'single quotes and double quotes%<extra>s.'
DYNAMIC_MSG = ', or for dynamic strings that contain ' \
Expand Down Expand Up @@ -45,22 +47,18 @@ def on_str(node)
check(node)
end

def autocorrect(node)
delimiter =
/^%Q[^"]+$|'/.match?(node.source) ? QUOTE : SINGLE_QUOTE
lambda do |corrector|
corrector.replace(node.loc.begin, delimiter)
corrector.replace(node.loc.end, delimiter)
end
end

private

def check(node)
return unless start_with_percent_q_variant?(node)
return if interpolated_quotes?(node) || allowed_percent_q?(node)

add_offense(node)
add_offense(node) do |corrector|
delimiter = /^%Q[^"]+$|'/.match?(node.source) ? QUOTE : SINGLE_QUOTE

corrector.replace(node.loc.begin, delimiter)
corrector.replace(node.loc.end, delimiter)
end
end

def interpolated_quotes?(node)
Expand Down
21 changes: 9 additions & 12 deletions lib/rubocop/cop/style/redundant_return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ module Style
# return x, y
# end
#
class RedundantReturn < Cop
class RedundantReturn < Base
include RangeHelp
extend AutoCorrector

MSG = 'Redundant `return` detected.'
MULTI_RETURN_MSG = 'To return multiple values, use an array.'
Expand All @@ -58,16 +59,6 @@ def on_def(node)
end
alias on_defs on_def

def autocorrect(node)
lambda do |corrector|
if node.arguments?
correct_with_arguments(node, corrector)
else
correct_without_arguments(node, corrector)
end
end
end

private

def correct_without_arguments(return_node, corrector)
Expand Down Expand Up @@ -121,7 +112,13 @@ def check_return_node(node)
return if cop_config['AllowMultipleReturnValues'] &&
node.children.size > 1

add_offense(node, location: :keyword)
add_offense(node.loc.keyword, message: message(node)) do |corrector|
if node.arguments?
correct_with_arguments(node, corrector)
else
correct_without_arguments(node, corrector)
end
end
end

def check_case_node(node)
Expand Down
16 changes: 7 additions & 9 deletions lib/rubocop/cop/style/redundant_self.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ module Style
# self.bar == bar # Resolves name clash with argument of the block.
# end
# end
class RedundantSelf < Cop
class RedundantSelf < Base
extend AutoCorrector

MSG = 'Redundant `self` detected.'
KERNEL_METHODS = Kernel.methods(false)
KEYWORDS = %i[alias and begin break case class def defined? do
Expand Down Expand Up @@ -106,20 +108,16 @@ def on_send(node)

return if allowed_send_node?(node)

add_offense(node)
add_offense(node) do |corrector|
corrector.remove(node.receiver)
corrector.remove(node.loc.dot)
end
end

def on_block(node)
add_scope(node, @local_variables_scopes[node])
end

def autocorrect(node)
lambda do |corrector|
corrector.remove(node.receiver)
corrector.remove(node.loc.dot)
end
end

private

def add_scope(node, local_variables = [])
Expand Down
37 changes: 13 additions & 24 deletions lib/rubocop/cop/style/redundant_sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ module Style
# # good
# arr.max_by(&:foo)
#
class RedundantSort < Cop
class RedundantSort < Base
include RangeHelp
extend AutoCorrector

MSG = 'Use `%<suggestion>s` instead of '\
'`%<sorter>s...%<accessor_source>s`.'
Expand Down Expand Up @@ -82,35 +83,23 @@ def on_send(node)
return
end

add_offense(ancestor,
location: offense_range(sort_node, ancestor),
message: message(ancestor,
sorter,
accessor))
end

def autocorrect(node)
sort_node, sorter, accessor = redundant_sort?(node)

lambda do |corrector|
# Remove accessor, e.g. `first` or `[-1]`.
corrector.remove(
range_between(
accessor_start(node),
node.loc.expression.end_pos
)
)
message = message(ancestor, sorter, accessor)

# Replace "sort" or "sort_by" with the appropriate min/max method.
corrector.replace(
sort_node.loc.selector,
suggestion(sorter, accessor, arg_value(node))
)
add_offense(offense_range(sort_node, ancestor), message: message) do |corrector|
autocorrect(corrector, ancestor, sort_node, sorter, accessor)
end
end

private

def autocorrect(corrector, node, sort_node, sorter, accessor)
# Remove accessor, e.g. `first` or `[-1]`.
corrector.remove(range_between(accessor_start(node), node.loc.expression.end_pos))

# Replace "sort" or "sort_by" with the appropriate min/max method.
corrector.replace(sort_node.loc.selector, suggestion(sorter, accessor, arg_value(node)))
end

def offense_range(sort_node, ancestor)
range_between(sort_node.loc.selector.begin_pos, ancestor.loc.expression.end_pos)
end
Expand Down
14 changes: 5 additions & 9 deletions lib/rubocop/cop/style/redundant_sort_by.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ module Style
#
# # good
# array.sort
class RedundantSortBy < Cop
class RedundantSortBy < Base
include RangeHelp
extend AutoCorrector

MSG = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'

Expand All @@ -28,17 +29,12 @@ def on_block(node)
redundant_sort_by(node) do |send, var_name|
range = sort_by_range(send, node)

add_offense(node,
location: range,
message: format(MSG, var: var_name))
add_offense(range, message: format(MSG, var: var_name)) do |corrector|
corrector.replace(range, 'sort')
end
end
end

def autocorrect(node)
send = node.send_node
->(corrector) { corrector.replace(sort_by_range(send, node), 'sort') }
end

private

def sort_by_range(send, node)
Expand Down
36 changes: 20 additions & 16 deletions lib/rubocop/cop/style/rescue_standard_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ module Style
# rescue StandardError, SecurityError
# bar
# end
class RescueStandardError < Cop
class RescueStandardError < Base
include RescueNode
include ConfigurableEnforcedStyle
include RangeHelp
extend AutoCorrector

MSG_IMPLICIT = 'Omit the error class when rescuing ' \
'`StandardError` by itself.'
Expand All @@ -94,28 +95,31 @@ def on_resbody(node)
case style
when :implicit
rescue_standard_error?(node) do |error|
add_offense(node,
location: node.loc.keyword.join(error.loc.expression),
message: MSG_IMPLICIT)
offense_for_implicit_enforced_style(node, error)
end
when :explicit
rescue_without_error_class?(node) do
add_offense(node, location: :keyword, message: MSG_EXPLICIT)
offense_for_exlicit_enforced_style(node)
end
end
end

def autocorrect(node)
lambda do |corrector|
case style
when :implicit
error = rescue_standard_error?(node)
range = range_between(node.loc.keyword.end_pos,
error.loc.expression.end_pos)
corrector.remove(range)
when :explicit
corrector.insert_after(node.loc.keyword, ' StandardError')
end
private

def offense_for_implicit_enforced_style(node, error)
range = node.loc.keyword.join(error.loc.expression)

add_offense(range, message: MSG_IMPLICIT) do |corrector|
error = rescue_standard_error?(node)
range = range_between(node.loc.keyword.end_pos, error.loc.expression.end_pos)

corrector.remove(range)
end
end

def offense_for_exlicit_enforced_style(node)
add_offense(node.loc.keyword, message: MSG_EXPLICIT) do |corrector|
corrector.insert_after(node.loc.keyword, ' StandardError')
end
end
end
Expand Down

0 comments on commit fd6d273

Please sign in to comment.