Skip to content
This repository was archived by the owner on Apr 23, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions lib/ruby_ast_gen/erb_to_ruby_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def initialize
@parser = Temple::ERB::Parser.new
@in_control_block = false
@output_tmp_var = "self.joern__buffer"
@output_tmp_append_func = "self.joern__buffer_append"
@in_do_block = false
@inner_buffer = "joern__inner_buffer"
@current_counter = 0
Expand Down Expand Up @@ -70,7 +71,7 @@ def visit(node)
@output << "if #{if_cond}"
@output << "#{template_call}(#{if_body})" if if_body
@output << "else" if else_body
@output << "#{@output_tmp_var} << #{template_call}(#{else_body})" if else_body
@output << "#{@output_tmp_append_func}(#{@output_tmp_var}, #{template_call}(#{else_body}))" if else_body
else
if code.include?(" if ")
call, if_cond = code.split(" if ")
Expand All @@ -84,7 +85,7 @@ def visit(node)
else
"joern__template_out_escape"
end
@output << "#{@output_tmp_var} << #{template_call}(#{call})"
@output << "#{@output_tmp_append_func}(#{@output_tmp_var}, #{template_call}(#{call}))"
end
@output << "end"
else
Expand All @@ -93,7 +94,7 @@ def visit(node)
else
"joern__template_out_escape"
end
@output << "#{@inner_buffer} << #{template_call}(#{code})"
@output << "#{@output_tmp_append_func}(#{@inner_buffer}, #{template_call}(#{code}))"
end
end
elsif is_do_block(code)
Expand All @@ -105,14 +106,14 @@ def visit(node)
else
"joern__template_out_escape"
end
@output << "#{@inner_buffer} << #{template_call}(#{code})"
@output << "#{@output_tmp_append_func}(#{@inner_buffer}, #{template_call}(#{code}))"
else
template_call = if escape_enabled then
"joern__template_out_raw"
else
"joern__template_out_escape"
end
@output << "#{@output_tmp_var} << #{template_call}(#{code})"
@output << "#{@output_tmp_append_func}(#{@output_tmp_var}, #{template_call}(#{code}))"
end
when :code
flush_static_block
Expand All @@ -131,7 +132,7 @@ def visit(node)
@in_do_block = false
@output << "#{@inner_buffer}"
@output << "end"
@output << "#{@output_tmp_var} << #{current_lambda}.call(#{@current_lambda_vars})"
@output << "#{@output_tmp_append_func}(#{@output_tmp_var}, #{current_lambda}.call(#{@current_lambda_vars}))"
else
@in_control_block = false
@output << "end"
Expand Down Expand Up @@ -169,7 +170,7 @@ def flush_static_block()
else
@output_tmp_var
end
@output << "#{buffer_to_use} << \"#{@static_buff.join('\n').gsub(/(?<!\\)"/, '')}\""
@output << "#{@output_tmp_append_func}(#{buffer_to_use}, \"#{@static_buff.join('\n').gsub(/(?<!\\)"/, '')}\")"
@static_buff = [] # clear static buffer
end
end
Expand All @@ -186,10 +187,10 @@ def lower_do_block(code)
call = extract_code_snippet(ast.children[0].location, code) if ast.children[0]
args = extract_code_snippet(ast.children[1].location, code) if ast.children[1]
body = extract_code_snippet(ast.children[2].location, code) if ast.children[2]
@output << "#{@output_tmp_var} << #{call}"
@output << "#{@output_tmp_append_func}(#{@output_tmp_var}, #{call})"
@output << "#{lambda_incrementor} = lambda do |#{args if args}|"
@output << "#{@inner_buffer} = \"\""
@output << "#{@inner_buffer} << #{body}"
@output << "#{@output_tmp_append_func}(#{@inner_buffer}, #{body})"
@output << "end"
else
code
Expand All @@ -206,7 +207,7 @@ def lower_do_block(code)
elsif rest != nil && !rest.start_with?('(') && !rest.end_with?(')')
method_call = "#{call_name}(#{rest})"
end
@output << "#{@output_tmp_var} << #{method_call}"
@output << "#{@output_tmp_append_func}(#{@output_tmp_var}, #{method_call})"
end
@in_do_block = true
@output << "#{lambda_incrementor} = lambda do |#{@current_lambda_vars}|"
Expand Down Expand Up @@ -234,7 +235,3 @@ def extract_ast(code)
ruby_parser.parse(parser_buffer)
end
end




15 changes: 7 additions & 8 deletions spec/ruby_ast_gen_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def foo(x)
expect(ast).not_to be_nil
end

it "should create a function with a keyword option argument sucessfully" do
it "should create a function with a keyword option argument successfully" do
code(<<-CODE)
def foo(a, bar: "default")
puts(bar)
Expand Down Expand Up @@ -162,16 +162,15 @@ def foo(a, bar: "default")
file_content = File.read(temp_erb_file.path)
code = RubyAstGen::get_erb_content(file_content)
expected = <<-HEREDOC
joern__buffer = ""
joern__buffer << form_with(url: some_url)
self.joern__buffer = ""
self.joern__buffer_append(self.joern__buffer, form_with(url: some_url))
rails_lambda_0 = lambda do |form|
joern__inner_buffer = ""
joern__inner_buffer << joern__template_out_escape(form.text_field :name)
joern_inner_buffer
self.joern__buffer_append(joern__inner_buffer, joern__template_out_escape( form.text_field :name ))
joern__inner_buffer
end
joern__buffer << rails_lambda_0.call(form)
return joern__buffer
self.joern__buffer_append(self.joern__buffer, rails_lambda_0.call(form))
HEREDOC
expect(code).equal?(expected)
expect(code).to eq(expected)
end
end