Skip to content

Commit

Permalink
Re-Add Solidus/TaxCategoryDeprecated cop
Browse files Browse the repository at this point in the history
This cop was removed earlier as it was giving a lot of false positives.

We have re-added this cop again but now with a warning offense so that it doesn't raise any errors for this cop
but warns the user to have a look at certain places in the code where something might be wrong.
  • Loading branch information
piyushswain committed Aug 21, 2023
1 parent e62c4c3 commit dfb5cf2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ Solidus/SpreeTDeprecated:
Enabled: true
VersionAdded: '0.1.0'
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/22'

Solidus/TaxCategoryDeprecated:
Description: 'Checks if .tax_category=Instance is being used and suggest to replace it with .tax_categories=<Array of Instances>'
Enabled: true
VersionAdded: '0.1.4'
Reference: 'https://github.com/solidusio/solidus/issues/1836'
32 changes: 32 additions & 0 deletions lib/rubocop/cop/solidus/tax_category_deprecated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Solidus
# This cop finds tax_category= usages and suggests the usage of tax_categories= instead.
# This cop is needed as tax_category= has been deprecated in future version.
#
# @example EnforcedStyle:
# # bad
# model.tax_category = data
#
# # good
# model.tax_categories = [data]
#
class TaxCategoryDeprecated < Base
MSG = 'tax_category= is deprecated and will be removed from Solidus 3.0. Please use tax_categories= instead. If this call is not from a Spree::TaxRate object then it is false positive.'

# @!method bad_method?(node)
def_node_matcher :tax_category?, <<~PATTERN
(send (...) :tax_category= (...))
PATTERN

def on_send(node)
return unless 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 @@ -12,3 +12,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'
33 changes: 33 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,33 @@

# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Solidus::TaxCategoryDeprecated, :config do
let(:config) { RuboCop::Config.new('Solidus/TaxCategoryDeprecated' => { 'Enabled' => true }) }

it 'registers a warning when using `#.tax_category = `' do
expect_offense(<<~RUBY, severity: :warning)
model.tax_category = data
^^^^^^^^^^^^^^^^^^^^^^^^^ tax_category= is deprecated and will be removed from Solidus 3.0. Please use tax_categories= instead. If this call is not from a Spree::TaxRate object then it is false positive.
RUBY
end

it 'registers a warning when using `#.tax_category =`' do
expect_offense(<<~RUBY, severity: :warning)
model.tax_category =data
^^^^^^^^^^^^^^^^^^^^^^^^ tax_category= is deprecated and will be removed from Solidus 3.0. Please use tax_categories= instead. If this call is not from a Spree::TaxRate object then it is false positive.
RUBY
end

it 'registers a warning when using `#.tax_category= `' do
expect_offense(<<~RUBY, severity: :warning)
model.tax_category= data
^^^^^^^^^^^^^^^^^^^^^^^^ tax_category= is deprecated and will be removed from Solidus 3.0. Please use tax_categories= instead. If this call is not from a Spree::TaxRate object then it is false positive.
RUBY
end

it 'does not register any offense when using `#.tax_categories=`' do
expect_no_offenses(<<~RUBY)
model.tax_categories = [data]
RUBY
end
end

0 comments on commit dfb5cf2

Please sign in to comment.