-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sebastian Welther
committed
Dec 1, 2023
1 parent
9d51eb0
commit 6345386
Showing
8 changed files
with
103 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks that `remove_const` is not used in specs. | ||
# | ||
# @example | ||
# # bad | ||
# it 'does something' do | ||
# Object.send(:remove_const, :SomeConstant) | ||
# end | ||
# | ||
# before do | ||
# Object.send(:remove_const, :SomeConstant) | ||
# end | ||
# | ||
class RemoveConst < Base | ||
include RuboCop::RSpec::Language | ||
extend RuboCop::RSpec::Language::NodePattern | ||
|
||
MSG = 'Do not use remove_const in specs. ' \ | ||
'Consider using e.g. `stub_const`.' | ||
|
||
# @!method remove_const(node) | ||
def_node_matcher :remove_const, <<~PATTERN | ||
(send Object :send (sym :remove_const) _) | ||
PATTERN | ||
|
||
# Check for offenses | ||
def on_send(node) | ||
remove_const(node) do | ||
add_offense(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
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::RemoveConst do | ||
it 'detects the `remove_const` usage' do | ||
expect_offense(<<-RUBY) | ||
it 'does something' do | ||
Object.send(:remove_const, :SomeConstant) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use remove_const in specs. Consider using e.g. `stub_const`. | ||
end | ||
RUBY | ||
|
||
expect_offense(<<-RUBY) | ||
before do | ||
Object.send(:remove_const, :SomeConstant) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use remove_const in specs. Consider using e.g. `stub_const`. | ||
end | ||
RUBY | ||
end | ||
end |