Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
873b4055
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
873b4055
编写于
7月 04, 2017
作者:
D
Douwe Maan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add ProjectPathHelper cop
上级
fe13f110
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
93 addition
and
0 deletion
+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
未找到文件。
rubocop/cop/project_path_helper.rb
0 → 100644
浏览文件 @
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
浏览文件 @
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
浏览文件 @
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
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录