lib/node_mutation.rb in node_mutation-1.21.6 vs lib/node_mutation.rb in node_mutation-1.22.0

- old
+ new

@@ -3,10 +3,11 @@ require_relative "node_mutation/version" class NodeMutation class MethodNotSupported < StandardError; end class ConflictActionError < StandardError; end + class InvalidAdapterError < StandardError; end autoload :Adapter, "node_mutation/adapter" autoload :ParserAdapter, "node_mutation/adapter/parser" autoload :SyntaxTreeAdapter, "node_mutation/adapter/syntax_tree" autoload :Action, 'node_mutation/action' @@ -25,40 +26,30 @@ autoload :Struct, 'node_mutation/struct' autoload :Helper, 'node_mutation/helper' # @!attribute [r] actions # @return [Array<NodeMutation::Struct::Action>] - attr_reader :actions + attr_reader :actions, :adapter # @!attribute [rw] transform_proc # @return [Proc] proc to transfor the actions attr_accessor :transform_proc class << self # Configure NodeMutation # @param [Hash] options options to configure - # @option options [NodeMutation::Adapter] :adapter the adpater # @option options [NodeMutation::Strategy] :strategy the strategy # @option options [Integer] :tab_width the tab width def configure(options) - if options[:adapter] - @adapter = options[:adapter] - end if options[:strategy] @strategy = options[:strategy] end if options[:tab_width] @tab_width = options[:tab_width].to_i end end - # Get the adapter - # @return [NodeMutation::Adapter] current adapter, by default is {NodeMutation::ParserAdapter} - def adapter - @adapter ||= ParserAdapter.new - end - # Get the strategy # @return [Integer] current strategy, could be {NodeMutation::Strategy::KEEP_RUNNING} or {NodeMutation::Strategy::THROW_ERROR}, # by default is {NodeMutation::Strategy::KEEP_RUNNING} def strategy @strategy ||= Strategy::KEEP_RUNNING @@ -71,13 +62,15 @@ end end # Initialize a NodeMutation. # @param source [String] file source - def initialize(source) + # @param adapter [Symbol] :parser or :syntax_tree + def initialize(source, adapter:) @source = source @actions = [] + @adapter = get_adapter_instance(adapter) end # Append code to the ast node. # @param node [Node] ast node # @param code [String] new code to append @@ -92,11 +85,11 @@ # def teardown # clean_something # super # end def append(node, code) - @actions << AppendAction.new(node, code).process + @actions << AppendAction.new(node, code, adapter: @adapter).process end # Delete source code of the child ast node. # @param node [Node] ast node # @param selectors [Array<Symbol>] selector names of child node. @@ -107,11 +100,11 @@ # then we call # mutation.delete(node, :receiver, :dot) # the source code will be rewritten to # create(...) def delete(node, *selectors, and_comma: false) - @actions << DeleteAction.new(node, *selectors, and_comma: and_comma).process + @actions << DeleteAction.new(node, *selectors, and_comma: and_comma, adapter: @adapter).process end # Insert code to the ast node. # @param node [Node] ast node # @param code [String] code need to be inserted. @@ -124,11 +117,11 @@ # then we call # mutation.insert(node, 'URI.', at: 'beginning') # the source code will be rewritten to # URI.open('http://test.com') def insert(node, code, at: 'end', to: nil, and_comma: false) - @actions << InsertAction.new(node, code, at: at, to: to, and_comma: and_comma).process + @actions << InsertAction.new(node, code, at: at, to: to, and_comma: and_comma, adapter: @adapter).process end # Prepend code to the ast node. # @param node [Node] ast node # @param code [String] new code to prepend. @@ -143,11 +136,11 @@ # def setup # super # do_something # end def prepend(node, code) - @actions << PrependAction.new(node, code).process + @actions << PrependAction.new(node, code, adapter: @adapter).process end # Remove source code of the ast node. # @param node [Node] ast node # @param and_comma [Boolean] delete extra comma. @@ -156,11 +149,11 @@ # puts "test" # then we call # mutation.remove(node) # the source code will be removed def remove(node, and_comma: false) - @actions << RemoveAction.new(node, and_comma: and_comma).process + @actions << RemoveAction.new(node, and_comma: and_comma, adapter: @adapter).process end # Replace child node of the ast node with new code. # @param node [Node] ast node # @param selectors [Array<Symbol>] selector names of child node. @@ -172,11 +165,11 @@ # mutation.replace(node, :message, with: 'assert_empty') # mutation.replace(node, :arguments, with: '{{arguments.first.receiver}}') # the source code will be rewritten to # assert_empty(object) def replace(node, *selectors, with:) - @actions << ReplaceAction.new(node, *selectors, with: with).process + @actions << ReplaceAction.new(node, *selectors, with: with, adapter: @adapter).process end # Replace source code of the ast node with new code. # @param node [Node] ast node # @param code [String] code need to be replaced with. @@ -186,11 +179,11 @@ # then we call # replace_with 'allow({{receiver}}).to receive_messages({{arguments}})' # the source code will be rewritten to # allow(obj).to receive_messages(:foo => 1, :bar => 2) def replace_with(node, code) - @actions << ReplaceWithAction.new(node, code).process + @actions << ReplaceWithAction.new(node, code, adapter: @adapter).process end # Wrap source code of the ast node with prefix and suffix code. # @param node [Node] ast node # @param prefix [String] prefix code need to be wrapped with. @@ -207,11 +200,11 @@ # class Foobar # end # end def wrap(node, prefix:, suffix:, newline: false) if newline - indentation = NodeMutation.adapter.get_start_loc(node).column + indentation = @adapter.get_start_loc(node).column group do insert node, prefix + "\n" + (' ' * indentation), at: 'beginning' insert node, "\n" + (' ' * indentation) + suffix, at: 'end' indent node end @@ -233,17 +226,17 @@ # indent(node) # the source code will be rewritten to # class Foobar # end def indent(node) - @actions << IndentAction.new(node).process + @actions << IndentAction.new(node, adapter: @adapter).process end # No operation. # @param node [Node] ast node def noop(node) - @actions << NoopAction.new(node).process + @actions << NoopAction.new(node, adapter: @adapter).process end # group multiple actions def group current_actions = @actions @@ -421,7 +414,18 @@ end end def strategy?(strategy) NodeMutation.strategy & strategy == strategy + end + + def get_adapter_instance(adapter) + case adapter.to_sym + when :parser + ParserAdapter.new + when :syntax_tree + SyntaxTreeAdapter.new + else + raise InvalidAdapterError, "adapter #{adapter} is not supported" + end end end