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.
-
#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. -
#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.
337 338 339 |
# File 'lib/synvert/core/rewriter/instance.rb', line 337 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.
181 182 183 |
# File 'lib/synvert/core/rewriter/instance.rb', line 181 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.
301 302 303 |
# File 'lib/synvert/core/rewriter/instance.rb', line 301 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.
85 86 87 88 89 |
# File 'lib/synvert/core/rewriter/instance.rb', line 85 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.
119 120 121 |
# File 'lib/synvert/core/rewriter/instance.rb', line 119 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.
133 134 135 |
# File 'lib/synvert/core/rewriter/instance.rb', line 133 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.
162 163 164 |
# File 'lib/synvert/core/rewriter/instance.rb', line 162 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.
215 216 217 |
# File 'lib/synvert/core/rewriter/instance.rb', line 215 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.
230 231 232 |
# File 'lib/synvert/core/rewriter/instance.rb', line 230 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.
47 48 49 |
# File 'lib/synvert/core/rewriter/instance.rb', line 47 def node @current_node end |
#prepend(code) ⇒ Object
Parse prepend dsl, it creates a PrependAction to prepend the code to the top of current node body.
200 201 202 |
# File 'lib/synvert/core/rewriter/instance.rb', line 200 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 write the code back to the original file.
34 35 36 37 38 39 40 41 42 |
# File 'lib/synvert/core/rewriter/instance.rb', line 34 def process @file_patterns.each do |file_pattern| Dir.glob(File.join(Configuration.path, file_pattern)).each do |file_path| next if Configuration.skip_files.include?(file_path) process_file(file_path) end end end |
#process_with_node(node) { ... } ⇒ Object
Set current_node to node and process.
55 56 57 58 59 |
# File 'lib/synvert/core/rewriter/instance.rb', line 55 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.
65 66 67 68 69 70 |
# File 'lib/synvert/core/rewriter/instance.rb', line 65 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.
286 287 288 |
# File 'lib/synvert/core/rewriter/instance.rb', line 286 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.
275 276 277 |
# File 'lib/synvert/core/rewriter/instance.rb', line 275 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.
245 246 247 |
# File 'lib/synvert/core/rewriter/instance.rb', line 245 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.
259 260 261 |
# File 'lib/synvert/core/rewriter/instance.rb', line 259 def replace_with(code) @current_mutation.replace_with(@current_node, code) 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.
147 148 149 |
# File 'lib/synvert/core/rewriter/instance.rb', line 147 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.
329 330 331 |
# File 'lib/synvert/core/rewriter/instance.rb', line 329 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.
103 104 105 |
# File 'lib/synvert/core/rewriter/instance.rb', line 103 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.
319 320 321 |
# File 'lib/synvert/core/rewriter/instance.rb', line 319 def wrap(with:) @current_mutation.wrap(@current_node, with: with) end |