Class: Parser::TreeRewriter

Inherits:
AST::Processor show all
Defined in:
lib/parser/tree_rewriter.rb

Overview

TreeRewriter offers a basic API that makes it easy to rewrite existing ASTs. It's built on top of AST::Processor and Source::TreeRewriter

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::TreeRewriter
  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

ast           = Parser::CurrentRuby.parse code
buffer        = Parser::Source::Buffer.new('(example)', source: code)
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 TreeRewriter 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.

Instance Method Summary collapse

Methods inherited from AST::Processor

#on_arg, #on_argument, #on_back_ref, #on_blockarg, #on_casgn, #on_const, #on_cvar, #on_cvasgn, #on_def, #on_defs, #on_empty_else, #on_forward_arg, #on_gvar, #on_gvasgn, #on_ivar, #on_ivasgn, #on_kwarg, #on_kwoptarg, #on_kwrestarg, #on_lvar, #on_lvasgn, #on_match_var, #on_nth_ref, #on_numblock, #on_op_asgn, #on_optarg, #on_procarg0, #on_restarg, #on_send, #on_shadowarg, #on_var, #on_vasgn, #process_argument_node, #process_regular_node, #process_var_asgn_node, #process_variable_node

Instance Method Details

#assignment?(node) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)

79
80
81
# File 'lib/parser/tree_rewriter.rb', line 79

def assignment?(node)
  [:lvasgn, :ivasgn, :gvasgn, :cvasgn, :casgn].include?(node.type)
end

#insert_after(range, content) ⇒ Object

Inserts new code after the given source range.

Parameters:


118
119
120
# File 'lib/parser/tree_rewriter.rb', line 118

def insert_after(range, content)
  @source_rewriter.insert_after(range, content)
end

#insert_before(range, content) ⇒ Object

Inserts new code before the given source range.

Parameters:


108
109
110
# File 'lib/parser/tree_rewriter.rb', line 108

def insert_before(range, content)
  @source_rewriter.insert_before(range, content)
end

#remove(range) ⇒ Object

Removes the source range.

Parameters:


88
89
90
# File 'lib/parser/tree_rewriter.rb', line 88

def remove(range)
  @source_rewriter.remove(range)
end

#replace(range, content) ⇒ Object

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

Parameters:


128
129
130
# File 'lib/parser/tree_rewriter.rb', line 128

def replace(range, content)
  @source_rewriter.replace(range, content)
end

#rewrite(source_buffer, ast, **policy) ⇒ String

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

Parameters:

Returns:

  • (String)

62
63
64
65
66
67
68
69
70
# File 'lib/parser/tree_rewriter.rb', line 62

def rewrite(source_buffer,
            ast,
            **policy)
  @source_rewriter = Parser::Source::TreeRewriter.new(source_buffer, **policy)

  process(ast)

  @source_rewriter.process
end

#wrap(range, before, after) ⇒ Object

Wraps the given source range with the given values.

Parameters:


98
99
100
# File 'lib/parser/tree_rewriter.rb', line 98

def wrap(range, before, after)
  @source_rewriter.wrap(range, before, after)
end