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 ⇒ NodeQuery::AnyValue
Match any value but nil.
-
#append(code) ⇒ Object
It appends the code to the bottom of current node body.
-
#delete(*selectors, **options) ⇒ Object
It deletes child nodes.
-
#goto_node(child_node_name, &block) ⇒ Object
It creates a GotoScope to go to a child node, then continue operating on the child node.
-
#if_exist_node(nql_or_rules, &block) ⇒ Object
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(nql_or_rules, &block) ⇒ Object
It creates a IfOnlyExistCondition to check if current node has only one child node and the child node matches, 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
It inserts code.
-
#insert_after(code, to: nil) ⇒ Object
It inserts the code next to the current node.
-
#insert_before(code, to: nil) ⇒ Object
It inserts the code previous to the current node.
-
#node ⇒ Parser::AST::Node
Gets current node, it allows to get current node in block code.
-
#noop ⇒ Object
No operation.
-
#prepend(code) ⇒ Object
It prepends 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
It removes current node.
-
#replace(*selectors, with:) ⇒ Object
It replaces the code of specified child nodes.
-
#replace_erb_stmt_with_expr ⇒ Object
It replaces erb stmt code to expr code.
-
#replace_with(code) ⇒ Object
It replaces the whole code of current node.
-
#test ⇒ Object
Test the instance.
-
#unless_exist_node(nql_or_rules, &block) ⇒ Object
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
It creates a Warning to save warning message.
-
#within_node(nql_or_rules, options = {}) { ... } ⇒ Object
(also: #with_node, #find_node)
It creates a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node.
-
#wrap(with:) ⇒ Object
It wraps 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.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/synvert/core/rewriter/instance.rb', line 17 def initialize(rewriter, file_patterns, &block) @rewriter = rewriter @actions = [] @file_patterns = file_patterns @block = block strategy = NodeMutation::Strategy::KEEP_RUNNING if rewriter.[:strategy] == Strategy::ALLOW_INSERT_AT_SAME_POSITION strategy |= NodeMutation::Strategy::ALLOW_INSERT_AT_SAME_POSITION end NodeMutation.configure({ strategy: strategy }) rewriter.helpers.each { |helper| singleton_class.send(:define_method, helper[:name], &helper[:block]) } end |
Instance Attribute Details
#current_file ⇒ Object
Returns current filename.
36 |
# File 'lib/synvert/core/rewriter/instance.rb', line 36 attr_accessor :current_node, :current_file, :current_mutation |
#current_mutation ⇒ Object
Returns current mutation.
36 |
# File 'lib/synvert/core/rewriter/instance.rb', line 36 attr_accessor :current_node, :current_file, :current_mutation |
#current_node ⇒ Object
Returns current parsing node.
36 37 38 |
# File 'lib/synvert/core/rewriter/instance.rb', line 36 def current_node @current_node end |
Instance Method Details
#any_value ⇒ NodeQuery::AnyValue
Match any value but nil.
360 361 362 |
# File 'lib/synvert/core/rewriter/instance.rb', line 360 def any_value NodeQuery::AnyValue.new end |
#append(code) ⇒ Object
It appends the code to the bottom of current node body.
189 190 191 |
# File 'lib/synvert/core/rewriter/instance.rb', line 189 def append(code) @current_mutation.append(@current_node, code) end |
#delete(*selectors, **options) ⇒ Object
It deletes child nodes.
320 321 322 |
# File 'lib/synvert/core/rewriter/instance.rb', line 320 def delete(*selectors, **) @current_mutation.delete(@current_node, *selectors, **) end |
#goto_node(child_node_name, &block) ⇒ Object
It creates a GotoScope to go to a child node, then continue operating on the child node.
128 129 130 |
# File 'lib/synvert/core/rewriter/instance.rb', line 128 def goto_node(child_node_name, &block) Rewriter::GotoScope.new(self, child_node_name, &block).process end |
#if_exist_node(nql_or_rules, &block) ⇒ Object
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.
142 143 144 |
# File 'lib/synvert/core/rewriter/instance.rb', line 142 def if_exist_node(nql_or_rules, &block) Rewriter::IfExistCondition.new(self, nql_or_rules, &block).process end |
#if_only_exist_node(nql_or_rules, &block) ⇒ Object
It creates a Synvert::Core::Rewriter::IfOnlyExistCondition to check if current node has only one child node and the child node matches, if so, then continue operating on each matching ast node.
171 172 173 |
# File 'lib/synvert/core/rewriter/instance.rb', line 171 def if_only_exist_node(nql_or_rules, &block) Rewriter::IfOnlyExistCondition.new(self, nql_or_rules, &block).process end |
#insert(code, at: 'end', to: nil) ⇒ Object
It inserts code.
222 223 224 |
# File 'lib/synvert/core/rewriter/instance.rb', line 222 def insert(code, at: 'end', to: nil) @current_mutation.insert(@current_node, code, at: at, to: to) end |
#insert_after(code, to: nil) ⇒ Object
It inserts the code next to the current node.
236 237 238 239 |
# File 'lib/synvert/core/rewriter/instance.rb', line 236 def insert_after(code, to: nil) column = ' ' * NodeMutation.adapter.get_start_loc(@current_node).column @current_mutation.insert(@current_node, "\n#{column}#{code}", at: 'end', to: to) end |
#insert_before(code, to: nil) ⇒ Object
It inserts the code previous to the current node.
251 252 253 254 |
# File 'lib/synvert/core/rewriter/instance.rb', line 251 def insert_before(code, to: nil) column = ' ' * NodeMutation.adapter.get_start_loc(@current_node).column @current_mutation.insert(@current_node, "#{code}\n#{column}", at: 'beginning', to: to) end |
#node ⇒ Parser::AST::Node
Gets current node, it allows to get current node in block code.
65 66 67 |
# File 'lib/synvert/core/rewriter/instance.rb', line 65 def node @current_node end |
#noop ⇒ Object
No operation.
342 343 344 |
# File 'lib/synvert/core/rewriter/instance.rb', line 342 def noop @current_mutation.noop(@current_node) end |
#prepend(code) ⇒ Object
It prepends the code to the top of current node body.
207 208 209 |
# File 'lib/synvert/core/rewriter/instance.rb', line 207 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.
41 42 43 44 45 |
# File 'lib/synvert/core/rewriter/instance.rb', line 41 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.
73 74 75 76 77 |
# File 'lib/synvert/core/rewriter/instance.rb', line 73 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.
83 84 85 86 87 88 |
# File 'lib/synvert/core/rewriter/instance.rb', line 83 def process_with_other_node(node) original_node = current_node self.current_node = node yield self.current_node = original_node end |
#remove(**options) ⇒ Object
It removes current node.
305 306 307 |
# File 'lib/synvert/core/rewriter/instance.rb', line 305 def remove(**) @current_mutation.remove(@current_node, **) end |
#replace(*selectors, with:) ⇒ Object
It replaces the code of specified child nodes.
294 295 296 |
# File 'lib/synvert/core/rewriter/instance.rb', line 294 def replace(*selectors, with:) @current_mutation.replace(@current_node, *selectors, with: with) end |
#replace_erb_stmt_with_expr ⇒ Object
It replaces erb stmt code to expr code.
266 267 268 |
# File 'lib/synvert/core/rewriter/instance.rb', line 266 def replace_erb_stmt_with_expr @current_mutation.actions << Rewriter::ReplaceErbStmtWithExprAction.new(@current_node).process end |
#replace_with(code) ⇒ Object
It replaces the whole code of current node.
279 280 281 |
# File 'lib/synvert/core/rewriter/instance.rb', line 279 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.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/synvert/core/rewriter/instance.rb', line 50 def test if Configuration.number_of_workers > 1 Parallel.map(get_file_paths, in_processes: Configuration.number_of_workers) do |file_path| test_file(file_path) end else get_file_paths.map do |file_path| test_file(file_path) end end end |
#unless_exist_node(nql_or_rules, &block) ⇒ Object
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.
156 157 158 |
# File 'lib/synvert/core/rewriter/instance.rb', line 156 def unless_exist_node(nql_or_rules, &block) Rewriter::UnlessExistCondition.new(self, nql_or_rules, &block).process end |
#warn(message) ⇒ Object
It creates a Warning to save warning message.
352 353 354 |
# File 'lib/synvert/core/rewriter/instance.rb', line 352 def warn() @rewriter.add_warning Rewriter::Warning.new(self, ) end |
#within_node(nql_or_rules, options = {}) { ... } ⇒ Object Also known as: with_node, find_node
It creates a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node.
109 110 111 112 113 |
# File 'lib/synvert/core/rewriter/instance.rb', line 109 def within_node(nql_or_rules, = {}, &block) Rewriter::WithinScope.new(self, nql_or_rules, , &block).process rescue NodeQueryLexer::ScanError, Racc::ParseError => e raise NodeQuery::Compiler::ParseError, "Invalid query string: #{nql_or_rules}" end |
#wrap(with:) ⇒ Object
It wraps current node with code.
337 338 339 |
# File 'lib/synvert/core/rewriter/instance.rb', line 337 def wrap(with:) @current_mutation.wrap(@current_node, with: with) end |