lib/node_mutation.rb in node_mutation-1.14.0 vs lib/node_mutation.rb in node_mutation-1.15.0

- old
+ new

@@ -9,16 +9,16 @@ autoload :Adapter, "node_mutation/adapter" autoload :ParserAdapter, "node_mutation/parser_adapter" autoload :Action, 'node_mutation/action' autoload :AppendAction, 'node_mutation/action/append_action' autoload :DeleteAction, 'node_mutation/action/delete_action' + autoload :IndentAction, 'node_mutation/action/indent_action' autoload :InsertAction, 'node_mutation/action/insert_action' autoload :RemoveAction, 'node_mutation/action/remove_action' autoload :PrependAction, 'node_mutation/action/prepend_action' autoload :ReplaceAction, 'node_mutation/action/replace_action' autoload :ReplaceWithAction, 'node_mutation/action/replace_with_action' - autoload :WrapAction, 'node_mutation/action/wrap_action' autoload :NoopAction, 'node_mutation/action/noop_action' autoload :Result, 'node_mutation/result' autoload :Strategy, 'node_mutation/strategy' autoload :Struct, 'node_mutation/struct' @@ -186,26 +186,30 @@ # allow(obj).to receive_messages(:foo => 1, :bar => 2) def replace_with(node, code) @actions << ReplaceWithAction.new(node, code).process end - # Wrap source code of the ast node with new code. + # Wrap source code of the ast node with prefix and suffix code. # @param node [Node] ast node - # @param with [String] code need to be wrapped with. + # @param prefix [String] prefix code need to be wrapped with. + # @param suffix [String] suffix code need to be wrapped with. + # @param newline [Boolean] add newline after prefix and before suffix. # @example # source code of the ast node is # class Foobar # end # then we call - # wrap(node, with: 'module Synvert') + # wrap(node, prefix: 'module Synvert', suffix: 'end', newline: true) # the source code will be rewritten to # module Synvert # class Foobar # end # end - def wrap(node, with:) - @actions << WrapAction.new(node, with: with).process + def wrap(node, prefix:, suffix:, newline: false) + @actions << InsertAction.new(node, prefix, at: 'beginning').process + @actions << InsertAction.new(node, suffix, at: 'end').process + @actions << IndentAction.new(node).process if newline end # No operation. # @param node [Node] ast node def noop(node) @@ -275,14 +279,11 @@ return [] if i < 0 begin_pos = @actions[i].start end_pos = @actions[i].end while j > -1 - # if we have two insert actions at same position. - same_position = begin_pos == @actions[j].start && begin_pos == end_pos && @actions[j].start == @actions[j].end # if we have two actions with overlapped range. - overlapped_position = begin_pos < @actions[j].end - if (!strategy?(Strategy::ALLOW_INSERT_AT_SAME_POSITION) && same_position) || overlapped_position + if begin_pos < @actions[j].end conflict_actions << @actions.delete_at(j) else i = j begin_pos = @actions[i].start end_pos = @actions[i].end