-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Solidus/SpreeDefaultAddressDeprecated cop
- Loading branch information
Showing
6 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
lib/rubocop/cop/solidus/spree_default_address_deprecated.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Solidus | ||
# This cop finds user.default_address or user.default_address and suggest | ||
# using user.ship_address or user.default_user_ship_address instead. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# user.default_address | ||
# user.default_user_address | ||
# | ||
# # good | ||
# user.ship_address | ||
# user.default_user_ship_address | ||
# | ||
class SpreeDefaultAddressDeprecated < Base | ||
include TargetSolidusVersion | ||
minimum_solidus_version 2.11 | ||
|
||
MSG = 'user.default_address and user.default_user_address are deprecated and will be removed on Solidus 3.0. ' \ | ||
'Please use user.ship_address or user.default_user_ship_address instead.' | ||
|
||
def_node_matcher :default_address?, <<~PATTERN | ||
(send ... :default_address) | ||
PATTERN | ||
|
||
def_node_matcher :default_user_address?, <<~PATTERN | ||
(send ... :default_user_address) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless default_address?(node) || default_user_address?(node) | ||
|
||
add_offense(node) do |corrector| | ||
corrector.replace(node, node.source.gsub('default_address', 'ship_address')) if default_address?(node) | ||
corrector.replace(node, node.source.gsub('default_user_address', 'default_user_ship_address')) if default_user_address?(node) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
spec/rubocop/cop/solidus/spree_default_address_deprecated_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Solidus::SpreeDefaultAddressDeprecated, :config do | ||
it 'registers an offense when using `#.default_address`' do | ||
expect_offense(<<~RUBY) | ||
user.default_address | ||
^^^^^^^^^^^^^^^^^^^^ user.default_address and user.default_user_address are deprecated and will be removed on Solidus 3.0. Please use user.ship_address or user.default_user_ship_address instead. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#ship_address`' do | ||
expect_no_offenses(<<~RUBY) | ||
user.ship_address | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `#.default_user_address`' do | ||
expect_offense(<<~RUBY) | ||
user.default_user_address | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ user.default_address and user.default_user_address are deprecated and will be removed on Solidus 3.0. Please use user.ship_address or user.default_user_ship_address instead. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#default_user_ship_address`' do | ||
expect_no_offenses(<<~RUBY) | ||
user.default_user_ship_address | ||
RUBY | ||
end | ||
end |