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_node ⇒ Object
Current ast node.
-
#file_path ⇒ Object
readonly
File path.
-
#mutation_adapter ⇒ Object
readonly
Returns the value of attribute mutation_adapter.
-
#query_adapter ⇒ Object
readonly
NodeQuery Adapter.
Instance Method Summary collapse
-
#add_action(action) ⇒ Object
Add a custom action.
-
#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_path) { ... } ⇒ 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_path) { ... } ⇒ Instance
Initialize an Instance.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/synvert/core/rewriter/instance.rb', line 16 def initialize(rewriter, file_path, &block) @rewriter = rewriter @actions = [] @file_path = file_path @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_node ⇒ Object
Returns current ast node.
37 |
# File 'lib/synvert/core/rewriter/instance.rb', line 37 attr_reader :file_path, :current_node, :query_adapter, :mutation_adapter |
#file_path ⇒ Object (readonly)
Returns file path.
37 38 39 |
# File 'lib/synvert/core/rewriter/instance.rb', line 37 def file_path @file_path end |
#mutation_adapter ⇒ Object (readonly)
Returns the value of attribute mutation_adapter.
37 |
# File 'lib/synvert/core/rewriter/instance.rb', line 37 attr_reader :file_path, :current_node, :query_adapter, :mutation_adapter |
#query_adapter ⇒ Object (readonly)
Returns NodeQuery Adapter.
37 |
# File 'lib/synvert/core/rewriter/instance.rb', line 37 attr_reader :file_path, :current_node, :query_adapter, :mutation_adapter |
Instance Method Details
#add_action(action) ⇒ Object
Add a custom action.
403 404 405 |
# File 'lib/synvert/core/rewriter/instance.rb', line 403 def add_action(action) @current_mutation.actions << action.process end |
#append(code) ⇒ Object
It appends the code to the bottom of current node body.
241 242 243 |
# File 'lib/synvert/core/rewriter/instance.rb', line 241 def append(code) @current_mutation.append(@current_node, code) end |
#delete(*selectors, **options) ⇒ Object
It deletes child nodes.
372 373 374 |
# File 'lib/synvert/core/rewriter/instance.rb', line 372 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.
180 181 182 |
# File 'lib/synvert/core/rewriter/instance.rb', line 180 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.
194 195 196 |
# File 'lib/synvert/core/rewriter/instance.rb', line 194 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.
223 224 225 |
# File 'lib/synvert/core/rewriter/instance.rb', line 223 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.
274 275 276 |
# File 'lib/synvert/core/rewriter/instance.rb', line 274 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.
288 289 290 291 |
# File 'lib/synvert/core/rewriter/instance.rb', line 288 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.
303 304 305 306 |
# File 'lib/synvert/core/rewriter/instance.rb', line 303 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.
117 118 119 |
# File 'lib/synvert/core/rewriter/instance.rb', line 117 def node @current_node end |
#noop ⇒ Object
No operation.
394 395 396 |
# File 'lib/synvert/core/rewriter/instance.rb', line 394 def noop @current_mutation.noop(@current_node) end |
#prepend(code) ⇒ Object
It prepends the code to the top of current node body.
259 260 261 |
# File 'lib/synvert/core/rewriter/instance.rb', line 259 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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/synvert/core/rewriter/instance.rb', line 43 def process puts @file_path if Configuration.show_run_process absolute_file_path = File.join(Configuration.root_path, @file_path) while true source = read_source(absolute_file_path) @current_mutation = NodeMutation.new(source) @mutation_adapter = NodeMutation.adapter @query_adapter = NodeQuery.adapter begin node = parse_code(@file_path, source) process_with_node(node) do instance_eval(&@block) rescue NoMethodError => e puts [ "error: #{e.}", "file: #{file_path}", "source: #{source}", "line: #{current_node.line}" ].join("\n") raise end result = @current_mutation.process if result.affected? @rewriter.add_affected_file(file_path) write_source(absolute_file_path, result.new_source) end break unless result.conflicted? rescue Parser::SyntaxError puts "[Warn] file #{file_path} was not parsed correctly." # do nothing, iterate next file end end end |
#process_with_node(node) { ... } ⇒ Object
Set current_node to node and process.
125 126 127 128 129 |
# File 'lib/synvert/core/rewriter/instance.rb', line 125 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.
135 136 137 138 139 140 |
# File 'lib/synvert/core/rewriter/instance.rb', line 135 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.
357 358 359 |
# File 'lib/synvert/core/rewriter/instance.rb', line 357 def remove(**) @current_mutation.remove(@current_node, **) end |
#replace(*selectors, with:) ⇒ Object
It replaces the code of specified child nodes.
346 347 348 |
# File 'lib/synvert/core/rewriter/instance.rb', line 346 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.
318 319 320 |
# File 'lib/synvert/core/rewriter/instance.rb', line 318 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.
331 332 333 |
# File 'lib/synvert/core/rewriter/instance.rb', line 331 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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/synvert/core/rewriter/instance.rb', line 83 def test absolute_file_path = File.join(Configuration.root_path, file_path) source = read_source(absolute_file_path) @current_mutation = NodeMutation.new(source) @mutation_adapter = NodeMutation.adapter @query_adapter = NodeQuery.adapter begin node = parse_code(file_path, source) process_with_node(node) do instance_eval(&@block) rescue NoMethodError => e puts [ "error: #{e.}", "file: #{file_path}", "source: #{source}", "line: #{current_node.line}" ].join("\n") raise end result = @current_mutation.test result.file_path = file_path result rescue Parser::SyntaxError puts "[Warn] file #{file_path} was not parsed correctly." # do nothing, iterate next file 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.
208 209 210 |
# File 'lib/synvert/core/rewriter/instance.rb', line 208 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.
413 414 415 |
# File 'lib/synvert/core/rewriter/instance.rb', line 413 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.
161 162 163 164 165 |
# File 'lib/synvert/core/rewriter/instance.rb', line 161 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.
389 390 391 |
# File 'lib/synvert/core/rewriter/instance.rb', line 389 def wrap(with:) @current_mutation.wrap(@current_node, with: with) end |