Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
9dff94db
Commit
9dff94db
authored
Nov 06, 2019
by
Arturo Herrero
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve code avoiding mutable state
parent
5e2b317f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
15 deletions
+22
-15
rubocop/cop/rspec/any_instance_of.rb
rubocop/cop/rspec/any_instance_of.rb
+20
-15
spec/rubocop/cop/rspec/any_instance_of_spec.rb
spec/rubocop/cop/rspec/any_instance_of_spec.rb
+2
-0
No files found.
rubocop/cop/rspec/any_instance_of.rb
View file @
9dff94db
...
...
@@ -29,14 +29,10 @@ module RuboCop
MESSAGE_ALLOW
=
'Do not use `allow_any_instance_of` method, use `allow_next_instance_of` instead.'
def_node_search
:expect_any_instance_of?
,
<<~
PATTERN
(send
(send nil? :expect_any_instance_of ...) _ ...
)
(send (send nil? :expect_any_instance_of ...) ...)
PATTERN
def_node_search
:allow_any_instance_of?
,
<<~
PATTERN
(send
(send nil? :allow_any_instance_of ...) _ ...
)
(send (send nil? :allow_any_instance_of ...) ...)
PATTERN
def
on_send
(
node
)
...
...
@@ -48,10 +44,11 @@ module RuboCop
end
def
autocorrect
(
node
)
replacement
=
if
expect_any_instance_of?
(
node
)
replacement
=
replacement_any_instance_of
(
node
,
'expect'
)
replacement_any_instance_of
(
node
,
'expect'
)
elsif
allow_any_instance_of?
(
node
)
replacement
=
replacement_any_instance_of
(
node
,
'allow'
)
replacement_any_instance_of
(
node
,
'allow'
)
end
lambda
do
|
corrector
|
...
...
@@ -62,10 +59,18 @@ module RuboCop
private
def
replacement_any_instance_of
(
node
,
rspec_prefix
)
replacement
=
node
.
receiver
.
source
.
sub
(
"
#{
rspec_prefix
}
_any_instance_of"
,
"
#{
rspec_prefix
}
_next_instance_of"
)
replacement
<<
" do |instance|
\n
"
replacement
<<
"
#{
rspec_prefix
}
(instance).
#{
node
.
method_name
}
#{
node
.
children
.
last
.
source
}
\n
"
replacement
<<
'end'
method_call
=
node
.
receiver
.
source
.
sub
(
"
#{
rspec_prefix
}
_any_instance_of"
,
"
#{
rspec_prefix
}
_next_instance_of"
)
block
=
<<~
RUBY
.
chomp
do |instance|
#{
rspec_prefix
}
(instance).
#{
node
.
method_name
}
#{
node
.
children
.
last
.
source
}
end
RUBY
"
#{
method_call
}
#{
block
}
"
end
end
end
...
...
spec/rubocop/cop/rspec/any_instance_of_spec.rb
View file @
9dff94db
...
...
@@ -25,6 +25,7 @@ describe RuboCop::Cop::RSpec::AnyInstanceOf do
it
'registers an offence'
do
inspect_source
(
source
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
end
...
...
@@ -49,6 +50,7 @@ describe RuboCop::Cop::RSpec::AnyInstanceOf do
it
'registers an offence'
do
inspect_source
(
source
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment