Class: Synvert::Rewriter::Instance
- Inherits:
-
Object
- Object
- Synvert::Rewriter::Instance
- Defined in:
- lib/synvert/rewriter/instance.rb
Overview
Instance is an execution unit, it finds specified ast nodes, checks if the nodes match some conditions, then add, replace or remove code.
One instance can contains one or many [Synvert::Rewriter::Scope] and [Synvert::Rewriter::Condition].
Instance Attribute Summary collapse
-
#current_file ⇒ Object
Current filename.
-
#current_node ⇒ Object
Current parsing node.
-
#current_source ⇒ Object
Current source code of file.
Instance Method Summary collapse
-
#append(code) ⇒ Object
Parse append dsl, it creates a [Synvert::Rewriter::AppendAction] to append the code to the bottom of current node body.
-
#if_exist_node(rules, &block) ⇒ Object
Parse if_exist_node dsl, it creates a [Synvert::Rewriter::IfExistCondition] to check if matching nodes exist in the child nodes, if so, then continue operating on each matching ast node.
-
#if_only_exist_node(rules, &block) ⇒ Object
Parse if_only_exist_node dsl, it creates a [Synvert::Rewriter::IfOnlyExistCondition] to check if current node has only one child node and the child node matches rules, if so, then continue operating on each matching ast node.
-
#initialize(file_pattern, &block) ⇒ Synvert::Rewriter::Instance
constructor
Initialize an instance.
-
#insert(code) ⇒ Object
Parse insert dsl, it creates a [Synvert::Rewriter::InsertAction] to insert the code to the top of current node body.
-
#insert_after(node) ⇒ Object
Parse insert_after dsl, it creates a [Synvert::Rewriter::InsertAfterAction] to insert the code next to the current node.
-
#node ⇒ Parser::AST::Node
Gets current node, it allows to get current node in block code.
-
#process ⇒ Object
Process the instance.
-
#remove ⇒ Object
Parse remove dsl, it creates a [Synvert::Rewriter::RemoveAction] to current node.
-
#replace_with(code) ⇒ Object
Parse replace_with dsl, it creates a [Synvert::Rewriter::ReplaceWithAction] to replace current node with code.
-
#unless_exist_node(rules, &block) ⇒ Object
Parse unless_exist_node dsl, it creates a [Synvert::Rewriter::UnlessExistCondition] to check if matching nodes doesn’t exist in the child nodes, if so, then continue operating on each matching ast node.
-
#within_node(rules, &block) ⇒ Object
(also: #with_node)
Parse within_node dsl, it creates a [Synvert::Rewriter::Scope] to find matching ast nodes, then continue operating on each matching ast node.
Constructor Details
#initialize(file_pattern, &block) ⇒ Synvert::Rewriter::Instance
Initialize an instance.
22 23 24 25 26 |
# File 'lib/synvert/rewriter/instance.rb', line 22 def initialize(file_pattern, &block) @actions = [] @file_pattern = file_pattern @block = block end |
Instance Attribute Details
#current_file ⇒ Object
Returns current filename.
15 |
# File 'lib/synvert/rewriter/instance.rb', line 15 attr_accessor :current_node, :current_source, :current_file |
#current_node ⇒ Object
Returns current parsing node.
15 16 17 |
# File 'lib/synvert/rewriter/instance.rb', line 15 def current_node @current_node end |
#current_source ⇒ Object
Returns current source code of file.
15 |
# File 'lib/synvert/rewriter/instance.rb', line 15 attr_accessor :current_node, :current_source, :current_file |
Instance Method Details
#append(code) ⇒ Object
Parse append dsl, it creates a [Synvert::Rewriter::AppendAction] to append the code to the bottom of current node body.
116 117 118 |
# File 'lib/synvert/rewriter/instance.rb', line 116 def append(code) @actions << Rewriter::AppendAction.new(self, code) end |
#if_exist_node(rules, &block) ⇒ Object
Parse if_exist_node dsl, it creates a [Synvert::Rewriter::IfExistCondition] to check if matching nodes exist in the child nodes, if so, then continue operating on each matching ast node.
89 90 91 |
# File 'lib/synvert/rewriter/instance.rb', line 89 def if_exist_node(rules, &block) Rewriter::IfExistCondition.new(self, rules, &block).process end |
#if_only_exist_node(rules, &block) ⇒ Object
Parse if_only_exist_node dsl, it creates a [Synvert::Rewriter::IfOnlyExistCondition] to check if current node has only one child node and the child node matches rules, if so, then continue operating on each matching ast node.
108 109 110 |
# File 'lib/synvert/rewriter/instance.rb', line 108 def if_only_exist_node(rules, &block) Rewriter::IfOnlyExistCondition.new(self, rules, &block).process end |
#insert(code) ⇒ Object
Parse insert dsl, it creates a [Synvert::Rewriter::InsertAction] to insert the code to the top of current node body.
124 125 126 |
# File 'lib/synvert/rewriter/instance.rb', line 124 def insert(code) @actions << Rewriter::InsertAction.new(self, code) end |
#insert_after(node) ⇒ Object
Parse insert_after dsl, it creates a [Synvert::Rewriter::InsertAfterAction] to insert the code next to the current node.
132 133 134 |
# File 'lib/synvert/rewriter/instance.rb', line 132 def insert_after(node) @actions << Rewriter::InsertAfterAction.new(self, node) end |
#node ⇒ Parser::AST::Node
Gets current node, it allows to get current node in block code.
65 66 67 |
# File 'lib/synvert/rewriter/instance.rb', line 65 def node @current_node end |
#process ⇒ Object
Process the instance. It finds all files, for each file, it executes the block code, gets all rewrite actions, and rewrite source code back to original file.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/synvert/rewriter/instance.rb', line 31 def process parser = Parser::CurrentRuby.new file_pattern = File.join(Configuration.instance.get(:path), @file_pattern) Dir.glob(file_pattern).each do |file_path| begin source = File.read(file_path) buffer = Parser::Source::Buffer.new file_path buffer.source = source parser.reset ast = parser.parse buffer @current_file = file_path @current_source = source @current_node = ast instance_eval &@block @current_node = ast @actions.sort! check_conflict_actions @actions.reverse.each do |action| source[action.begin_pos...action.end_pos] = action.rewritten_code source = remove_code_or_whole_line(source, action.line) end @actions = [] File.write file_path, source end while !@conflict_actions.empty? end end |
#remove ⇒ Object
Parse remove dsl, it creates a [Synvert::Rewriter::RemoveAction] to current node.
145 146 147 |
# File 'lib/synvert/rewriter/instance.rb', line 145 def remove @actions << Rewriter::RemoveAction.new(self) end |
#replace_with(code) ⇒ Object
Parse replace_with dsl, it creates a [Synvert::Rewriter::ReplaceWithAction] to replace current node with code.
140 141 142 |
# File 'lib/synvert/rewriter/instance.rb', line 140 def replace_with(code) @actions << Rewriter::ReplaceWithAction.new(self, code) end |
#unless_exist_node(rules, &block) ⇒ Object
Parse unless_exist_node dsl, it creates a [Synvert::Rewriter::UnlessExistCondition] to check if matching nodes doesn’t exist in the child nodes, if so, then continue operating on each matching ast node.
98 99 100 |
# File 'lib/synvert/rewriter/instance.rb', line 98 def unless_exist_node(rules, &block) Rewriter::UnlessExistCondition.new(self, rules, &block).process end |
#within_node(rules, &block) ⇒ Object Also known as: with_node
Parse within_node dsl, it creates a [Synvert::Rewriter::Scope] to find matching ast nodes, then continue operating on each matching ast node.
78 79 80 |
# File 'lib/synvert/rewriter/instance.rb', line 78 def within_node(rules, &block) Rewriter::Scope.new(self, rules, &block).process end |