Class Parser::Rewriter
In: lib/parser/rewriter.rb
Parent: Parser::AST::Processor

{Parser::Rewriter} offers a basic API that makes it easy to rewrite existing ASTs. It‘s built on top of {Parser::AST::Processor} and {Parser::Source::Rewriter}.

For example, assume you want to remove `do` tokens from a while statement. You can do this as following:

    require 'parser/current'

    class RemoveDo < Parser::Rewriter
      def on_while(node)
        # Check if the statement starts with "do"
        if node.location.begin.is?('do')
          remove(node.location.begin)
        end
      end
    end

    code = <<-EOF
    while true do
      puts 'hello'
    end
    EOF

    buffer        = Parser::Source::Buffer.new('(example)')
    buffer.source = code
    parser        = Parser::CurrentRuby.new
    ast           = parser.parse(buffer)
    rewriter      = RemoveDo.new

    # Rewrite the AST, returns a String with the new form.
    puts rewriter.rewrite(buffer, ast)

This would result in the following Ruby code:

    while true
      puts 'hello'
    end

Keep in mind that {Parser::Rewriter} does not take care of indentation when inserting/replacing code so you‘ll have to do this yourself.

See also [a blog entry](whitequark.org/blog/2013/04/26/lets-play-with-ruby-code/) describing rewriters in greater detail.

@api public

Methods

Public Instance methods

Returns `true` if the specified node is an assignment node, returns false otherwise.

@param [Parser::AST::Node] node @return [Boolean]

Inserts new code after the given source range.

@param [Parser::Source::Range] range @param [String] content

Inserts new code before the given source range.

@param [Parser::Source::Range] range @param [String] content

Removes the source range.

@param [Parser::Source::Range] range

Replaces the code of the source range `range` with `content`.

@param [Parser::Source::Range] range @param [String] content

Rewrites the AST/source buffer and returns a String containing the new version.

@param [Parser::Source::Buffer] source_buffer @param [Parser::AST::Node] ast @return [String]

[Validate]