lib/node_mutation.rb in node_mutation-1.20.0 vs lib/node_mutation.rb in node_mutation-1.21.0
- old
+ new
@@ -9,11 +9,11 @@
autoload :Adapter, "node_mutation/adapter"
autoload :ParserAdapter, "node_mutation/adapter/parser"
autoload :SyntaxTreeAdapter, "node_mutation/adapter/syntax_tree"
autoload :Action, 'node_mutation/action'
autoload :AppendAction, 'node_mutation/action/append_action'
- autoload :CombinedAction, 'node_mutation/action/combined_action'
+ autoload :GroupAction, 'node_mutation/action/group_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'
@@ -208,17 +208,17 @@
# end
# end
def wrap(node, prefix:, suffix:, newline: false)
if newline
indentation = NodeMutation.adapter.get_start_loc(node).column
- combine do
+ group do
insert node, prefix + "\n" + (' ' * indentation), at: 'beginning'
insert node, "\n" + (' ' * indentation) + suffix, at: 'end'
indent node
end
else
- combine do
+ group do
insert node, prefix, at: 'beginning'
insert node, suffix, at: 'end'
end
end
end
@@ -242,18 +242,18 @@
# @param node [Node] ast node
def noop(node)
@actions << NoopAction.new(node).process
end
- # Combine multiple actions
- def combine
+ # group multiple actions
+ def group
current_actions = @actions
- combined_action = CombinedAction.new
- @actions = combined_action.actions
+ group_action = GroupAction.new
+ @actions = group_action.actions
yield
@actions = current_actions
- @actions << combined_action.process
+ @actions << group_action.process
end
# Process actions and return the new source.
#
# If there's an action range conflict,
@@ -306,31 +306,31 @@
result
end
private
- # It flattens a series of actions by removing any CombinedAction
+ # It flattens a series of actions by removing any GroupAction
# objects that contain only a single action. This is done recursively.
def flatten_actions(actions)
new_actions = []
actions.each do |action|
- if action.is_a?(CombinedAction)
- new_actions << flatten_combined_action(action)
+ if action.is_a?(GroupAction)
+ new_actions << flatten_group_action(action)
else
new_actions << action
end
end
new_actions.compact
end
- # It flattens a combined action.
- def flatten_combined_action(action)
+ # It flattens a group action.
+ def flatten_group_action(action)
if action.actions.empty?
nil
elsif action.actions.size == 1
- if action.actions.first.is_a?(CombinedAction)
- flatten_combined_action(action.actions.first)
+ if action.actions.first.is_a?(GroupAction)
+ flatten_group_action(action.actions.first)
else
action.actions.first
end
else
action.actions = flatten_actions(action.actions)
@@ -342,21 +342,21 @@
# @param actions [Array<NodeMutation::Action>]
# @return [Array<NodeMutation::Action>] sorted actions
def sort_actions!(actions)
actions.sort_by! { |action| [action.start, action.end] }
actions.each do |action|
- sort_actions!(action.actions) if action.is_a?(CombinedAction)
+ sort_actions!(action.actions) if action.is_a?(GroupAction)
end
end
# Rewrite source code with actions.
# @param source [String] source code
# @param actions [Array<NodeMutation::Action>] actions
# @return [String] new source code
def rewrite_source(source, actions)
actions.reverse_each do |action|
- if action.is_a?(CombinedAction)
+ if action.is_a?(GroupAction)
source = rewrite_source(source, action.actions)
else
source[action.start...action.end] = action.new_code if action.new_code
end
end
@@ -383,10 +383,10 @@
end_pos = actions[i].end
end
j -= 1
end
actions.each do |action|
- conflict_actions.concat(get_conflict_actions(action.actions)) if action.is_a?(CombinedAction)
+ conflict_actions.concat(get_conflict_actions(action.actions)) if action.is_a?(GroupAction)
end
conflict_actions
end
def strategy?(strategy)