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(query_string) { ... } ⇒ 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.
336 337 338 |
# File 'lib/synvert/core/rewriter/instance.rb', line 336 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.
180 181 182 |
# File 'lib/synvert/core/rewriter/instance.rb', line 180 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.
300 301 302 |
# File 'lib/synvert/core/rewriter/instance.rb', line 300 def delete(*selectors, **) @current_mutation.delete(@current_node, *selectors, **) end |
#find_node(query_string) { ... } ⇒ 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 |
# File 'lib/synvert/core/rewriter/instance.rb', line 85 def find_node(query_string, &block) Rewriter::QueryScope.new(self, query_string, &block).process 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.
118 119 120 |
# File 'lib/synvert/core/rewriter/instance.rb', line 118 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.
132 133 134 |
# File 'lib/synvert/core/rewriter/instance.rb', line 132 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.
161 162 163 |
# File 'lib/synvert/core/rewriter/instance.rb', line 161 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.
214 215 216 |
# File 'lib/synvert/core/rewriter/instance.rb', line 214 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.
229 230 231 |
# File 'lib/synvert/core/rewriter/instance.rb', line 229 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.
199 200 201 |
# File 'lib/synvert/core/rewriter/instance.rb', line 199 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.
285 286 287 |
# File 'lib/synvert/core/rewriter/instance.rb', line 285 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.
274 275 276 |
# File 'lib/synvert/core/rewriter/instance.rb', line 274 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.
244 245 246 |
# File 'lib/synvert/core/rewriter/instance.rb', line 244 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.
258 259 260 |
# File 'lib/synvert/core/rewriter/instance.rb', line 258 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.
146 147 148 |
# File 'lib/synvert/core/rewriter/instance.rb', line 146 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.
328 329 330 |
# File 'lib/synvert/core/rewriter/instance.rb', line 328 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.
100 101 102 103 104 |
# File 'lib/synvert/core/rewriter/instance.rb', line 100 def within_node(rules, = {}, &block) [:stop_when_match] ||= false [:direct] ||= false Rewriter::WithinScope.new(self, rules, , &block).process end |
#wrap(with:) ⇒ Object
Parse wrap dsl, it creates a WrapAction to wrap current node with code.
318 319 320 |
# File 'lib/synvert/core/rewriter/instance.rb', line 318 def wrap(with:) @current_mutation.wrap(@current_node, with: with) end |