lib/contrast/utils/ruby_ast_rewriter.rb in contrast-agent-4.9.1 vs lib/contrast/utils/ruby_ast_rewriter.rb in contrast-agent-4.10.0

- old
+ new

@@ -34,39 +34,42 @@ # This overloads the on_dstr method in Parser::AST::Processor to handle # the replace within the given node. def on_dstr node return if node.children.all? { |child_node| child_node.type == :str } - new_content = +'(' - node.children.each_with_index do |child_node, index| + idx = 0 + while idx < node.children.size + #node.children.each_with_index do |child_node, index| + child_node = node.children[idx] # A begin node looks like #{some_code} in ruby, we do a substring # from [2...-1] to get rid of the #{ & trailing }. if child_node.type == :begin code = child_node. - location. - expression. - source_buffer. - source[child_node.location.begin.begin_pos...child_node.location.end.end_pos] + location. + expression. + source_buffer. + source[child_node.location.begin.begin_pos...child_node.location.end.end_pos] code = code[2...-1] new_content += "((#{ code }).to_s)" - # For interpolations that use class, instance, or global variables, - # those are NOT within a begin block, but instead are a ivar, cvar, - # or gvar node, not stripping of interpolation markers required. + # For interpolations that use class, instance, or global variables, + # those are NOT within a begin block, but instead are a ivar, cvar, + # or gvar node, not stripping of interpolation markers required. elsif VARIABLES.include?(child_node.type) variable = child_node.children.first new_content << "((#{ variable }).to_s)" - # When interpolation includes strings before or after an - # interpolation they are simple :str nodes, in order to preserve - # escaping we need to do a dump of the string value. + # When interpolation includes strings before or after an + # interpolation they are simple :str nodes, in order to preserve + # escaping we need to do a dump of the string value. elsif child_node.type == :str literal_value = child_node.children.first literal_value = literal_value.dump[1...-1] new_content << "\"#{ literal_value }\"" end - new_content << ' + ' unless index == node.children.length - 1 + new_content << ' + ' unless idx == node.children.length - 1 + idx += 1 end new_content << ')' if node.location.cs__respond_to?(:heredoc_body) replace(node.location.heredoc_body, new_content) else