diff --git a/brakeman.gemspec b/brakeman.gemspec index 94019b0d15084e45884a7af75b656c4e4871f6d7..475e3648ddcb601931bf3b462f84b20621260299 100644 --- a/brakeman.gemspec +++ b/brakeman.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.executables = ["brakeman"] s.license = "MIT" s.add_dependency "ruby_parser", "~>3.1.1" - s.add_dependency "ruby2ruby", "2.0.3" + s.add_dependency "ruby2ruby", "~>2.0.5" s.add_dependency "terminal-table", "~>1.4" s.add_dependency "fastercsv", "~>1.5" s.add_dependency "highline", "~>1.6.19" diff --git a/lib/brakeman/processors/output_processor.rb b/lib/brakeman/processors/output_processor.rb index e4d042e5a6092351ef363a1c0b58a178ce142fc4..fa94b3fc09e3569542ee7654d32382f1f93749c5 100644 --- a/lib/brakeman/processors/output_processor.rb +++ b/lib/brakeman/processors/output_processor.rb @@ -14,7 +14,6 @@ class Brakeman::OutputProcessor < Ruby2Ruby end alias process_safely format - alias process_methdef process_defn def process exp begin @@ -99,6 +98,29 @@ class Brakeman::OutputProcessor < Ruby2Ruby out end + def process_defn exp + # Copied from Ruby2Ruby except without the whole + # "convert methods to attr_*" stuff + name = exp.shift + args = process exp.shift + args = "" if args == "()" + + exp.shift if exp == s(s(:nil)) # empty it out of a default nil expression + + body = [] + until exp.empty? do + body << indent(process(exp.shift)) + end + + body << indent("# do nothing") if body.empty? + + body = body.join("\n") + + return "def #{name}#{args}\n#{body}\nend".gsub(/\n\s*\n+/, "\n") + end + + alias process_methdef process_defn + def process_call_with_block exp call = process exp[0] block = process_rlist exp[2..-1] diff --git a/test/test.rb b/test/test.rb index 8dc70ae9f78092f488e3c741c4ae9001d369b9f0..046cfadcda29c9b5c949bf8f0260fcc597407738 100644 --- a/test/test.rb +++ b/test/test.rb @@ -191,7 +191,7 @@ module BrakemanTester::RescanTestHelper output = yield parsed File.open path, "w" do |f| - f.puts Ruby2Ruby.new.process output + f.puts Brakeman::OutputProcessor.new.process output end end diff --git a/test/tests/output_processor.rb b/test/tests/output_processor.rb index da65db6684f2586783aae447db948c55bd724303..a05ff352fc36b17c89e74af9031961bf348319f6 100644 --- a/test/tests/output_processor.rb +++ b/test/tests/output_processor.rb @@ -144,4 +144,21 @@ class OutputProcessorTests < Test::Unit::TestCase Sexp.new(:args), Sexp.new(:call, nil, :y)) end + + # Ruby2Ruby tries to convert some methods to attr_* calls, + # but it breaks some stuff because of how it accesses nodes. + # So we overwrite it. + def test_output_defn_not_attr + assert_output "def x\n @x\nend", + Sexp.new(:defn, + :x, + Sexp.new(:args), + Sexp.new(:ivar, :@x)) + + assert_output "def x(y)\n @x = (local y)\nend", + Sexp.new(:methdef, + :x, + Sexp.new(:args, :y), + Sexp.new(:iasgn, :@x, Sexp.new(:lvar, :y))) + end end