class Parser::Rewriter


@api public
inserting/replacing code so you’ll have to do this yourself.
Keep in mind that {Parser::Rewriter} does not take care of indentation when
end
puts ‘hello’
while true
This would result in the following Ruby code:
puts rewriter.rewrite(buffer, ast)
# Rewrite the AST, returns a String with the new form.
rewriter = RemoveDo.new
ast = parser.parse(buffer)
parser = Parser::CurrentRuby.new
buffer.source = code
buffer = Parser::Source::Buffer.new(‘(example)’)
EOF
end
puts ‘hello’
while true do
code = <<-EOF
end
end
end
remove(node.location.begin)
if node.location.begin.is?(‘do’)
# Check if the statement starts with “do”
def on_while(node)
class RemoveDo < Parser::Rewriter
You can do this as following:
For example, assume you want to remove ‘do` tokens from a while statement.
{Parser::Source::Rewriter}.
existing ASTs. It’s built on top of {Parser::AST::Processor} and
{Parser::Rewriter} offers a basic API that makes it easy to rewrite
#

def assignment?(node)

Returns:
  • (TrueClass|FalseClass) -

Parameters:
  • node (Parser::AST::Node) --
def assignment?(node)
  [:lvasgn, :ivasgn, :gvasgn, :cvasgn, :casgn].include?(node.type)
end

def insert_after(range, content)

Parameters:
  • content (String) --
  • range (Parser::Source::Range) --
def insert_after(range, content)
  @source_rewriter.insert_after(range, content)
end

def insert_before(range, content)

Parameters:
  • content (String) --
  • range (Parser::Source::Range) --
def insert_before(range, content)
  @source_rewriter.insert_before(range, content)
end

def remove(range)

Parameters:
  • range (Parser::Source::Range) --
def remove(range)
  @source_rewriter.remove(range)
end

def replace(range, content)

Parameters:
  • content (String) --
  • range (Parser::Source::Range) --
def replace(range, content)
  @source_rewriter.replace(range, content)
end

def rewrite(source_buffer, ast)

Returns:
  • (String) -

Parameters:
  • ast (Parser::AST::Node) --
  • source_buffer (Parser::Source::Buffer) --
def rewrite(source_buffer, ast)
  @source_rewriter = Source::Rewriter.new(source_buffer)
  process(ast)
  @source_rewriter.process
end