class RSpec::Support::Source
Represents a Ruby source file and provides access to AST and tokens.
@private
def self.from_file(path)
def self.from_file(path) source = File.read(path) new(source, path) end
def ast
def ast @ast ||= begin require 'ripper' sexp = Ripper.sexp(source) raise SyntaxError unless sexp Node.new(sexp) end end
def initialize(source_string, path=nil)
def initialize(source_string, path=nil) @source = RSpec::Support::EncodedString.new(source_string, Encoding.default_external) @path = path ? File.expand_path(path) : '(string)' end
def initialize(source_string, path=nil)
for 1.8.7
def initialize(source_string, path=nil) @source = RSpec::Support::EncodedString.new(source_string) @path = path ? File.expand_path(path) : '(string)' end
def inspect
def inspect "#<#{self.class} #{path}>" end
def lines
def lines @lines ||= source.split("\n") end
def nodes_by_line_number
def nodes_by_line_number @nodes_by_line_number ||= begin nodes_by_line_number = ast.select(&:location).group_by { |node| node.location.line } Hash.new { |hash, key| hash[key] = [] }.merge(nodes_by_line_number) end end
def tokens
def tokens @tokens ||= begin require 'ripper' tokens = Ripper.lex(source) Token.tokens_from_ripper_tokens(tokens) end end
def tokens_by_line_number
def tokens_by_line_number @tokens_by_line_number ||= begin nodes_by_line_number = tokens.group_by { |token| token.location.line } Hash.new { |hash, key| hash[key] = [] }.merge(nodes_by_line_number) end end