提交 a5450386 编写于 作者: J Justin

Merge pull request #682 from presidentbeef/make_array_new_into_an_array

Make Array.new into an array and Hash.new into a hash
......@@ -74,6 +74,9 @@ class Brakeman::AliasProcessor < Brakeman::SexpProcessor
result
end
ARRAY_CONST = s(:const, :Array)
HASH_CONST = s(:const, :Hash)
#Process a method call.
def process_call exp
target_var = exp.target
......@@ -96,6 +99,10 @@ class Brakeman::AliasProcessor < Brakeman::SexpProcessor
if node_type? target, :or and [:+, :-, :*, :/].include? method
res = process_or_simple_operation(exp)
return res if res
elsif target == ARRAY_CONST and method == :new
return Sexp.new(:array, *exp.args)
elsif target == HASH_CONST and method == :new and first_arg.nil? and !node_type?(@exp_context.last, :iter, :call_with_block)
return Sexp.new(:hash)
end
#See if it is possible to simplify some basic cases
......@@ -187,7 +194,9 @@ class Brakeman::AliasProcessor < Brakeman::SexpProcessor
end
def process_call_with_block exp
@exp_context.push exp
exp[1] = process exp.block_call
@exp_context.pop
env.scope do
exp.block_args.each do |e|
......
......@@ -108,6 +108,22 @@ class AliasProcessorTests < Test::Unit::TestCase
RUBY
end
def test_array_new_append
assert_alias '[1, 2, 3]', <<-RUBY
x = Array.new
x << 1 << 2 << 3
x
RUBY
end
def test_array_new_init_append
assert_alias '[1, 2, 3]', <<-RUBY
x = Array.new(1)
x << 2 << 3
x
RUBY
end
def test_hash_index
assert_alias "'You say goodbye, I say :hello'", <<-RUBY
x = {:goodbye => "goodbye cruel world" }
......@@ -117,6 +133,15 @@ class AliasProcessorTests < Test::Unit::TestCase
RUBY
end
def test_hash_new_index
assert_alias "'You say goodbye, I say :hello'", <<-RUBY
x = Hash.new
x[:hello] = "hello world"
x.merge! :goodbye => "You say goodbye, I say :hello"
x[:goodbye]
RUBY
end
def test_obvious_if
assert_alias "'Yes!'", <<-RUBY
condition = true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册