Skip to content

Commit

Permalink
Add solidus/tax_category_deprecated warning cop
Browse files Browse the repository at this point in the history
  • Loading branch information
safafa committed Nov 10, 2023
1 parent ea03a4b commit 0b6d754
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_add_solidus_tax_category_deprecated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#x](https://github.com/solidusio/rubocop-solidus/pull/x): Add solidus/tax_category_deprecated warning cop. ([@safafa][])
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<<next>>'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/25'
36 changes: 36 additions & 0 deletions lib/rubocop/cop/solidus/tax_category_deprecated.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/rubocop/cop/solidus_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
17 changes: 17 additions & 0 deletions spec/rubocop/cop/solidus/tax_category_deprecated_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0b6d754

Please sign in to comment.