Class: RSpec::Support::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source/node.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source/token.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source/location.rb

Overview

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

Defined Under Namespace

Classes: ExpressionSequenceNode, File, Location, Node, Token

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_string, path = nil) ⇒ Source

:nocov:



33
34
35
36
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 33

def initialize(source_string, path=nil)
  @source = RSpec::Support::EncodedString.new(source_string, Encoding.default_external)
  @path = path ? File.expand_path(path) : '(string)'
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 9

def path
  @path
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 9

def source
  @source
end

Class Method Details

.from_file(path) ⇒ Object



21
22
23
24
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 21

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

Instance Method Details

#astObject



52
53
54
55
56
57
58
59
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 52

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

#inspectObject



44
45
46
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 44

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

#linesObject



40
41
42
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 40

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

#nodes_by_line_numberObject



69
70
71
72
73
74
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 69

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

#tokensObject



61
62
63
64
65
66
67
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 61

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

#tokens_by_line_numberObject



76
77
78
79
80
81
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-support-3.12.0/lib/rspec/support/source.rb', line 76

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