lib/node_mutation.rb in node_mutation-1.10.0 vs lib/node_mutation.rb in node_mutation-1.10.1
- old
+ new
@@ -1,9 +1,7 @@
# frozen_string_literal: true
-require 'ostruct'
-
require_relative "node_mutation/version"
class NodeMutation
class MethodNotSupported < StandardError; end
class ConflictActionError < StandardError; end
@@ -18,16 +16,18 @@
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 :Range, 'node_mutation/range'
+ autoload :Location, 'node_mutation/location'
autoload :Result, 'node_mutation/result'
autoload :Strategy, 'node_mutation/strategy'
attr_reader :actions
- class <<self
+ 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
@@ -218,25 +218,21 @@
def process
if @actions.length == 0
return NodeMutation::Result.new(affected: false, conflicted: false)
end
- conflict_actions = []
source = +@source
@actions.sort_by! { |action| [action.start, action.end] }
conflict_actions = get_conflict_actions
if conflict_actions.size > 0 && strategy?(Strategy::THROW_ERROR)
raise ConflictActionError, "mutation actions are conflicted"
end
+
@actions.reverse_each do |action|
source[action.start...action.end] = action.new_code if action.new_code
end
- NodeMutation::Result.new(
- affected: true,
- conflicted: !conflict_actions.empty?,
- new_source: source
- )
+ NodeMutation::Result.new(affected: true, conflicted: !conflict_actions.empty?, new_source: source)
end
# Test actions and return the actions.
#
# If there's an action range conflict,
@@ -247,21 +243,17 @@
def test
if @actions.length == 0
return NodeMutation::Result.new(affected: false, conflicted: false, actions: [])
end
- conflict_actions = []
@actions.sort_by! { |action| [action.start, action.end] }
conflict_actions = get_conflict_actions
if conflict_actions.size > 0 && strategy?(Strategy::THROW_ERROR)
raise ConflictActionError, "mutation actions are conflicted"
end
- NodeMutation::Result.new(
- affected: true,
- conflicted: !conflict_actions.empty?,
- actions: format_actions(@actions)
- )
+
+ NodeMutation::Result.new(affected: true, conflicted: !conflict_actions.empty?, actions: @actions)
end
private
# It changes source code from bottom to top, and it can change source code twice at the same time,
@@ -291,11 +283,7 @@
conflict_actions
end
def strategy?(strategy)
NodeMutation.strategy & strategy == strategy
- end
-
- def format_actions(actions)
- actions.map { |action| OpenStruct.new(start: action.start, end: action.end, new_code: action.new_code ) }
end
end