Class: Synvert::Core::Rewriter::Instance
- Inherits:
-
Object
- Object
- Synvert::Core::Rewriter::Instance
- Includes:
- Helper
- Defined in:
- lib/synvert/core/rewriter/instance.rb
Overview
Constant Summary collapse
- DSL_METHODS =
%i[ within_node with_node find_node goto_node if_exist_node unless_exist_node append prepend insert insert_after insert_before replace delete remove wrap replace_with warn replace_erb_stmt_with_expr noop group add_action add_callback wrap_with_quotes add_leading_spaces file_path node mutation_adapter ].freeze
Instance Attribute Summary collapse
-
#current_node ⇒ Object
Current ast node.
-
#current_parser ⇒ Object
readonly
Returns the value of attribute current_parser.
-
#file_path ⇒ Object
readonly
File path.
Instance Method Summary collapse
-
#add_action(action) ⇒ Object
Add a custom action.
-
#add_callback(node_type, at: 'start') { ... } ⇒ Object
It adds a callback when visiting an ast node.
-
#append(code) ⇒ Object
It appends the code to the bottom of current node body.
-
#dedent(source, tab_size: 1) ⇒ String
Dedents the given source code by removing leading spaces or tabs.
-
#delete(*selectors, and_comma: false) ⇒ 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.
-
#group(&block) ⇒ Object
Group actions.
-
#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.
-
#indent(source, tab_size: 1) ⇒ String
Indents the given source code by the specified tab size.
-
#initialize(rewriter, file_path) { ... } ⇒ Instance
constructor
Initialize an Instance.
-
#insert(code, at: 'end', to: nil, and_comma: false) ⇒ Object
It inserts code.
-
#insert_after(code, to: nil, and_comma: false) ⇒ Object
It inserts the code next to the current node.
-
#insert_before(code, to: nil, and_comma: false) ⇒ Object
It inserts the code previous to the current node.
-
#mutation_adapter ⇒ NodeMutation::Adapter
Get current_mutation's adapter.
-
#node ⇒ 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(and_comma: false) ⇒ 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(prefix:, suffix:, newline: false) ⇒ Object
It wraps current node with prefix and suffix code.
-
#wrap_with_quotes(str) ⇒ String
Wrap str string with single or doulbe quotes based on Configuration.single_quote.
Methods included from Helper
#add_arguments_with_parenthesis_if_necessary, #add_curly_brackets_if_necessary, #add_receiver_if_necessary, #strip_brackets
Constructor Details
#initialize(rewriter, file_path) { ... } ⇒ Instance
Initialize an Instance.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/synvert/core/rewriter/instance.rb', line 32 def initialize(rewriter, file_path, &block) @rewriter = rewriter @current_parser = @rewriter.parser @current_visitor = NodeVisitor.new(adapter: @current_parser) @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, tab_width: Configuration.tab_width }) rewriter.helpers.each { |helper| singleton_class.send(:define_method, helper[:name], &helper[:block]) } end |
Instance Attribute Details
#current_node ⇒ Object
Returns current ast node.
53 |
# File 'lib/synvert/core/rewriter/instance.rb', line 53 attr_reader :file_path, :current_parser |
#current_parser ⇒ Object (readonly)
Returns the value of attribute current_parser.
53 |
# File 'lib/synvert/core/rewriter/instance.rb', line 53 attr_reader :file_path, :current_parser |
#file_path ⇒ Object (readonly)
Returns file path.
53 54 55 |
# File 'lib/synvert/core/rewriter/instance.rb', line 53 def file_path @file_path end |
Instance Method Details
#add_action(action) ⇒ Object
Add a custom action.
423 424 425 |
# File 'lib/synvert/core/rewriter/instance.rb', line 423 def add_action(action) @current_mutation.actions << action.process end |
#add_callback(node_type, at: 'start') { ... } ⇒ Object
It adds a callback when visiting an ast node.
446 447 448 |
# File 'lib/synvert/core/rewriter/instance.rb', line 446 def add_callback(node_type, at: 'start', &block) @current_visitor.add_callback(node_type, at: at, &block) end |
#append(code) ⇒ Object
It appends the code to the bottom of current node body.
243 244 245 |
# File 'lib/synvert/core/rewriter/instance.rb', line 243 def append(code) @current_mutation.append(@current_node, code) end |
#dedent(source, tab_size: 1) ⇒ String
Dedents the given source code by removing leading spaces or tabs.
478 479 480 |
# File 'lib/synvert/core/rewriter/instance.rb', line 478 def dedent(source, tab_size: 1) source.each_line.map { |line| line.sub(/^ {#{NodeMutation.tab_width * tab_size}}/, '') }.join end |
#delete(*selectors, and_comma: false) ⇒ Object
It deletes child nodes.
380 381 382 |
# File 'lib/synvert/core/rewriter/instance.rb', line 380 def delete(*selectors, and_comma: false) @current_mutation.delete(@current_node, *selectors, and_comma: and_comma) 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.
197 198 199 |
# File 'lib/synvert/core/rewriter/instance.rb', line 197 def goto_node(child_node_name, &block) Rewriter::GotoScope.new(self, child_node_name, &block).process end |
#group(&block) ⇒ Object
Group actions.
414 415 416 |
# File 'lib/synvert/core/rewriter/instance.rb', line 414 def group(&block) @current_mutation.group(&block) 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.
211 212 213 |
# File 'lib/synvert/core/rewriter/instance.rb', line 211 def if_exist_node(nql_or_rules, &block) Rewriter::IfExistCondition.new(self, nql_or_rules, &block).process end |
#indent(source, tab_size: 1) ⇒ String
Indents the given source code by the specified tab size.
469 470 471 |
# File 'lib/synvert/core/rewriter/instance.rb', line 469 def indent(source, tab_size: 1) source.each_line.map { |line| (' ' * NodeMutation.tab_width * tab_size) + line }.join end |
#insert(code, at: 'end', to: nil, and_comma: false) ⇒ Object
It inserts code.
277 278 279 |
# File 'lib/synvert/core/rewriter/instance.rb', line 277 def insert(code, at: 'end', to: nil, and_comma: false) @current_mutation.insert(@current_node, code, at: at, to: to, and_comma: and_comma) end |
#insert_after(code, to: nil, and_comma: false) ⇒ Object
It inserts the code next to the current node.
293 294 295 296 |
# File 'lib/synvert/core/rewriter/instance.rb', line 293 def insert_after(code, to: nil, and_comma: false) column = ' ' * @current_mutation.adapter.get_start_loc(@current_node, to).column @current_mutation.insert(@current_node, "\n#{column}#{code}", at: 'end', to: to, and_comma: and_comma) end |
#insert_before(code, to: nil, and_comma: false) ⇒ Object
It inserts the code previous to the current node.
310 311 312 313 |
# File 'lib/synvert/core/rewriter/instance.rb', line 310 def insert_before(code, to: nil, and_comma: false) column = ' ' * @current_mutation.adapter.get_start_loc(@current_node, to).column @current_mutation.insert(@current_node, "#{code}\n#{column}", at: 'beginning', to: to, and_comma: and_comma) end |
#mutation_adapter ⇒ NodeMutation::Adapter
Get current_mutation's adapter.
134 135 136 |
# File 'lib/synvert/core/rewriter/instance.rb', line 134 def mutation_adapter @current_mutation.adapter end |
#node ⇒ Node
Gets current node, it allows to get current node in block code.
127 128 129 |
# File 'lib/synvert/core/rewriter/instance.rb', line 127 def node @current_node end |
#noop ⇒ Object
No operation.
404 405 406 |
# File 'lib/synvert/core/rewriter/instance.rb', line 404 def noop @current_mutation.noop(@current_node) end |
#prepend(code) ⇒ Object
It prepends the code to the top of current node body.
261 262 263 |
# File 'lib/synvert/core/rewriter/instance.rb', line 261 def prepend(code) @current_mutation.prepend(@current_node, code) end |
#process ⇒ Object
Process the instance. It executes the block code, rewrites the original code, then writes the code back to the original file.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/synvert/core/rewriter/instance.rb', line 59 def process puts @file_path if Configuration.show_run_process absolute_file_path = File.join(Configuration.root_path, @file_path) # It keeps running until no conflict, # it will try 5 times at maximum. 5.times do source = read_source(absolute_file_path) encoded_source = Engine.encode(File.extname(file_path), source) @current_mutation = NodeMutation.new(source, adapter: @current_parser) @current_mutation.transform_proc = Engine.generate_transform_proc(File.extname(file_path), encoded_source) begin node = parse_code(@file_path, encoded_source) process_with_node(node) do instance_eval(&@block) end @current_visitor.visit(node, self) 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, Prism::ParseError, SyntaxTree::Parser::ParseError => e if ENV['DEBUG'] == 'true' puts "[Warn] file #{file_path} was not parsed correctly." puts e. end break end end end |
#process_with_node(node) { ... } ⇒ Object
Set current_node to node and process.
142 143 144 145 146 |
# File 'lib/synvert/core/rewriter/instance.rb', line 142 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.
152 153 154 155 156 157 |
# File 'lib/synvert/core/rewriter/instance.rb', line 152 def process_with_other_node(node) original_node = current_node self.current_node = node yield self.current_node = original_node end |
#remove(and_comma: false) ⇒ Object
It removes current node.
366 367 368 |
# File 'lib/synvert/core/rewriter/instance.rb', line 366 def remove(and_comma: false) @current_mutation.remove(@current_node, and_comma: and_comma) end |
#replace(*selectors, with:) ⇒ Object
It replaces the code of specified child nodes.
356 357 358 |
# File 'lib/synvert/core/rewriter/instance.rb', line 356 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.
325 326 327 328 329 330 |
# File 'lib/synvert/core/rewriter/instance.rb', line 325 def replace_erb_stmt_with_expr absolute_file_path = File.join(Configuration.root_path, @file_path) erb_source = read_source(absolute_file_path) action = Rewriter::ReplaceErbStmtWithExprAction.new(@current_node, erb_source, adapter: @current_mutation.adapter) add_action(action) end |
#replace_with(code) ⇒ Object
It replaces the whole code of current node.
341 342 343 |
# File 'lib/synvert/core/rewriter/instance.rb', line 341 def replace_with(code) @current_mutation.replace_with(@current_node, code) end |
#test ⇒ Object
Test the instance. It executes the block code, tests the original code, then returns the actions.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/synvert/core/rewriter/instance.rb', line 98 def test absolute_file_path = File.join(Configuration.root_path, file_path) source = read_source(absolute_file_path) @current_mutation = NodeMutation.new(source, adapter: @current_parser) encoded_source = Engine.encode(File.extname(file_path), source) @current_mutation.transform_proc = Engine.generate_transform_proc(File.extname(file_path), encoded_source) begin node = parse_code(file_path, encoded_source) process_with_node(node) do instance_eval(&@block) end @current_visitor.visit(node, self) result = Configuration.test_result == 'new_source' ? @current_mutation.process : @current_mutation.test result.file_path = file_path result rescue Parser::SyntaxError, Prism::ParseError, SyntaxTree::Parser::ParseError => e if ENV['DEBUG'] == 'true' puts "[Warn] file #{file_path} was not parsed correctly." puts e. 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.
225 226 227 |
# File 'lib/synvert/core/rewriter/instance.rb', line 225 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.
433 434 435 436 |
# File 'lib/synvert/core/rewriter/instance.rb', line 433 def warn() line = @current_mutation.adapter.get_start_loc(@current_node).line @rewriter.add_warning Rewriter::Warning.new(@file_path, line, ) 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.
178 179 180 181 182 |
# File 'lib/synvert/core/rewriter/instance.rb', line 178 def within_node(nql_or_rules, = {}, &block) Rewriter::WithinScope.new(self, nql_or_rules, , &block).process rescue NodeQueryLexer::ScanError, Racc::ParseError raise NodeQuery::Compiler::ParseError, "Invalid query string: #{nql_or_rules}" end |
#wrap(prefix:, suffix:, newline: false) ⇒ Object
It wraps current node with prefix and suffix code.
399 400 401 |
# File 'lib/synvert/core/rewriter/instance.rb', line 399 def wrap(prefix:, suffix:, newline: false) @current_mutation.wrap(@current_node, prefix: prefix, suffix: suffix, newline: newline) end |
#wrap_with_quotes(str) ⇒ String
Wrap str string with single or doulbe quotes based on Configuration.single_quote.
453 454 455 456 457 458 459 460 461 462 |
# File 'lib/synvert/core/rewriter/instance.rb', line 453 def wrap_with_quotes(str) quote = Configuration.single_quote ? "'" : '"'; another_quote = Configuration.single_quote ? '"' : "'"; if str.include?(quote) && !str.include?(another_quote) return "#{another_quote}#{str}#{another_quote}" end escaped_str = str.gsub(quote) { |_char| '\\' + quote } quote + escaped_str + quote end |