Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore solidus/tax_category_deprecated warning cop #63

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## main (unreleased)

### New features

* [#25](https://github.com/solidusio/rubocop-solidus/issues/25): Add solidus/tax_category_deprecated warning cop. ([@safafa][])

## 0.2.0 (2023-11-02)

### New features
Expand Down Expand Up @@ -36,3 +40,4 @@

- Initial release
[@MassimilianoLattanzio]: https://github.com/MassimilianoLattanzio
[@safafa]: https://github.com/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>>'
safafa marked this conversation as resolved.
Show resolved Hide resolved
Reference: 'https://github.com/solidusio/rubocop-solidus/issues/25'
22 changes: 22 additions & 0 deletions docs/cops_solidus.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,25 @@ I18n.t('bar', scope: 'spree.admin.city')
### References

* [https://github.com/solidusio/rubocop-solidus/issues/22](https://github.com/solidusio/rubocop-solidus/issues/22)

## Solidus/TaxCategoryDeprecated

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged | Required Solidus Version
--- | --- | --- | --- | --- | ---
Enabled | Yes | No | <<next>> | - | 2.2

This cop finds .tax_category occurrences and suggest using .tax_categories instead.

### Examples

```ruby
# bad
model.tax_category = data

# good
model.tax_categories = [data]
```

### References

* [https://github.com/solidusio/rubocop-solidus/issues/25](https://github.com/solidusio/rubocop-solidus/issues/25)
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ In the following sections, you will find all available cops:
* [Solidus/SpreeIconDeprecated](cops_solidus.md#solidusspreeicondeprecated)
* [Solidus/SpreeRefundCallPerform](cops_solidus.md#solidusspreerefundcallperform)
* [Solidus/SpreeTDeprecated](cops_solidus.md#solidusspreetdeprecated)
* [Solidus/TaxCategoryDeprecated](cops_solidus.md#solidustaxcategorydeprecated)

<!-- END_COP_LIST -->
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