Class: RSpec::Support::Source
- Inherits:
-
Object
- Object
- RSpec::Support::Source
show all
- Defined in:
- lib/rspec/support/source.rb,
lib/rspec/support/source/node.rb,
lib/rspec/support/source/token.rb,
lib/rspec/support/source/location.rb
Overview
Represents a Ruby source file and provides access to AST and tokens.
Defined Under Namespace
Classes: ExpressionSequenceNode, Location, Node, Token
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(source_string, path = nil) ⇒ Source
23
24
25
26
|
# File 'lib/rspec/support/source.rb', line 23
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
#path ⇒ Object
Returns the value of attribute path.
9
10
11
|
# File 'lib/rspec/support/source.rb', line 9
def path
@path
end
|
#source ⇒ Object
Returns the value of attribute source.
9
10
11
|
# File 'lib/rspec/support/source.rb', line 9
def source
@source
end
|
Class Method Details
.from_file(path) ⇒ Object
11
12
13
14
|
# File 'lib/rspec/support/source.rb', line 11
def self.from_file(path)
source = File.read(path)
new(source, path)
end
|
Instance Method Details
#ast ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/rspec/support/source.rb', line 42
def ast
@ast ||= begin
require 'ripper'
sexp = Ripper.sexp(source)
raise SyntaxError unless sexp
Node.new(sexp)
end
end
|
#inspect ⇒ Object
34
35
36
|
# File 'lib/rspec/support/source.rb', line 34
def inspect
"#<#{self.class} #{path}>"
end
|
#lines ⇒ Object
30
31
32
|
# File 'lib/rspec/support/source.rb', line 30
def lines
@lines ||= source.split("\n")
end
|
#nodes_by_line_number ⇒ Object
59
60
61
62
63
64
|
# File 'lib/rspec/support/source.rb', line 59
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
|
#tokens ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/rspec/support/source.rb', line 51
def tokens
@tokens ||= begin
require 'ripper'
tokens = Ripper.lex(source)
Token.tokens_from_ripper_tokens(tokens)
end
end
|
#tokens_by_line_number ⇒ Object
66
67
68
69
70
71
|
# File 'lib/rspec/support/source.rb', line 66
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
|