Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
Brakeman
提交
16fd2dd9
B
Brakeman
项目概览
李少辉-开发者
/
Brakeman
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
Brakeman
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
16fd2dd9
编写于
3月 23, 2015
作者:
J
Justin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #640 from presidentbeef/handle_array_include_guard
Special case `if [...].include? x`
上级
18e75b62
32a7bc62
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
61 addition
and
1 deletion
+61
-1
lib/brakeman/processors/alias_processor.rb
lib/brakeman/processors/alias_processor.rb
+24
-1
test/apps/rails4/app/models/user.rb
test/apps/rails4/app/models/user.rb
+11
-0
test/tests/alias_processor.rb
test/tests/alias_processor.rb
+26
-0
未找到文件。
lib/brakeman/processors/alias_processor.rb
浏览文件 @
16fd2dd9
...
...
@@ -465,6 +465,19 @@ class Brakeman::AliasProcessor < Brakeman::SexpProcessor
exp
end
# Check if exp is a call to Array#include? on an array literal
# that contains all literal values. For example:
#
# [1, 2, "a"].include? x
#
def
array_include_all_literals?
exp
call?
exp
and
exp
.
method
==
:include?
and
node_type?
exp
.
target
,
:array
and
exp
.
target
.
length
>
1
and
exp
.
target
.
all?
{
|
e
|
e
.
is_a?
Symbol
or
node_type?
e
,
:lit
,
:str
}
end
#Sets @inside_if = true
def
process_if
exp
if
@ignore_ifs
.
nil?
...
...
@@ -498,7 +511,17 @@ class Brakeman::AliasProcessor < Brakeman::SexpProcessor
scope
do
@branch_env
=
env
.
current
branch_index
=
2
+
i
# s(:if, condition, then_branch, else_branch)
exp
[
branch_index
]
=
process_if_branch
branch
if
i
==
0
and
array_include_all_literals?
condition
# If the condition is ["a", "b"].include? x
# set x to "a" inside the true branch
var
=
condition
.
first_arg
previous_value
=
env
.
current
[
var
]
env
.
current
[
var
]
=
condition
.
target
[
1
]
exp
[
branch_index
]
=
process_if_branch
branch
env
.
current
[
var
]
=
previous_value
else
exp
[
branch_index
]
=
process_if_branch
branch
end
branch_scopes
<<
env
.
current
@branch_env
=
nil
end
...
...
test/apps/rails4/app/models/user.rb
浏览文件 @
16fd2dd9
...
...
@@ -12,4 +12,15 @@ class User < ActiveRecord::Base
def
symbol_stuff
self
.
where
(
User
.
table_name
.
to_sym
)
end
scope
:sorted_by
,
->
(
field
,
asc
)
{
asc
=
[
'desc'
,
'asc'
].
include?
(
asc
)
?
asc
:
'asc'
ordering
=
if
field
==
'extension'
"substring_index(
#{
table_name
}
.data_file_name, '.', -1)
#{
asc
}
"
elsif
SORTABLE_COLUMNS
.
include?
(
field
)
{
field
.
to_sym
=>
asc
.
to_sym
}
end
order
(
ordering
)
# should not warn about `asc` interpolation
}
end
test/tests/alias_processor.rb
浏览文件 @
16fd2dd9
...
...
@@ -640,4 +640,30 @@ class AliasProcessorTests < Test::Unit::TestCase
x
INPUT
end
def
test_branch_array_include
assert_alias
'x'
,
<<-
INPUT
if [1,2,3].include? x
stuff
end
x
INPUT
assert_output
<<-
INPUT
,
<<-
OUTPUT
if [1,2,3].include? x
y = x + 2
p y
end
x
INPUT
if [1,2,3].include? x
y = 3
p 3
end
x
OUTPUT
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录