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
873b4055
Commit
873b4055
authored
Jul 04, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ProjectPathHelper cop
parent
fe13f110
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
93 additions
and
0 deletions
+93
-0
rubocop/cop/project_path_helper.rb
rubocop/cop/project_path_helper.rb
+51
-0
rubocop/rubocop.rb
rubocop/rubocop.rb
+1
-0
spec/rubocop/cop/project_path_helper_spec.rb
spec/rubocop/cop/project_path_helper_spec.rb
+41
-0
No files found.
rubocop/cop/project_path_helper.rb
0 → 100644
View file @
873b4055
module
RuboCop
module
Cop
class
ProjectPathHelper
<
RuboCop
::
Cop
::
Cop
MSG
=
'Use short project path helpers without explicitly passing the namespace: '
\
'`foo_project_bar_path(project, bar)` instead of '
\
'`foo_namespace_project_bar_path(project.namespace, project, bar)`.'
.
freeze
METHOD_NAME_PATTERN
=
/\A([a-z_]+_)?namespace_project(?:_[a-z_]+)?_(?:url|path)\z/
.
freeze
def
on_send
(
node
)
return
unless
method_name
(
node
).
to_s
=~
METHOD_NAME_PATTERN
namespace_expr
,
project_expr
=
arguments
(
node
)
return
unless
namespace_expr
&&
project_expr
return
unless
namespace_expr
.
type
==
:send
return
unless
method_name
(
namespace_expr
)
==
:namespace
return
unless
receiver
(
namespace_expr
)
==
project_expr
add_offense
(
node
,
:selector
)
end
def
autocorrect
(
node
)
helper_name
=
method_name
(
node
).
to_s
.
sub
(
'namespace_project'
,
'project'
)
arguments
=
arguments
(
node
)
arguments
.
shift
# Remove namespace argument
replacement
=
"
#{
helper_name
}
(
#{
arguments
.
map
(
&
:source
).
join
(
', '
)
}
)"
lambda
do
|
corrector
|
corrector
.
replace
(
node
.
source_range
,
replacement
)
end
end
private
def
receiver
(
node
)
node
.
children
[
0
]
end
def
method_name
(
node
)
node
.
children
[
1
]
end
def
arguments
(
node
)
node
.
children
[
2
..-
1
]
end
end
end
end
rubocop/rubocop.rb
View file @
873b4055
...
...
@@ -3,6 +3,7 @@ require_relative 'cop/gem_fetcher'
require_relative
'cop/activerecord_serialize'
require_relative
'cop/redirect_with_status'
require_relative
'cop/polymorphic_associations'
require_relative
'cop/project_path_helper'
require_relative
'cop/migration/add_column'
require_relative
'cop/migration/add_column_with_default_to_large_table'
require_relative
'cop/migration/add_concurrent_foreign_key'
...
...
spec/rubocop/cop/project_path_helper_spec.rb
0 → 100644
View file @
873b4055
require
'spec_helper'
require
'rubocop'
require
'rubocop/rspec/support'
require_relative
'../../../rubocop/cop/project_path_helper'
describe
RuboCop
::
Cop
::
ProjectPathHelper
do
include
CopHelper
subject
(
:cop
)
{
described_class
.
new
}
context
"when using namespace_project with the project's namespace"
do
let
(
:source
)
{
'edit_namespace_project_issue_path(@issue.project.namespace, @issue.project, @issue)'
}
let
(
:correct_source
)
{
'edit_project_issue_path(@issue.project, @issue)'
}
it
'registers an offense'
do
inspect_source
(
cop
,
source
)
aggregate_failures
do
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
expect
(
cop
.
offenses
.
map
(
&
:line
)).
to
eq
([
1
])
expect
(
cop
.
highlights
).
to
eq
([
'edit_namespace_project_issue_path'
])
end
end
it
'autocorrects to the right version'
do
autocorrected
=
autocorrect_source
(
cop
,
source
)
expect
(
autocorrected
).
to
eq
(
correct_source
)
end
end
context
'when using namespace_project with a different namespace'
do
it
'registers no offense'
do
inspect_source
(
cop
,
'edit_namespace_project_issue_path(namespace, project)'
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
0
)
end
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