提交 6a96c550 编写于 作者: J Justin Collins

Convert simple `if` to `or` on assignment

上级 eb2888d3
...@@ -388,6 +388,8 @@ class Brakeman::AliasProcessor < Brakeman::SexpProcessor ...@@ -388,6 +388,8 @@ class Brakeman::AliasProcessor < Brakeman::SexpProcessor
exps = [exp.then_clause, exp.else_clause] exps = [exp.then_clause, exp.else_clause]
end end
exps.compact!
exps.each do |e| exps.each do |e|
@inside_if << [] unless no_branch or @ignore_ifs @inside_if << [] unless no_branch or @ignore_ifs
...@@ -619,11 +621,37 @@ class Brakeman::AliasProcessor < Brakeman::SexpProcessor ...@@ -619,11 +621,37 @@ class Brakeman::AliasProcessor < Brakeman::SexpProcessor
end end
end end
def value_from_if exp
if node_type? exp.else_clause, :block or node_type? exp.then_clause, :block
#If either clause is more than a single expression, just use entire
#if expression for now
exp
elsif exp.else_clause.nil?
exp.then_clause
elsif exp.then_clause.nil?
exp.else_clause
else
condition = exp.condition
if true? condition
exp.then_clause
elsif false? condition
exp.else_clause
else
Sexp.new(:or, exp.then_clause, exp.else_clause).line(exp.line)
end
end
end
#Set variable to given value. #Set variable to given value.
#Creates "branched" versions of values when appropriate. #Creates "branched" versions of values when appropriate.
#Avoids creating multiple branched versions inside same #Avoids creating multiple branched versions inside same
#if branch. #if branch.
def set_value var, value, line = nil def set_value var, value, line = nil
if node_type? value, :if
value = value_from_if(value)
end
unless @ignore_ifs unless @ignore_ifs
current_val = env[var] current_val = env[var]
current_if = @inside_if.last current_if = @inside_if.last
......
...@@ -223,4 +223,18 @@ class AliasProcessorTests < Test::Unit::TestCase ...@@ -223,4 +223,18 @@ class AliasProcessorTests < Test::Unit::TestCase
[w, x, y] [w, x, y]
RUBY RUBY
end end
def test_assignment_of_simple_if_expression
assert_alias "1 or 2", <<-RUBY
x = (test ? 1 : 2)
x
RUBY
end
def test_assignment_of_forced_if_expression
assert_alias "1", <<-RUBY
x = (true ? 1 : 2)
x
RUBY
end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册