Class: Parser::Rewriter Deprecated

Inherits:
AST::Processor show all
Extended by:
Deprecation
Defined in:
lib/parser/rewriter.rb

Overview

Deprecated.

Rewriter is deprecated. Use TreeRewriter instead. It has a backwards compatible API and uses Source::TreeRewriter instead of Source::Rewriter. Please check the documentation for Source::Rewriter for details.

Constant Summary collapse

DEPRECATION_WARNING =
[
  'Parser::Rewriter is deprecated.',
  'Please update your code to use Parser::TreeRewriter instead'
].join("\n").freeze

Instance Attribute Summary

Attributes included from Deprecation

#warned_of_deprecation

Instance Method Summary collapse

Methods included from Deprecation

warn_of_deprecation

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

Constructor Details

#initializeRewriter

Returns a new instance of Rewriter.



98
99
100
101
102
# File 'lib/parser/rewriter.rb', line 98

def initialize(*)
  self.class.warn_of_deprecation
  Source::Rewriter.warned_of_deprecation = true
  super
end

Instance Method Details

#assignment?(node) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)


38
39
40
# File 'lib/parser/rewriter.rb', line 38

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:



77
78
79
# File 'lib/parser/rewriter.rb', line 77

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:



67
68
69
# File 'lib/parser/rewriter.rb', line 67

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

#remove(range) ⇒ Object

Removes the source range.

Parameters:



47
48
49
# File 'lib/parser/rewriter.rb', line 47

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

#replace(range, content) ⇒ Object

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

Parameters:



87
88
89
# File 'lib/parser/rewriter.rb', line 87

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

#rewrite(source_buffer, ast) ⇒ String

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

Parameters:

Returns:

  • (String)


23
24
25
26
27
28
29
# File 'lib/parser/rewriter.rb', line 23

def rewrite(source_buffer, ast)
  @source_rewriter = Source::Rewriter.new(source_buffer)

  process(ast)

  @source_rewriter.process
end

#wrap(range, before, after) ⇒ Object

Wraps the given source range with the given values.

Parameters:



57
58
59
# File 'lib/parser/rewriter.rb', line 57

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