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.
Instance Method Summary collapse
-
#add_action(action) ⇒ Object
Add a custom action.
-
#add_leading_spaces(str, tab_size: 1) ⇒ String
Add leading spaces before the str according to Configuration.tab_width.
-
#append(code) ⇒ Object
It appends the code to the bottom of current node body.
-
#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.
-
#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.
-
#indent(tab_size: 1) ⇒ Object
It indent the code of current node.
-
#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.
-
#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(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, #reject_keys_from_hash, #strip_brackets
Constructor Details
#initialize(rewriter, file_path) { ... } ⇒ Instance
Initialize an Instance.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/synvert/core/rewriter/instance.rb', line 21 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, 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.
40 |
# File 'lib/synvert/core/rewriter/instance.rb', line 40 attr_reader :file_path, :current_node, :mutation_adapter |
#file_path ⇒ Object (readonly)
Returns file path.
40 41 42 |
# File 'lib/synvert/core/rewriter/instance.rb', line 40 def file_path @file_path end |
#mutation_adapter ⇒ Object (readonly)
Returns the value of attribute mutation_adapter.
40 |
# File 'lib/synvert/core/rewriter/instance.rb', line 40 attr_reader :file_path, :current_node, :mutation_adapter |
Instance Method Details
#add_action(action) ⇒ Object
Add a custom action.
425 426 427 |
# File 'lib/synvert/core/rewriter/instance.rb', line 425 def add_action(action) @current_mutation.actions << action.process end |
#add_leading_spaces(str, tab_size: 1) ⇒ String
Add leading spaces before the str according to Configuration.tab_width.
457 458 459 |
# File 'lib/synvert/core/rewriter/instance.rb', line 457 def add_leading_spaces(str, tab_size: 1) (" " * Configuration.tab_width * tab_size) + str; end |
#append(code) ⇒ Object
It appends the code to the bottom of current node body.
231 232 233 |
# File 'lib/synvert/core/rewriter/instance.rb', line 231 def append(code) @current_mutation.append(@current_node, code) end |
#delete(*selectors, and_comma: false) ⇒ Object
It deletes child nodes.
368 369 370 |
# File 'lib/synvert/core/rewriter/instance.rb', line 368 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.
170 171 172 |
# File 'lib/synvert/core/rewriter/instance.rb', line 170 def goto_node(child_node_name, &block) Rewriter::GotoScope.new(self, child_node_name, &block).process end |
#group(&block) ⇒ Object
Group actions.
416 417 418 |
# File 'lib/synvert/core/rewriter/instance.rb', line 416 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.
184 185 186 |
# File 'lib/synvert/core/rewriter/instance.rb', line 184 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.
213 214 215 |
# File 'lib/synvert/core/rewriter/instance.rb', line 213 def if_only_exist_node(nql_or_rules, &block) Rewriter::IfOnlyExistCondition.new(self, nql_or_rules, &block).process end |
#indent(tab_size: 1) ⇒ Object
It indent the code of current node.
401 402 403 |
# File 'lib/synvert/core/rewriter/instance.rb', line 401 def indent(tab_size: 1) @current_mutation.indent(@current_node, tab_size: tab_size) end |
#insert(code, at: 'end', to: nil, and_comma: false) ⇒ Object
It inserts code.
265 266 267 |
# File 'lib/synvert/core/rewriter/instance.rb', line 265 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.
281 282 283 284 |
# File 'lib/synvert/core/rewriter/instance.rb', line 281 def insert_after(code, to: nil, and_comma: false) column = ' ' * NodeMutation.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.
298 299 300 301 |
# File 'lib/synvert/core/rewriter/instance.rb', line 298 def insert_before(code, to: nil, and_comma: false) column = ' ' * NodeMutation.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 |
#node ⇒ Parser::AST::Node
Gets current node, it allows to get current node in block code.
107 108 109 |
# File 'lib/synvert/core/rewriter/instance.rb', line 107 def node @current_node end |
#noop ⇒ Object
No operation.
406 407 408 |
# File 'lib/synvert/core/rewriter/instance.rb', line 406 def noop @current_mutation.noop(@current_node) end |
#prepend(code) ⇒ Object
It prepends the code to the top of current node body.
249 250 251 |
# File 'lib/synvert/core/rewriter/instance.rb', line 249 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.
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 |
# File 'lib/synvert/core/rewriter/instance.rb', line 46 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. loop do source = read_source(absolute_file_path) encoded_source = Engine.encode(File.extname(file_path), source) @current_mutation = NodeMutation.new(source) @current_mutation.transform_proc = Engine.generate_transform_proc(File.extname(file_path), encoded_source) @mutation_adapter = NodeMutation.adapter begin node = parse_code(@file_path, encoded_source) process_with_node(node) do instance_eval(&@block) 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 => e puts "[Warn] file #{file_path} was not parsed correctly." puts e. break end end end |
#process_with_node(node) { ... } ⇒ Object
Set current_node to node and process.
115 116 117 118 119 |
# File 'lib/synvert/core/rewriter/instance.rb', line 115 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.
125 126 127 128 129 130 |
# File 'lib/synvert/core/rewriter/instance.rb', line 125 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.
354 355 356 |
# File 'lib/synvert/core/rewriter/instance.rb', line 354 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.
344 345 346 |
# File 'lib/synvert/core/rewriter/instance.rb', line 344 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.
313 314 315 316 317 318 |
# File 'lib/synvert/core/rewriter/instance.rb', line 313 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) add_action(action) end |
#replace_with(code) ⇒ Object
It replaces the whole code of current node.
329 330 331 |
# File 'lib/synvert/core/rewriter/instance.rb', line 329 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.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/synvert/core/rewriter/instance.rb', line 81 def test absolute_file_path = File.join(Configuration.root_path, file_path) source = read_source(absolute_file_path) @current_mutation = NodeMutation.new(source) encoded_source = Engine.encode(File.extname(file_path), source) @current_mutation.transform_proc = Engine.generate_transform_proc(File.extname(file_path), encoded_source) @mutation_adapter = NodeMutation.adapter begin node = parse_code(file_path, encoded_source) process_with_node(node) do instance_eval(&@block) end result = @current_mutation.test result.file_path = file_path result rescue Parser::SyntaxError => e puts "[Warn] file #{file_path} was not parsed correctly." puts e. 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.
198 199 200 |
# File 'lib/synvert/core/rewriter/instance.rb', line 198 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.
435 436 437 |
# File 'lib/synvert/core/rewriter/instance.rb', line 435 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.
151 152 153 154 155 |
# File 'lib/synvert/core/rewriter/instance.rb', line 151 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(prefix:, suffix:, newline: false) ⇒ Object
It wraps current node with prefix and suffix code.
387 388 389 |
# File 'lib/synvert/core/rewriter/instance.rb', line 387 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.
442 443 444 445 446 447 448 449 450 451 |
# File 'lib/synvert/core/rewriter/instance.rb', line 442 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 |