diff --git a/changelog/new_add_solidus_tax_category_deprecated.md b/changelog/new_add_solidus_tax_category_deprecated.md new file mode 100644 index 0000000..361082a --- /dev/null +++ b/changelog/new_add_solidus_tax_category_deprecated.md @@ -0,0 +1 @@ +* [#x](https://github.com/solidusio/rubocop-solidus/pull/x): Add solidus/tax_category_deprecated warning cop. ([@safafa][]) diff --git a/config/default.yml b/config/default.yml index fa486bd..b4358ee 100644 --- a/config/default.yml +++ b/config/default.yml @@ -73,3 +73,9 @@ Solidus/SpreeTDeprecated: Enabled: true VersionAdded: '0.1' Reference: 'https://github.com/solidusio/rubocop-solidus/issues/22' + +Solidus/TaxCategoryDeprecated: + Description: 'Checks if tax_category is being used and issue a warning' + Enabled: true + VersionAdded: '<>' + Reference: 'https://github.com/solidusio/rubocop-solidus/issues/25' diff --git a/lib/rubocop/cop/solidus/tax_category_deprecated.rb b/lib/rubocop/cop/solidus/tax_category_deprecated.rb new file mode 100644 index 0000000..4fb5985 --- /dev/null +++ b/lib/rubocop/cop/solidus/tax_category_deprecated.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Solidus + # This cop finds .tax_category occurrences and suggest using .tax_categories instead. + # + # @example + # + # # bad + # model.tax_category = data + # + # # good + # model.tax_categories = [data] + # + # + # + class TaxCategoryDeprecated < Base + include TargetSolidusVersion + minimum_solidus_version 2.2 + + MSG = 'If you are Using Taxrate model use `.tax_categories = [data]` instead of `.tax_category = data`' + + def_node_matcher :existing_tax_category?, <<~PATTERN + (send ... :tax_category) + PATTERN + + def on_send(node) + return unless existing_tax_category?(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 e9b7a70..46af44e 100644 --- a/lib/rubocop/cop/solidus_cops.rb +++ b/lib/rubocop/cop/solidus_cops.rb @@ -13,3 +13,4 @@ require_relative 'solidus/spree_icon_deprecated' require_relative 'solidus/spree_refund_call_perform' require_relative 'solidus/spree_t_deprecated' +require_relative 'solidus/tax_category_deprecated' diff --git a/spec/rubocop/cop/solidus/tax_category_deprecated_spec.rb b/spec/rubocop/cop/solidus/tax_category_deprecated_spec.rb new file mode 100644 index 0000000..9dd2b60 --- /dev/null +++ b/spec/rubocop/cop/solidus/tax_category_deprecated_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::Cop::Solidus::TaxCategoryDeprecated, :config do + let(:config) { RuboCop::Config.new } + it 'registers an offense when using `tax_category`' do + expect_offense(<<~RUBY) + model.tax_category + ^^^^^^^^^^^^^^^^^^ Solidus/TaxCategoryDeprecated: If you are Using Taxrate model use `.tax_categories = [data]` instead of `.tax_category = data` + RUBY + end + + it 'does not register an offense when using `tax_categories`' do + expect_no_offenses(<<~RUBY) + model.tax_categories + RUBY + end +end