diff --git a/CHANGELOG.md b/CHANGELOG.md index 828d951..51c69e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ### New features * [#25](https://github.com/solidusio/rubocop-solidus/issues/25): Add solidus/tax_category_deprecated warning cop. ([@safafa][]) +* [#31](https://github.com/solidusio/rubocop-solidus/issues/31): Add discounted amount deprecated warning cop. ([@safafa][]) + ### Bug fixes * [#53](https://github.com/solidusio/rubocop-solidus/pull/53): Change spree_default_credit_card_deperecated cop severity to warning. ([@safafa][]) diff --git a/config/default.yml b/config/default.yml index b4358ee..2c707c7 100644 --- a/config/default.yml +++ b/config/default.yml @@ -14,6 +14,12 @@ Solidus/ClassEvalDecorator: VersionAdded: '0.1' Reference: 'https://github.com/solidusio/rubocop-solidus/issues/21' +Solidus/DiscountedAmountDeprecated: + Description: 'Checks if .discounted_amount is being used and suggest using .total_before_tax instead.' + Enabled: true + VersionAdded: '<>' + Reference: 'https://github.com/solidusio/rubocop-solidus/issues/31' + Solidus/ExistingCardIdDeprecated: Description: 'Checks if existing_card_id is being used and suggest using wallet_payment_source_id instead' Enabled: true diff --git a/docs/cops_solidus.md b/docs/cops_solidus.md index 9b0a79b..73108d0 100644 --- a/docs/cops_solidus.md +++ b/docs/cops_solidus.md @@ -35,6 +35,28 @@ end * [https://github.com/solidusio/rubocop-solidus/issues/21](https://github.com/solidusio/rubocop-solidus/issues/21) +## Solidus/DiscountedAmountDeprecated + +Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version +--- | --- | --- | --- | --- | --- +Enabled | Yes | No | <> | - | 2.4 + +This cop finds .discounted_amount occurrences and suggest using .total_before_tax instead. + +### Examples + +```ruby +# bad +line_item.discounted_amount + +# good +line_item.total_before_tax +``` + +### References + +* [https://github.com/solidusio/rubocop-solidus/issues/31](https://github.com/solidusio/rubocop-solidus/issues/31) + ## Solidus/ExistingCardIdDeprecated Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version diff --git a/docs/index.md b/docs/index.md index 35e49db..46d8702 100644 --- a/docs/index.md +++ b/docs/index.md @@ -4,6 +4,7 @@ In the following sections, you will find all available cops: #### Department [Solidus](cops_solidus.md) * [Solidus/ClassEvalDecorator](cops_solidus.md#solidusclassevaldecorator) +* [Solidus/DiscountedAmountDeprecated](cops_solidus.md#solidusdiscountedamountdeprecated) * [Solidus/ExistingCardIdDeprecated](cops_solidus.md#solidusexistingcardiddeprecated) * [Solidus/ReimbursementHookDeprecated](cops_solidus.md#solidusreimbursementhookdeprecated) * [Solidus/SpreeCalculatorFreeShippingDeprecated](cops_solidus.md#solidusspreecalculatorfreeshippingdeprecated) diff --git a/lib/rubocop/cop/solidus/discounted_amount_deprecated.rb b/lib/rubocop/cop/solidus/discounted_amount_deprecated.rb new file mode 100644 index 0000000..37684ce --- /dev/null +++ b/lib/rubocop/cop/solidus/discounted_amount_deprecated.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Solidus + # This cop finds .discounted_amount occurrences and suggest using .total_before_tax instead. + # + # @example + # + # # bad + # line_item.discounted_amount + # + # # good + # line_item.total_before_tax + # + # + class DiscountedAmountDeprecated < Base + include TargetSolidusVersion + minimum_solidus_version 2.4 + + MSG = 'Use `.total_before_tax` instead of `.discounted_amount`.' + + def_node_matcher :bad_method?, <<~PATTERN + (send ... :discounted_amount) + PATTERN + + def on_send(node) + return unless bad_method?(node) + + add_offense(node, severity: :warning) + end + end + end + end +end diff --git a/lib/rubocop/cop/solidus_cops.rb b/lib/rubocop/cop/solidus_cops.rb index 46af44e..82b6789 100644 --- a/lib/rubocop/cop/solidus_cops.rb +++ b/lib/rubocop/cop/solidus_cops.rb @@ -3,6 +3,7 @@ require_relative 'mixin/target_solidus_version' require_relative 'solidus/class_eval_decorator' +require_relative 'solidus/discounted_amount_deprecated' require_relative 'solidus/existing_card_id_deprecated' require_relative 'solidus/reimbursement_hook_deprecated' require_relative 'solidus/spree_calculator_free_shipping_deprecated' diff --git a/spec/rubocop/cop/solidus/discounted_amount_deprecated_spec.rb b/spec/rubocop/cop/solidus/discounted_amount_deprecated_spec.rb new file mode 100644 index 0000000..bb56810 --- /dev/null +++ b/spec/rubocop/cop/solidus/discounted_amount_deprecated_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::Cop::Solidus::DiscountedAmountDeprecated, :config do + it 'registers an offense when using .discounted_amount' do + expect_offense(<<~RUBY) + line_item.discounted_amount + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `.total_before_tax` instead of `.discounted_amount`. + RUBY + end + + it 'does not register an offense when using .total_before_tax' do + expect_no_offenses(<<~RUBY) + line_item.total_before_tax + RUBY + end +end