123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Support::Source Private

Overview

Represents a Ruby source file and provides access to AST and tokens.

Class Method Summary

Instance Attribute Summary

  • #path readonly Internal use only Internal use only
  • #source readonly Internal use only Internal use only

Instance Method Summary

Class Method Details

.from_file(path)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source.rb', line 23

def self.from_file(path)
  source = File.read(path)
  new(source, path)
end

Instance Attribute Details

#path (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source.rb', line 11

attr_reader :source, :path

#source (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source.rb', line 11

attr_reader :source, :path

Instance Method Details

#ast

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source.rb', line 54

def ast
  @ast ||= begin
    require 'ripper'
    sexp = Ripper.sexp(source)
    raise SyntaxError unless sexp
    Node.new(sexp)
  end
end

#inspect

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source.rb', line 46

def inspect
  "#<#{self.class} #{path}>"
end

#lines

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source.rb', line 42

def lines
  @lines ||= source.split("\n")
end

#nodes_by_line_number

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source.rb', line 71

def 
  @nodes_by_line_number ||= begin
     = ast.select(&:location).group_by { |node| node.location.line }
    Hash.new { |hash, key| hash[key] = [] }.merge()
  end
end

#tokens

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source.rb', line 63

def tokens
  @tokens ||= begin
    require 'ripper'
    tokens = Ripper.lex(source)
    Token.tokens_from_ripper_tokens(tokens)
  end
end

#tokens_by_line_number

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/source.rb', line 78

def 
  @tokens_by_line_number ||= begin
     = tokens.group_by { |token| token.location.line }
    Hash.new { |hash, key| hash[key] = [] }.merge()
  end
end