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_node ⇒ Object
Current parsing node.
Class Method Summary collapse
-
.file_ast(file_path) ⇒ String
Get file ast.
-
.file_source(file_path) ⇒ String
Get file source.
-
.reset ⇒ Object
Reset file source and ast.
-
.write_file(file_path, source) ⇒ Object
Write source to file and remove cached file source and ast.
Instance Method Summary collapse
-
#any_value ⇒ Synvert::Core::Rewriter::AnyValue
Match any value but nil.
-
#append(code) ⇒ Object
Parse
append
dsl, it creates a AppendAction to append the code to the bottom of current node body. -
#delete(*selectors, **options) ⇒ Object
Parse
delete
dsl, it creates a DeleteAction to delete child nodes. -
#file_source ⇒ Object
Current file source.
-
#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. -
#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. -
#if_exist_node(rules, &block) ⇒ Object
Parse
if_exist_node
dsl, 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_node
dsl, 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
insert
dsl, it creates a InsertAction to insert code. -
#insert_after(code) ⇒ Object
Parse
insert_after
dsl, 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
prepend
dsl, 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
remove
dsl, it creates a RemoveAction to remove current node. -
#replace(*selectors, with:) ⇒ Object
Parse
replace
dsl, it creates a ReplaceAction to replace the code of specified child nodes. -
#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. -
#replace_with(code) ⇒ Object
Parse
replace_with
dsl, it creates a ReplaceWithAction to replace the whole code of current node. -
#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. -
#warn(message) ⇒ Object
Parse
warn
dsl, it creates a Warning to save warning message. -
#within_node(rules, options = {}) { ... } ⇒ Object
(also: #with_node)
Parse
within_node
dsl, it creates a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node. -
#wrap(with:, indent: nil) ⇒ Object
Parse
wrap
dsl, 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.
77 |
# File 'lib/synvert/core/rewriter/instance.rb', line 77 attr_accessor :current_node, :current_file |
#current_node ⇒ Object
Returns current parsing node.
77 78 79 |
# File 'lib/synvert/core/rewriter/instance.rb', line 77 def current_node @current_node end |
Class Method Details
.file_ast(file_path) ⇒ String
Get file ast.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/synvert/core/rewriter/instance.rb', line 42 def file_ast(file_path) @file_ast ||= {} @file_ast[file_path] ||= begin buffer = Parser::Source::Buffer.new file_path buffer.source = file_source(file_path) parser = Parser::CurrentRuby.new parser.reset parser.parse buffer end end |
.file_source(file_path) ⇒ String
Get file source.
28 29 30 31 32 33 34 35 36 |
# File 'lib/synvert/core/rewriter/instance.rb', line 28 def file_source(file_path) @file_source ||= {} @file_source[file_path] ||= begin source = File.read(file_path, encoding: 'UTF-8') source = Engine::ERB.encode(source) if /\.erb$/.match?(file_path) source end end |
.reset ⇒ Object
Reset file source and ast.
67 68 69 70 |
# File 'lib/synvert/core/rewriter/instance.rb', line 67 def reset @file_source = {} @file_ast = {} end |
.write_file(file_path, source) ⇒ Object
Write source to file and remove cached file source and ast.
59 60 61 62 63 64 |
# File 'lib/synvert/core/rewriter/instance.rb', line 59 def write_file(file_path, source) source = Engine::ERB.decode(source) if /\.erb/.match?(file_path) File.write(file_path, source.gsub(/ +\n/, "\n")) @file_source[file_path] = nil @file_ast[file_path] = nil end |
Instance Method Details
#any_value ⇒ Synvert::Core::Rewriter::AnyValue
Match any value but nil.
390 391 392 |
# File 'lib/synvert/core/rewriter/instance.rb', line 390 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.
233 234 235 |
# File 'lib/synvert/core/rewriter/instance.rb', line 233 def append(code) @actions << Rewriter::AppendAction.new(self, code).process end |
#delete(*selectors, **options) ⇒ Object
Parse delete
dsl, it creates a DeleteAction to delete child nodes.
353 354 355 |
# File 'lib/synvert/core/rewriter/instance.rb', line 353 def delete(*selectors, **) @actions << Rewriter::DeleteAction.new(self, *selectors, **).process end |
#file_source ⇒ Object
Current file source
80 81 82 |
# File 'lib/synvert/core/rewriter/instance.rb', line 80 def file_source self.class.file_source(current_file) 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.
138 139 140 |
# File 'lib/synvert/core/rewriter/instance.rb', line 138 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.
171 172 173 |
# File 'lib/synvert/core/rewriter/instance.rb', line 171 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.
185 186 187 |
# File 'lib/synvert/core/rewriter/instance.rb', line 185 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.
214 215 216 |
# File 'lib/synvert/core/rewriter/instance.rb', line 214 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.
267 268 269 |
# File 'lib/synvert/core/rewriter/instance.rb', line 267 def insert(code, at: 'end', to: nil) @actions << Rewriter::InsertAction.new(self, code, at: at, to: to).process 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.
282 283 284 |
# File 'lib/synvert/core/rewriter/instance.rb', line 282 def insert_after(code) @actions << Rewriter::InsertAfterAction.new(self, code).process end |
#node ⇒ Parser::AST::Node
Gets current node, it allows to get current node in block code.
100 101 102 |
# File 'lib/synvert/core/rewriter/instance.rb', line 100 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.
252 253 254 |
# File 'lib/synvert/core/rewriter/instance.rb', line 252 def prepend(code) @actions << Rewriter::PrependAction.new(self, code).process 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.
87 88 89 90 91 92 93 94 95 |
# File 'lib/synvert/core/rewriter/instance.rb', line 87 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.
108 109 110 111 112 |
# File 'lib/synvert/core/rewriter/instance.rb', line 108 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.
118 119 120 121 122 123 |
# File 'lib/synvert/core/rewriter/instance.rb', line 118 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.
338 339 340 |
# File 'lib/synvert/core/rewriter/instance.rb', line 338 def remove(**) @actions << Rewriter::RemoveAction.new(self, **).process end |
#replace(*selectors, with:) ⇒ Object
Parse replace
dsl, it creates a ReplaceAction to replace the code of specified child nodes.
312 313 314 |
# File 'lib/synvert/core/rewriter/instance.rb', line 312 def replace(*selectors, with:) @actions << Rewriter::ReplaceAction.new(self, *selectors, with: with).process 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.
327 328 329 |
# File 'lib/synvert/core/rewriter/instance.rb', line 327 def replace_erb_stmt_with_expr @actions << Rewriter::ReplaceErbStmtWithExprAction.new(self).process end |
#replace_with(code) ⇒ Object
Parse replace_with
dsl, it creates a ReplaceWithAction to replace the whole code of current node.
296 297 298 |
# File 'lib/synvert/core/rewriter/instance.rb', line 296 def replace_with(code) @actions << Rewriter::ReplaceWithAction.new(self, code).process 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.
199 200 201 |
# File 'lib/synvert/core/rewriter/instance.rb', line 199 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.
382 383 384 |
# File 'lib/synvert/core/rewriter/instance.rb', line 382 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.
153 154 155 156 157 |
# File 'lib/synvert/core/rewriter/instance.rb', line 153 def within_node(rules, = {}, &block) [:stop_when_match] ||= false [:direct] ||= false Rewriter::WithinScope.new(self, rules, , &block).process end |
#wrap(with:, indent: nil) ⇒ Object
Parse wrap
dsl, it creates a WrapAction to wrap current node with code.
372 373 374 |
# File 'lib/synvert/core/rewriter/instance.rb', line 372 def wrap(with:, indent: nil) @actions << Rewriter::WrapAction.new(self, with: with, indent: indent).process end |