module Overloader::AstExt
Public Instance Methods
comment(content: nil, path: nil)
click to toggle source
# File lib/overloader/ast_ext.rb, line 23 def comment(content: nil, path: nil) raise ArgumentError.new("content or path is required") unless content || path content ||= file_content(path) l = first_lineno - 2 res = [] while (comment = content.lines[l])&.match?(/^\s*#/) res.unshift comment l -= 1 end res.empty? ? nil : res.join end
file_content(path)
click to toggle source
# File lib/overloader/ast_ext.rb, line 70 def file_content(path) @file_content ||= {} @file_content[path] ||= File.binread(path) end
find_nodes(*types)
click to toggle source
# File lib/overloader/ast_ext.rb, line 11 def find_nodes(*types) nodes = [] traverse do |node| nodes << node if types.include?(node.type) end nodes end
first_index(path)
click to toggle source
# File lib/overloader/ast_ext.rb, line 51 def first_index(path) return first_column if first_lineno == 1 lines = file_content(path).split("\n") lines[0..(first_lineno - 2)].sum(&:size) + first_lineno - 1 + # For \n first_column end
last_index(path)
click to toggle source
# File lib/overloader/ast_ext.rb, line 60 def last_index(path) last_column = self.last_column - 1 return last_column if last_lineno == 1 lines = file_content(path).split("\n") lines[0..(last_lineno - 2)].sum(&:size) + last_lineno - 1 + # For \n last_column end
method_args()
click to toggle source
# File lib/overloader/ast_ext.rb, line 43 def method_args children[1].children[1] end
method_body()
click to toggle source
method node ext
# File lib/overloader/ast_ext.rb, line 39 def method_body children[1].children[2] end
method_name()
click to toggle source
# File lib/overloader/ast_ext.rb, line 47 def method_name children[0] end
to_source(path)
click to toggle source
# File lib/overloader/ast_ext.rb, line 19 def to_source(path) file_content(path)[first_index(path)..last_index(path)] end
traverse(&block)
click to toggle source
# File lib/overloader/ast_ext.rb, line 4 def traverse(&block) block.call(self) children.each do |child| child.traverse(&block) if child.is_a?(RubyVM::AbstractSyntaxTree::Node) end end