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
Hide 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
...
@@ -29,14 +29,10 @@ module RuboCop
MESSAGE_ALLOW
=
'Do not use `allow_any_instance_of` method, use `allow_next_instance_of` instead.'
MESSAGE_ALLOW
=
'Do not use `allow_any_instance_of` method, use `allow_next_instance_of` instead.'
def_node_search
:expect_any_instance_of?
,
<<~
PATTERN
def_node_search
:expect_any_instance_of?
,
<<~
PATTERN
(send
(send (send nil? :expect_any_instance_of ...) ...)
(send nil? :expect_any_instance_of ...) _ ...
)
PATTERN
PATTERN
def_node_search
:allow_any_instance_of?
,
<<~
PATTERN
def_node_search
:allow_any_instance_of?
,
<<~
PATTERN
(send
(send (send nil? :allow_any_instance_of ...) ...)
(send nil? :allow_any_instance_of ...) _ ...
)
PATTERN
PATTERN
def
on_send
(
node
)
def
on_send
(
node
)
...
@@ -48,11 +44,12 @@ module RuboCop
...
@@ -48,11 +44,12 @@ module RuboCop
end
end
def
autocorrect
(
node
)
def
autocorrect
(
node
)
if
expect_any_instance_of?
(
node
)
replacement
=
replacement
=
replacement_any_instance_of
(
node
,
'expect'
)
if
expect_any_instance_of?
(
node
)
elsif
allow_any_instance_of?
(
node
)
replacement_any_instance_of
(
node
,
'expect'
)
replacement
=
replacement_any_instance_of
(
node
,
'allow'
)
elsif
allow_any_instance_of?
(
node
)
end
replacement_any_instance_of
(
node
,
'allow'
)
end
lambda
do
|
corrector
|
lambda
do
|
corrector
|
corrector
.
replace
(
node
.
loc
.
expression
,
replacement
)
corrector
.
replace
(
node
.
loc
.
expression
,
replacement
)
...
@@ -62,10 +59,18 @@ module RuboCop
...
@@ -62,10 +59,18 @@ module RuboCop
private
private
def
replacement_any_instance_of
(
node
,
rspec_prefix
)
def
replacement_any_instance_of
(
node
,
rspec_prefix
)
replacement
=
node
.
receiver
.
source
.
sub
(
"
#{
rspec_prefix
}
_any_instance_of"
,
"
#{
rspec_prefix
}
_next_instance_of"
)
method_call
=
replacement
<<
" do |instance|
\n
"
node
.
receiver
.
source
.
sub
(
replacement
<<
"
#{
rspec_prefix
}
(instance).
#{
node
.
method_name
}
#{
node
.
children
.
last
.
source
}
\n
"
"
#{
rspec_prefix
}
_any_instance_of"
,
replacement
<<
'end'
"
#{
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
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
...
@@ -25,6 +25,7 @@ describe RuboCop::Cop::RSpec::AnyInstanceOf do
it
'registers an offence'
do
it
'registers an offence'
do
inspect_source
(
source
)
inspect_source
(
source
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
end
end
...
@@ -49,6 +50,7 @@ describe RuboCop::Cop::RSpec::AnyInstanceOf do
...
@@ -49,6 +50,7 @@ describe RuboCop::Cop::RSpec::AnyInstanceOf do
it
'registers an offence'
do
it
'registers an offence'
do
inspect_source
(
source
)
inspect_source
(
source
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
end
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