lib/parser/source/tree_rewriter.rb in parser-2.7.1.0 vs lib/parser/source/tree_rewriter.rb in parser-2.7.1.1
- old
+ new
@@ -116,10 +116,47 @@
all_encompassing_range = @source_buffer.source_range.adjust(begin_pos: -1, end_pos: +1)
@action_root = TreeRewriter::Action.new(all_encompassing_range, @enforcer)
end
##
+ # Returns true iff no (non trivial) update has been recorded
+ #
+ # @return [Boolean]
+ #
+ def empty?
+ @action_root.empty?
+ end
+
+ ##
+ # Merges the updates of argument with the receiver.
+ # Policies of the receiver are used.
+ #
+ # @param [Rewriter] with
+ # @return [Rewriter] self
+ # @raise [ClobberingError] when clobbering is detected
+ #
+ def merge!(with)
+ raise 'TreeRewriter are not for the same source_buffer' unless
+ source_buffer == with.source_buffer
+
+ @action_root = @action_root.combine(with.action_root)
+ self
+ end
+
+ ##
+ # Returns a new rewriter that consists of the updates of the received
+ # and the given argument. Policies of the receiver are used.
+ #
+ # @param [Rewriter] with
+ # @return [Rewriter] merge of receiver and argument
+ # @raise [ClobberingError] when clobbering is detected
+ #
+ def merge(with)
+ dup.merge!(with)
+ end
+
+ ##
# Replaces the code of the source range `range` with `content`.
#
# @param [Range] range
# @param [String] content
# @return [Rewriter] self
@@ -201,14 +238,13 @@
end
##
# Provides a protected block where a sequence of multiple rewrite actions
# are handled atomically. If any of the actions failed by clobbering,
- # all the actions are rolled back.
+ # all the actions are rolled back. Transactions can be nested.
#
# @raise [RuntimeError] when no block is passed
- # @raise [RuntimeError] when already in a transaction
#
def transaction
unless block_given?
raise "#{self.class}##{__method__} requires block"
end
@@ -253,9 +289,13 @@
'TreeRewriter#insert_before_multi and insert_before_multi exist only for legacy compatibility.',
'Please update your code to use `wrap`, `insert_before` or `insert_after` instead.'
].join("\n").freeze
extend Deprecation
+
+ protected
+
+ attr_reader :action_root
private
ACTIONS = %i[accept warn raise].freeze
def check_policy_validity