Skip to content

Commit

Permalink
Use Cop::Base API for Rails department
Browse files Browse the repository at this point in the history
Follow rubocop/rubocop#7868.

This PR uses `Cop::Base` API for almost `Rails` department's cops.
  • Loading branch information
koic authored and bbatsov committed Nov 23, 2020
1 parent 55d87ef commit 9808efd
Show file tree
Hide file tree
Showing 80 changed files with 592 additions and 702 deletions.
15 changes: 5 additions & 10 deletions lib/rubocop/cop/mixin/index_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ def on_csend(node)
end
end

def autocorrect(node)
lambda do |corrector|
correction = prepare_correction(node)
execute_correction(corrector, node, correction)
end
end

private

# @abstract Implemented with `def_node_matcher`
Expand Down Expand Up @@ -69,9 +62,11 @@ def handle_possible_offense(node, match, match_desc)
return if captures.noop_transformation?

add_offense(
node,
message: "Prefer `#{new_method_name}` over `#{match_desc}`."
)
node, message: "Prefer `#{new_method_name}` over `#{match_desc}`."
) do |corrector|
correction = prepare_correction(node)
execute_correction(corrector, node, correction)
end
end

def extract_captures(match)
Expand Down
22 changes: 8 additions & 14 deletions lib/rubocop/cop/rails/action_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ module Rails
# after_filter :do_stuff
# append_around_filter :do_stuff
# skip_after_filter :do_stuff
class ActionFilter < Cop
class ActionFilter < Base
include ConfigurableEnforcedStyle
extend AutoCorrector

MSG = 'Prefer `%<prefer>s` over `%<current>s`.'

Expand Down Expand Up @@ -76,24 +77,17 @@ def on_send(node)
check_method_node(node) unless node.receiver
end

def autocorrect(node)
lambda do |corrector|
corrector.replace(node.loc.selector,
preferred_method(node.loc.selector.source).to_s)
end
end

private

def check_method_node(node)
return unless bad_methods.include?(node.method_name)
method_name = node.method_name
return unless bad_methods.include?(method_name)

add_offense(node, location: :selector)
end
message = format(MSG, prefer: preferred_method(method_name), current: method_name)

def message(node)
format(MSG, prefer: preferred_method(node.method_name),
current: node.method_name)
add_offense(node.loc.selector, message: message) do |corrector|
corrector.replace(node.loc.selector, preferred_method(node.loc.selector.source))
end
end

def bad_methods
Expand Down
23 changes: 9 additions & 14 deletions lib/rubocop/cop/rails/active_record_aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ module Rails
#
# #good
# Book.update!(author: 'Alice')
class ActiveRecordAliases < Cop
class ActiveRecordAliases < Base
extend AutoCorrector

MSG = 'Use `%<prefer>s` instead of `%<current>s`.'

ALIASES = {
Expand All @@ -24,25 +26,18 @@ class ActiveRecordAliases < Cop

def on_send(node)
method_name = node.method_name
alias_method = ALIASES[method_name]

add_offense(
node,
message: format(MSG, prefer: ALIASES[method_name], current: method_name),
location: :selector,
node.loc.selector,
message: format(MSG, prefer: alias_method, current: method_name),
severity: :warning
)
) do |corrector|
corrector.replace(node.loc.selector, alias_method)
end
end

alias on_csend on_send

def autocorrect(node)
lambda do |corrector|
corrector.replace(
node.loc.selector,
ALIASES[node.method_name].to_s
)
end
end
end
end
end
Expand Down
23 changes: 12 additions & 11 deletions lib/rubocop/cop/rails/active_record_callbacks_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ module Rails
# after_commit :after_commit_callback
# end
#
class ActiveRecordCallbacksOrder < Cop
class ActiveRecordCallbacksOrder < Base
extend AutoCorrector

MSG = '`%<current>s` is supposed to appear before `%<previous>s`.'

CALLBACKS_IN_ORDER = %i[
Expand Down Expand Up @@ -55,32 +57,31 @@ def on_class(class_node)
index = CALLBACKS_ORDER_MAP[callback]

if index < previous_index
message = format(MSG, current: callback,
previous: previous_callback)
add_offense(node, message: message)
message = format(MSG, current: callback, previous: previous_callback)
add_offense(node, message: message) do |corrector|
autocorrect(corrector, node)
end
end
previous_index = index
previous_callback = callback
end
end

private

# Autocorrect by swapping between two nodes autocorrecting them
def autocorrect(node)
def autocorrect(corrector, node)
previous = left_siblings_of(node).reverse_each.find do |sibling|
callback?(sibling)
end

current_range = source_range_with_comment(node)
previous_range = source_range_with_comment(previous)

lambda do |corrector|
corrector.insert_before(previous_range, current_range.source)
corrector.remove(current_range)
end
corrector.insert_before(previous_range, current_range.source)
corrector.remove(current_range)
end

private

def defined_callbacks(class_node)
class_def = class_node.body

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/active_record_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Rails
# end
# end
#
class ActiveRecordOverride < Cop
class ActiveRecordOverride < Base
MSG =
'Use %<prefer>s callbacks instead of overriding the Active Record ' \
'method `%<bad>s`.'
Expand Down
32 changes: 11 additions & 21 deletions lib/rubocop/cop/rails/active_support_aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ module Rails
# [1, 2, 'a'].append('b')
# [1, 2, 'a'].prepend('b')
#
class ActiveSupportAliases < Cop
class ActiveSupportAliases < Base
extend AutoCorrector

MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
RESTRICT_ON_SEND = %i[starts_with? ends_with? append prepend].freeze

Expand All @@ -40,29 +42,17 @@ class ActiveSupportAliases < Cop

def on_send(node)
ALIASES.each_key do |aliased_method|
register_offense(node, aliased_method) if
public_send(aliased_method, node)
end
end
next unless public_send(aliased_method, node)

def autocorrect(node)
return false if append(node)
preferred_method = ALIASES[aliased_method][:original]
message = format(MSG, prefer: preferred_method, current: aliased_method)

lambda do |corrector|
method_name = node.loc.selector.source
replacement = ALIASES[method_name.to_sym][:original]
corrector.replace(node.loc.selector, replacement.to_s)
end
end

private
add_offense(node, message: message) do |corrector|
next if append(node)

def register_offense(node, method_name)
add_offense(
node,
message: format(MSG, prefer: ALIASES[method_name][:original],
current: method_name)
)
corrector.replace(node.loc.selector, preferred_method)
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/after_commit_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Rails
# after_create_commit :log_create_action
# after_update_commit :log_update_action
#
class AfterCommitOverride < Cop
class AfterCommitOverride < Base
MSG = 'There can only be one `after_*_commit :%<name>s` hook defined for a model.'

AFTER_COMMIT_CALLBACKS = %i[
Expand Down
12 changes: 6 additions & 6 deletions lib/rubocop/cop/rails/arel_star.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module Rails
# # good
# MyTable.arel_table[Arel.star]
#
class ArelStar < Cop
class ArelStar < Base
extend AutoCorrector

MSG = 'Use `Arel.star` instead of `"*"` for expanded column lists.'

RESTRICT_ON_SEND = %i[[]].freeze
Expand All @@ -29,11 +31,9 @@ class ArelStar < Cop
def on_send(node)
return unless (star = star_bracket?(node))

add_offense(star)
end

def autocorrect(node)
->(corrector) { corrector.replace(node.loc.expression, 'Arel.star') }
add_offense(star) do |corrector|
corrector.replace(star.loc.expression, 'Arel.star')
end
end
end
end
Expand Down
17 changes: 7 additions & 10 deletions lib/rubocop/cop/rails/assert_not.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ module Rails
# # good
# assert_not x
#
class AssertNot < RuboCop::Cop::Cop
class AssertNot < Base
extend AutoCorrector

MSG = 'Prefer `assert_not` over `assert !`.'
RESTRICT_ON_SEND = %i[assert].freeze

def_node_matcher :offensive?, '(send nil? :assert (send ... :!) ...)'

def on_send(node)
add_offense(node) if offensive?(node)
end
return unless offensive?(node)

def autocorrect(node)
expression = node.loc.expression
add_offense(node) do |corrector|
expression = node.loc.expression

lambda do |corrector|
corrector.replace(
expression,
corrected_source(expression.source)
)
corrector.replace(expression, corrected_source(expression.source))
end
end

Expand Down
16 changes: 7 additions & 9 deletions lib/rubocop/cop/rails/attribute_default_block_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ module Rails
# FOO = 123
# attribute :custom_attribute, :integer, default: FOO
# end
class AttributeDefaultBlockValue < Cop
class AttributeDefaultBlockValue < Base
extend AutoCorrector

MSG = 'Pass method in a block to `:default` option.'
RESTRICT_ON_SEND = %i[attribute].freeze
TYPE_OFFENDERS = %i[send array hash].freeze
Expand All @@ -75,15 +77,11 @@ def on_send(node)
value = attribute.children.last
return unless TYPE_OFFENDERS.any? { |type| value.type == type }

add_offense(node, location: value)
end
end

def autocorrect(node)
expression = default_attribute(node).children.last
add_offense(value) do |corrector|
expression = default_attribute(node).children.last

lambda do |corrector|
corrector.replace(expression, "-> { #{expression.source} }")
corrector.replace(value, "-> { #{expression.source} }")
end
end
end
end
Expand Down
26 changes: 8 additions & 18 deletions lib/rubocop/cop/rails/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ module Rails
#
# @see https://guides.rubyonrails.org/5_0_release_notes.html
# @see https://github.com/rails/rails/pull/18937
class BelongsTo < Cop
class BelongsTo < Base
extend AutoCorrector
extend TargetRailsVersion

minimum_target_rails_version 5.0
Expand All @@ -73,27 +74,16 @@ class BelongsTo < Cop
PATTERN

def on_send(node)
match_belongs_to_with_options(node) do |_option_node, option_value|
message =
match_belongs_to_with_options(node) do |option_node, option_value|
message, replacement =
if option_value.true_type?
SUPERFLOUS_REQUIRE_TRUE_MSG
[SUPERFLOUS_REQUIRE_TRUE_MSG, 'optional: false']
elsif option_value.false_type?
SUPERFLOUS_REQUIRE_FALSE_MSG
[SUPERFLOUS_REQUIRE_FALSE_MSG, 'optional: true']
end

add_offense(node, message: message, location: :selector)
end
end

def autocorrect(node)
option_node, option_value = match_belongs_to_with_options(node)
return unless option_node

lambda do |corrector|
if option_value.true_type?
corrector.replace(option_node.loc.expression, 'optional: false')
elsif option_value.false_type?
corrector.replace(option_node.loc.expression, 'optional: true')
add_offense(node.loc.selector, message: message) do |corrector|
corrector.replace(option_node.loc.expression, replacement)
end
end
end
Expand Down
Loading

0 comments on commit 9808efd

Please sign in to comment.