Class: Synvert::Core::Rewriter::Instance
- Inherits:
-
Object
- Object
- Synvert::Core::Rewriter::Instance
- Includes:
- Helper
- Defined in:
- lib/synvert/core/rewriter/instance.rb
Overview
Instance Attribute Summary collapse
-
#current_file ⇒ Object
Current filename.
-
#current_mutation ⇒ Object
Current mutation.
-
#current_node ⇒ Object
Current parsing node.
Instance Method Summary collapse
-
#any_value ⇒ Synvert::Core::Rewriter::AnyValue
Match any value but nil.
-
#append(code) ⇒ Object
Parse
appenddsl, it creates a AppendAction to append the code to the bottom of current node body. -
#delete(*selectors, **options) ⇒ Object
Parse
deletedsl, it creates a DeleteAction to delete child nodes. -
#find_node(nql, options = {}) { ... } ⇒ Object
Parse
find_nodedsl, it creates QueryScope to recursively find matching ast nodes, then continue operating on each matching ast node. -
#goto_node(child_node_name, &block) ⇒ Object
Parse
goto_nodedsl, it creates a GotoScope to go to a child node, then continue operating on the child node. -
#if_exist_node(rules, &block) ⇒ Object
Parse
if_exist_nodedsl, it creates a 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_nodedsl, it creates a 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(rewriter, file_patterns) { ... } ⇒ Instance
constructor
Initialize an Instance.
-
#insert(code, at: 'end', to: nil) ⇒ Object
Parse
insertdsl, it creates a InsertAction to insert code. -
#insert_after(code) ⇒ Object
Parse
insert_afterdsl, it creates a 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.
-
#noop ⇒ Object
Parse +noop dsl.
-
#prepend(code) ⇒ Object
Parse
prependdsl, it creates a PrependAction to prepend the code to the top of current node body. -
#process ⇒ Object
Process the instance.
-
#process_with_node(node) { ... } ⇒ Object
Set current_node to node and process.
-
#process_with_other_node(node) { ... } ⇒ Object
Set current_node properly, process and set current_node back to original current_node.
-
#remove(**options) ⇒ Object
Parse
removedsl, it creates a RemoveAction to remove current node. -
#replace(*selectors, with:) ⇒ Object
Parse
replacedsl, it creates a ReplaceAction to replace the code of specified child nodes. -
#replace_erb_stmt_with_expr ⇒ Object
Parse
replace_erb_stmt_with_exprdsl, it creates a ReplaceErbStmtWithExprAction to replace erb stmt code to expr code. -
#replace_with(code) ⇒ Object
Parse
replace_withdsl, it creates a ReplaceWithAction to replace the whole code of current node. -
#test ⇒ Object
Test the instance.
-
#unless_exist_node(rules, &block) ⇒ Object
Parse
unless_exist_nodedsl, it creates a UnlessExistCondition to check if matching nodes doesn’t exist in the child nodes, if so, then continue operating on each matching ast node. -
#warn(message) ⇒ Object
Parse
warndsl, it creates a Warning to save warning message. -
#within_node(rules, options = {}) { ... } ⇒ Object
(also: #with_node)
Parse
within_nodedsl, it creates a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node. -
#wrap(with:) ⇒ Object
Parse
wrapdsl, it creates a WrapAction to wrap current node with code.
Methods included from Helper
#add_arguments_with_parenthesis_if_necessary, #add_curly_brackets_if_necessary, #add_receiver_if_necessary, #reject_keys_from_hash, #strip_brackets
Constructor Details
#initialize(rewriter, file_patterns) { ... } ⇒ Instance
Initialize an Instance.
15 16 17 18 19 20 21 |
# File 'lib/synvert/core/rewriter/instance.rb', line 15 def initialize(rewriter, file_patterns, &block) @rewriter = rewriter @actions = [] @file_patterns = file_patterns @block = block rewriter.helpers.each { |helper| singleton_class.send(:define_method, helper[:name], &helper[:block]) } end |
Instance Attribute Details
#current_file ⇒ Object
Returns current filename.
29 |
# File 'lib/synvert/core/rewriter/instance.rb', line 29 attr_accessor :current_node, :current_file, :current_mutation |
#current_mutation ⇒ Object
Returns current mutation.
29 |
# File 'lib/synvert/core/rewriter/instance.rb', line 29 attr_accessor :current_node, :current_file, :current_mutation |
#current_node ⇒ Object
Returns current parsing node.
29 30 31 |
# File 'lib/synvert/core/rewriter/instance.rb', line 29 def current_node @current_node end |
Instance Method Details
#any_value ⇒ Synvert::Core::Rewriter::AnyValue
Match any value but nil.
347 348 349 |
# File 'lib/synvert/core/rewriter/instance.rb', line 347 def any_value Rewriter::AnyValue.new end |
#append(code) ⇒ Object
Parse append dsl, it creates a AppendAction to append the code to the bottom of current node body.
186 187 188 |
# File 'lib/synvert/core/rewriter/instance.rb', line 186 def append(code) @current_mutation.append(@current_node, code) end |
#delete(*selectors, **options) ⇒ Object
Parse delete dsl, it creates a DeleteAction to delete child nodes.
306 307 308 |
# File 'lib/synvert/core/rewriter/instance.rb', line 306 def delete(*selectors, **) @current_mutation.delete(@current_node, *selectors, **) end |
#find_node(nql, options = {}) { ... } ⇒ Object
Parse find_node dsl, it creates QueryScope to recursively find matching ast nodes, then continue operating on each matching ast node.
90 91 92 93 94 |
# File 'lib/synvert/core/rewriter/instance.rb', line 90 def find_node(nql, = {}, &block) Rewriter::QueryScope.new(self, nql, , &block).process rescue NodeQueryLexer::ScanError, Racc::ParseError => e raise NodeQuery::Compiler::ParseError, "Invalid query string: #{nql}" end |
#goto_node(child_node_name, &block) ⇒ Object
Parse goto_node dsl, it creates a GotoScope to go to a child node, then continue operating on the child node.
124 125 126 |
# File 'lib/synvert/core/rewriter/instance.rb', line 124 def goto_node(child_node_name, &block) Rewriter::GotoScope.new(self, child_node_name, &block).process end |
#if_exist_node(rules, &block) ⇒ Object
Parse if_exist_node dsl, it creates a Synvert::Core::Rewriter::IfExistCondition to check if matching nodes exist in the child nodes, if so, then continue operating on each matching ast node.
138 139 140 |
# File 'lib/synvert/core/rewriter/instance.rb', line 138 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::Core::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.
167 168 169 |
# File 'lib/synvert/core/rewriter/instance.rb', line 167 def if_only_exist_node(rules, &block) Rewriter::IfOnlyExistCondition.new(self, rules, &block).process end |
#insert(code, at: 'end', to: nil) ⇒ Object
Parse insert dsl, it creates a Synvert::Core::Rewriter::InsertAction to insert code.
220 221 222 |
# File 'lib/synvert/core/rewriter/instance.rb', line 220 def insert(code, at: 'end', to: nil) @current_mutation.insert(@current_node, code, at: at, to: to) end |
#insert_after(code) ⇒ Object
Parse insert_after dsl, it creates a Synvert::Core::Rewriter::InsertAfterAction to insert the code next to the current node.
235 236 237 |
# File 'lib/synvert/core/rewriter/instance.rb', line 235 def insert_after(code) @current_mutation.insert_after(@current_node, code) end |
#node ⇒ Parser::AST::Node
Gets current node, it allows to get current node in block code.
52 53 54 |
# File 'lib/synvert/core/rewriter/instance.rb', line 52 def node @current_node end |
#noop ⇒ Object
Parse +noop dsl.
329 330 331 |
# File 'lib/synvert/core/rewriter/instance.rb', line 329 def noop @current_mutation.noop(@current_node) end |
#prepend(code) ⇒ Object
Parse prepend dsl, it creates a PrependAction to prepend the code to the top of current node body.
205 206 207 |
# File 'lib/synvert/core/rewriter/instance.rb', line 205 def prepend(code) @current_mutation.prepend(@current_node, code) end |
#process ⇒ Object
Process the instance. It finds specified files, for each file, it executes the block code, rewrites the original code, then writes the code back to the original file.
34 35 36 37 38 |
# File 'lib/synvert/core/rewriter/instance.rb', line 34 def process get_file_paths.each do |file_path| process_file(file_path) end end |
#process_with_node(node) { ... } ⇒ Object
Set current_node to node and process.
60 61 62 63 64 |
# File 'lib/synvert/core/rewriter/instance.rb', line 60 def process_with_node(node) self.current_node = node yield self.current_node = node end |
#process_with_other_node(node) { ... } ⇒ Object
Set current_node properly, process and set current_node back to original current_node.
70 71 72 73 74 75 |
# File 'lib/synvert/core/rewriter/instance.rb', line 70 def process_with_other_node(node) original_node = current_node self.current_node = node yield self.current_node = original_node end |
#remove(**options) ⇒ Object
Parse remove dsl, it creates a RemoveAction to remove current node.
291 292 293 |
# File 'lib/synvert/core/rewriter/instance.rb', line 291 def remove(**) @current_mutation.remove(@current_node, **) end |
#replace(*selectors, with:) ⇒ Object
Parse replace dsl, it creates a ReplaceAction to replace the code of specified child nodes.
280 281 282 |
# File 'lib/synvert/core/rewriter/instance.rb', line 280 def replace(*selectors, with:) @current_mutation.replace(@current_node, *selectors, with: with) end |
#replace_erb_stmt_with_expr ⇒ Object
Parse replace_erb_stmt_with_expr dsl, it creates a ReplaceErbStmtWithExprAction to replace erb stmt code to expr code.
250 251 252 |
# File 'lib/synvert/core/rewriter/instance.rb', line 250 def replace_erb_stmt_with_expr @current_mutation.actions << Rewriter::ReplaceErbStmtWithExprAction.new(@current_node).process end |
#replace_with(code) ⇒ Object
Parse replace_with dsl, it creates a ReplaceWithAction to replace the whole code of current node.
264 265 266 |
# File 'lib/synvert/core/rewriter/instance.rb', line 264 def replace_with(code) @current_mutation.replace_with(@current_node, code) end |
#test ⇒ Object
Test the instance. It finds specified files, for each file, it executes the block code, tests the original code, then returns the actions.
43 44 45 46 47 |
# File 'lib/synvert/core/rewriter/instance.rb', line 43 def test get_file_paths.map do |file_path| test_file(file_path) end end |
#unless_exist_node(rules, &block) ⇒ Object
Parse unless_exist_node dsl, it creates a UnlessExistCondition to check if matching nodes doesn’t exist in the child nodes, if so, then continue operating on each matching ast node.
152 153 154 |
# File 'lib/synvert/core/rewriter/instance.rb', line 152 def unless_exist_node(rules, &block) Rewriter::UnlessExistCondition.new(self, rules, &block).process end |
#warn(message) ⇒ Object
Parse warn dsl, it creates a Warning to save warning message.
339 340 341 |
# File 'lib/synvert/core/rewriter/instance.rb', line 339 def warn() @rewriter.add_warning Rewriter::Warning.new(self, ) end |
#within_node(rules, options = {}) { ... } ⇒ Object Also known as: with_node
Parse within_node dsl, it creates a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node.
108 109 110 |
# File 'lib/synvert/core/rewriter/instance.rb', line 108 def within_node(rules, = {}, &block) Rewriter::WithinScope.new(self, rules, , &block).process end |
#wrap(with:) ⇒ Object
Parse wrap dsl, it creates a WrapAction to wrap current node with code.
324 325 326 |
# File 'lib/synvert/core/rewriter/instance.rb', line 324 def wrap(with:) @current_mutation.wrap(@current_node, with: with) end |