Skip to content

Commit

Permalink
Merge pull request #349 from Shopify/remove-deprecated-cop-cop
Browse files Browse the repository at this point in the history
Use RuboCop::Cop::Base
  • Loading branch information
etiennebarrie authored Nov 12, 2024
2 parents 99c99e2 + 2d88692 commit 8dd6e27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
33 changes: 13 additions & 20 deletions lib/rubocop/cop/money/missing_currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
module RuboCop
module Cop
module Money
class MissingCurrency < Cop
class MissingCurrency < Base
extend RuboCop::Cop::AutoCorrector
# `Money.new()` without a currency argument cannot guarantee correctness:
# - no error raised for cross-currency computation (e.g. 5 CAD + 5 USD)
# - #subunits returns wrong values for 0 and 3 decimals currencies
Expand Down Expand Up @@ -32,42 +33,34 @@ class MissingCurrency < Cop
PATTERN

def on_send(node)
receiver, method, _ = *node

money_new(node) do |amount, currency_arg|
return if amount&.splat_type?
return if currency_arg

add_offense(node, message: 'Money is missing currency argument')
end

if to_money_block?(node) || to_money_without_currency?(node)
add_offense(node, message: 'to_money is missing currency argument')
end
end
alias_method :on_csend, :on_send

def autocorrect(node)
receiver, method, _ = *node

lambda do |corrector|
money_new(node) do |amount, currency_arg|
return if currency_arg

add_offense(node, message: 'Money is missing currency argument') do |corrector|
corrector.replace(
node.loc.expression,
"#{receiver.source}.#{method}(#{amount&.source || 0}, #{replacement_currency})",
)
end
end

if to_money_without_currency?(node)
corrector.insert_after(node.loc.expression, "(#{replacement_currency})")
elsif to_money_block?(node)
if to_money_block?(node)
add_offense(node, message: 'to_money is missing currency argument') do |corrector|
corrector.replace(
node.loc.expression,
"#{receiver.source}.#{method} { |x| x.to_money(#{replacement_currency}) }",
)
end
elsif to_money_without_currency?(node)
add_offense(node, message: 'to_money is missing currency argument') do |corrector|
corrector.insert_after(node.loc.expression, "(#{replacement_currency})")
end
end
end
alias_method :on_csend, :on_send

private

Expand Down
18 changes: 6 additions & 12 deletions lib/rubocop/cop/money/zero_money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
module RuboCop
module Cop
module Money
class ZeroMoney < Cop
class ZeroMoney < Base
extend RuboCop::Cop::AutoCorrector

# `Money.zero` and it's alias `empty`, with or without currency
# argument is removed in favour of the more explicit Money.new
# syntax. Supplying it with a real currency is preferred for
Expand Down Expand Up @@ -31,21 +33,13 @@ class ZeroMoney < Cop
PATTERN

def on_send(node)
money_zero(node) do |currency_arg|
add_offense(node, message: format(MSG, currency: replacement_currency(currency_arg)))
end
end

def autocorrect(node)
receiver, _ = *node

lambda do |corrector|
money_zero(node) do |currency_arg|
replacement_currency = replacement_currency(currency_arg)

money_zero(node) do |currency_arg|
add_offense(node, message: format(MSG, currency: replacement_currency(currency_arg))) do |corrector|
corrector.replace(
node.loc.expression,
"#{receiver.source}.new(0, #{replacement_currency})",
"#{receiver.source}.new(0, #{replacement_currency(currency_arg)})",
)
end
end
Expand Down

0 comments on commit 8dd6e27

Please sign in to comment.