Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
Brakeman
提交
83dc2d9b
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,体验更适合开发者的 AI 搜索 >>
提交
83dc2d9b
编写于
9月 13, 2012
作者:
J
Justin Collins
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'treat_super_as_method_call'
Conflicts: test/tests/test_brakeman.rb
上级
ee7d87e6
fa9da064
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
283 addition
and
34 deletion
+283
-34
lib/ruby_parser/bm_sexp.rb
lib/ruby_parser/bm_sexp.rb
+37
-12
test/tests/test_brakeman.rb
test/tests/test_brakeman.rb
+0
-22
test/tests/test_sexp.rb
test/tests/test_sexp.rb
+246
-0
未找到文件。
lib/ruby_parser/bm_sexp.rb
浏览文件 @
83dc2d9b
...
...
@@ -147,8 +147,14 @@ class Sexp
#s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1)))
# ^- method
def
method
expect
:call
,
:attrasgn
self
[
2
]
expect
:call
,
:attrasgn
,
:super
,
:zsuper
case
self
.
node_type
when
:call
,
:attrasgn
self
[
2
]
when
:super
,
:zsuper
:super
end
end
#Sets the arglist in a method call.
...
...
@@ -165,8 +171,18 @@ class Sexp
# s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1), s(:lit, 2)))
# ^------------ arglist ------------^
def
arglist
expect
:call
,
:attrasgn
self
[
3
]
expect
:call
,
:attrasgn
,
:super
,
:zsuper
case
self
.
node_type
when
:call
,
:attrasgn
self
[
3
]
when
:super
,
:zsuper
if
self
[
1
]
Sexp
.
new
(
:arglist
).
concat
self
[
1
..-
1
]
else
Sexp
.
new
(
:arglist
)
end
end
#For new ruby_parser
#Sexp.new(:arglist, *self[3..-1])
...
...
@@ -177,7 +193,7 @@ class Sexp
# s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1), s(:lit, 2)))
# ^--------args--------^
def
args
expect
:call
,
:attrasgn
expect
:call
,
:attrasgn
,
:super
,
:zsuper
#For new ruby_parser
#if self[3]
# self[3..-1]
...
...
@@ -185,11 +201,20 @@ class Sexp
# []
#end
#For old ruby_parser
if
self
[
3
]
self
[
3
][
1
..-
1
]
else
[]
case
self
.
node_type
when
:call
,
:attrasgn
#For old ruby_parser
if
self
[
3
]
self
[
3
][
1
..-
1
]
else
[]
end
when
:super
,
:zsuper
if
self
[
1
]
self
[
1
..-
1
]
else
[]
end
end
end
...
...
@@ -345,7 +370,7 @@ class Sexp
#Sets body
def
body
=
exp
expect
:defn
,
:defs
,
:methdef
,
:selfdef
,
:class
,
:module
case
self
.
node_type
when
:defn
,
:methdef
,
:class
self
[
3
]
=
exp
...
...
@@ -392,7 +417,7 @@ end
[
:[]=
,
:clear
,
:collect!
,
:compact!
,
:concat
,
:delete
,
:delete_at
,
:delete_if
,
:drop
,
:drop_while
,
:fill
,
:flatten!
,
:replace
,
:insert
,
:keep_if
,
:map!
,
:pop
,
:push
,
:reject!
,
:replace
,
:reverse!
,
:rotate!
,
:select!
,
:shift
,
:shuffle!
,
:slice!
,
:sort!
,
:sort_by!
,
:transpose
,
:select!
,
:shift
,
:shuffle!
,
:slice!
,
:sort!
,
:sort_by!
,
:transpose
,
:uniq!
,
:unshift
].
each
do
|
method
|
Sexp
.
class_eval
<<-
RUBY
...
...
test/tests/test_brakeman.rb
浏览文件 @
83dc2d9b
...
...
@@ -20,28 +20,6 @@ class UtilTests < Test::Unit::TestCase
end
end
class
SexpTests
<
Test
::
Unit
::
TestCase
def
setup
if
RUBY_VERSION
=~
/^1\.9/
@ruby_parser
=
Ruby19Parser
else
@ruby_parser
=
RubyParser
end
end
def
parse
src
@ruby_parser
.
new
.
parse
src
end
def
test_sexp_call
call
=
parse
"x()"
assert_equal
call
.
method
,
:x
assert_nil
call
.
target
assert_equal
call
.
args
,
Sexp
.
new
()
end
end
class
BaseCheckTests
<
Test
::
Unit
::
TestCase
FakeTracker
=
Struct
.
new
(
:config
)
...
...
test/tests/test_sexp.rb
0 → 100644
浏览文件 @
83dc2d9b
require
'brakeman/processors/base_processor'
class
SexpTests
<
Test
::
Unit
::
TestCase
def
setup
if
RUBY_VERSION
[
/^1\.9/
]
@ruby_parser
=
::
Ruby19Parser
else
@ruby_parser
=
::
RubyParser
end
end
def
parse
string
Brakeman
::
BaseProcessor
.
new
(
nil
).
process
@ruby_parser
.
new
.
parse
string
end
def
test_method_call_with_no_args
exp
=
parse
"x.y"
assert_equal
s
(
:call
,
nil
,
:x
,
s
(
:arglist
)),
exp
.
target
assert_equal
:y
,
exp
.
method
assert_equal
s
(),
exp
.
args
assert_equal
s
(
:arglist
),
exp
.
arglist
assert_nil
exp
.
first_arg
assert_nil
exp
.
second_arg
end
def
test_method_call_with_args
exp
=
parse
'x.y(1, 2, 3)'
assert_equal
s
(
:call
,
nil
,
:x
,
s
(
:arglist
)),
exp
.
target
assert_equal
:y
,
exp
.
method
assert_equal
s
(
s
(
:lit
,
1
),
s
(
:lit
,
2
),
s
(
:lit
,
3
)),
exp
.
args
assert_equal
s
(
:arglist
,
s
(
:lit
,
1
),
s
(
:lit
,
2
),
s
(
:lit
,
3
)),
exp
.
arglist
assert_equal
s
(
:lit
,
1
),
exp
.
first_arg
assert_equal
s
(
:lit
,
2
),
exp
.
second_arg
end
def
test_method_call_no_target
exp
=
parse
'x 1, 2, 3'
assert_nil
exp
.
target
assert_equal
:x
,
exp
.
method
assert_equal
s
(
s
(
:lit
,
1
),
s
(
:lit
,
2
),
s
(
:lit
,
3
)),
exp
.
args
assert_equal
s
(
:arglist
,
s
(
:lit
,
1
),
s
(
:lit
,
2
),
s
(
:lit
,
3
)),
exp
.
arglist
assert_equal
s
(
:lit
,
1
),
exp
.
first_arg
assert_equal
s
(
:lit
,
2
),
exp
.
second_arg
end
def
test_method_call_set_target
exp
=
parse
'x.y'
exp
.
target
=
:z
assert_equal
:z
,
exp
.
target
end
def
test_method_call_set_args
exp
=
parse
'x.y'
exp
.
arglist
=
s
(
:arglist
,
s
(
:lit
,
1
),
s
(
:lit
,
2
))
assert_equal
s
(
:lit
,
1
),
exp
.
first_arg
assert_equal
s
(
:lit
,
2
),
exp
.
second_arg
assert_equal
s
(
:arglist
,
s
(
:lit
,
1
),
s
(
:lit
,
2
)),
exp
.
arglist
assert_equal
s
(
s
(
:lit
,
1
),
s
(
:lit
,
2
)),
exp
.
args
end
def
test_method_call_with_block
exp
=
parse
"x do |z|; blah z; end"
block
=
exp
.
block
call
=
exp
.
block_call
args
=
exp
.
block_args
assert_equal
s
(
:call
,
nil
,
:x
,
s
(
:arglist
)),
call
assert_equal
s
(
:lasgn
,
:z
),
args
assert_equal
s
(
:call
,
nil
,
:blah
,
s
(
:arglist
,
s
(
:lvar
,
:z
))),
block
end
def
test_or
exp
=
parse
'1 or 2'
assert_equal
s
(
:lit
,
1
),
exp
.
lhs
assert_equal
s
(
:lit
,
2
),
exp
.
rhs
end
def
test_and
exp
=
parse
'1 and 2'
assert_equal
s
(
:lit
,
1
),
exp
.
lhs
assert_equal
s
(
:lit
,
2
),
exp
.
rhs
end
def
test_if_expression
exp
=
parse
<<-
RUBY
if x
y
else
z
end
RUBY
assert_equal
s
(
:call
,
nil
,
:x
,
s
(
:arglist
)),
exp
.
condition
assert_equal
s
(
:call
,
nil
,
:y
,
s
(
:arglist
)),
exp
.
then_clause
assert_equal
s
(
:call
,
nil
,
:z
,
s
(
:arglist
)),
exp
.
else_clause
end
def
test_local_assignment
exp
=
parse
'x = 1'
assert_equal
:x
,
exp
.
lhs
assert_equal
s
(
:lit
,
1
),
exp
.
rhs
end
def
test_instance_assignment
exp
=
parse
'@x = 1'
assert_equal
:@x
,
exp
.
lhs
assert_equal
s
(
:lit
,
1
),
exp
.
rhs
end
def
test_global_assignment
exp
=
parse
'$x = 1'
assert_equal
:
$x
,
exp
.
lhs
assert_equal
s
(
:lit
,
1
),
exp
.
rhs
end
def
test_constant_assignment
exp
=
parse
'X = 1'
assert_equal
:X
,
exp
.
lhs
assert_equal
s
(
:lit
,
1
),
exp
.
rhs
end
def
test_class_variable_assignment
exp
=
parse
'@@x = 1'
assert_equal
:@@x
,
exp
.
lhs
assert_equal
s
(
:lit
,
1
),
exp
.
rhs
end
def
test_method_def_name
exp
=
parse
<<-
RUBY
def x(y)
z
y
end
RUBY
assert_equal
:x
,
exp
.
method_name
end
def
test_method_self_def_name
exp
=
parse
<<-
RUBY
def self.x(y)
z
y
end
RUBY
assert_equal
:x
,
exp
.
method_name
end
def
test_method_def_body
exp
=
parse
<<-
RUBY
def x(y)
z
y
end
RUBY
assert_equal
s
(
:scope
,
s
(
:rlist
,
s
(
:call
,
nil
,
:z
,
s
(
:arglist
)),
s
(
:lvar
,
:y
))),
exp
.
body
end
def
test_method_def_body_single_line
exp
=
parse
<<-
RUBY
def x(y)
y
end
RUBY
assert_equal
s
(
:scope
,
s
(
:rlist
,
s
(
:lvar
,
:y
))),
exp
.
body
end
def
test_class_body
exp
=
parse
<<-
RUBY
class X
def y
end
end
RUBY
assert_equal
s
(
:scope
,
s
(
:defn
,
:y
,
s
(
:args
),
s
(
:scope
,
s
(
:block
,
s
(
:nil
))))),
exp
.
body
end
def
test_module_body
exp
=
parse
<<-
RUBY
module X
def y
end
end
RUBY
assert_equal
s
(
:scope
,
s
(
:defn
,
:y
,
s
(
:args
),
s
(
:scope
,
s
(
:block
,
s
(
:nil
))))),
exp
.
body
end
def
test_class_name
exp
=
parse
'class X < Y; end'
assert_equal
:X
,
exp
.
class_name
end
def
test_parent_name
exp
=
parse
'class X < Y; end'
assert_equal
s
(
:const
,
:Y
),
exp
.
parent_name
end
def
test_module_name
exp
=
parse
'module X; end'
assert_equal
:X
,
exp
.
module_name
end
def
test_wrong_sexp_error
exp
=
parse
'true ? false : true'
assert_raise
WrongSexpError
do
exp
.
method
end
end
def
test_zsuper_call
exp
=
parse
'super'
assert_equal
:super
,
exp
.
method
assert_equal
s
(
:arglist
),
exp
.
arglist
assert_equal
[],
exp
.
args
end
def
test_super_call
exp
=
parse
'super 1'
assert_equal
:super
,
exp
.
method
assert_equal
s
(
:arglist
,
s
(
:lit
,
1
)),
exp
.
arglist
assert_equal
s
(
s
(
:lit
,
1
)),
exp
.
args
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录