Class: Parser::TreeRewriter
- Inherits:
-
AST::Processor
- Object
- AST::Processor
- Parser::TreeRewriter
- Defined in:
- sorbet/rbi/parser@3.3.7.4.rbi
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 describing rewriters in greater detail.
source://parser//lib/parser/tree_rewriter.rb#51
Instance Method Summary collapse
-
#assignment?(node) ⇒ Boolean
Returns
true
if the specified node is an assignment node, returns false otherwise. -
#insert_after(range, content) ⇒ Object
Inserts new code after the given source range.
-
#insert_before(range, content) ⇒ Object
Inserts new code before the given source range.
-
#remove(range) ⇒ Object
Removes the source range.
-
#replace(range, content) ⇒ Object
Replaces the code of the source range
range
withcontent
. -
#rewrite(source_buffer, ast, **policy) ⇒ String
Rewrites the AST/source buffer and returns a String containing the new version.
-
#wrap(range, before, after) ⇒ Object
Wraps the given source range with the given values.
Methods inherited from AST::Processor
#on_alias, #on_and, #on_and_asgn, #on_arg_expr, #on_args, #on_argument, #on_array, #on_array_pattern, #on_array_pattern_with_tail, #on_begin, #on_block, #on_block_pass, #on_blockarg_expr, #on_break, #on_case, #on_case_match, #on_casgn, #on_class, #on_const, #on_const_pattern, #on_csend, #on_def, #on_defined?, #on_defs, #on_dstr, #on_dsym, #on_eflipflop, #on_empty_else, #on_ensure, #on_erange, #on_find_pattern, #on_for, #on_forwarded_kwrestarg, #on_forwarded_restarg, #on_hash, #on_hash_pattern, #on_if, #on_if_guard, #on_iflipflop, #on_in_match, #on_in_pattern, #on_index, #on_indexasgn, #on_irange, #on_kwargs, #on_kwbegin, #on_kwsplat, #on_lambda, #on_masgn, #on_match_alt, #on_match_as, #on_match_current_line, #on_match_pattern, #on_match_pattern_p, #on_match_rest, #on_match_with_lvasgn, #on_mlhs, #on_module, #on_next, #on_not, #on_numblock, #on_op_asgn, #on_or, #on_or_asgn, #on_pair, #on_pin, #on_postexe, #on_preexe, #on_procarg0, #on_redo, #on_regexp, #on_resbody, #on_rescue, #on_restarg_expr, #on_retry, #on_return, #on_sclass, #on_send, #on_splat, #on_super, #on_undef, #on_unless_guard, #on_until, #on_until_post, #on_var, #on_vasgn, #on_when, #on_while, #on_while_post, #on_xstr, #on_yield, #process_regular_node
Instance Method Details
#assignment?(node) ⇒ Boolean
Returns true
if the specified node is an assignment node, returns false
otherwise.
source://parser//lib/parser/tree_rewriter.rb#79
7230 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 7230 def assignment?(node); end |
#insert_after(range, content) ⇒ Object
Inserts new code after the given source range.
source://parser//lib/parser/tree_rewriter.rb#118
7239 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 7239 def insert_after(range, content); end |
#insert_before(range, content) ⇒ Object
Inserts new code before the given source range.
source://parser//lib/parser/tree_rewriter.rb#108
7248 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 7248 def insert_before(range, content); end |
#remove(range) ⇒ Object
Removes the source range.
source://parser//lib/parser/tree_rewriter.rb#88
7256 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 7256 def remove(range); end |
#replace(range, content) ⇒ Object
Replaces the code of the source range range
with content
.
source://parser//lib/parser/tree_rewriter.rb#128
7265 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 7265 def replace(range, content); end |
#rewrite(source_buffer, ast, **policy) ⇒ String
Rewrites the AST/source buffer and returns a String containing the new version.
source://parser//lib/parser/tree_rewriter.rb#62
7278 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 7278 def rewrite(source_buffer, ast, **policy); end |
#wrap(range, before, after) ⇒ Object
Wraps the given source range with the given values.
source://parser//lib/parser/tree_rewriter.rb#98
7287 |
# File 'sorbet/rbi/parser@3.3.7.4.rbi', line 7287 def wrap(range, before, after); end |