Class: Astel::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/astel/source_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, path:, version: nil) ⇒ SourceFile

Returns a new instance of SourceFile.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/astel/source_file.rb', line 17

def initialize(code, path:, version: nil)
  @path = path.to_s.freeze
  raw_source = code.dup.freeze
  options = { filepath: @path }
  options[:version] = normalize_version(version) if version
  @parse_result = Prism.parse(raw_source, **options)
  @source = @parse_result.source.source.freeze
  @ast = @parse_result.value
  @comments = @parse_result.comments.freeze
  @errors = @parse_result.errors.freeze
  @lines = nil
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



7
8
9
# File 'lib/astel/source_file.rb', line 7

def ast
  @ast
end

#commentsObject (readonly)

Returns the value of attribute comments.



7
8
9
# File 'lib/astel/source_file.rb', line 7

def comments
  @comments
end

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/astel/source_file.rb', line 7

def errors
  @errors
end

#parse_resultObject (readonly)

Returns the value of attribute parse_result.



7
8
9
# File 'lib/astel/source_file.rb', line 7

def parse_result
  @parse_result
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/astel/source_file.rb', line 7

def path
  @path
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/astel/source_file.rb', line 7

def source
  @source
end

Class Method Details

.from_string(code, path: '(string)', version: nil) ⇒ Object



13
14
15
# File 'lib/astel/source_file.rb', line 13

def self.from_string(code, path: '(string)', version: nil)
  new(code, path: path, version: version)
end

.parse(path:, version: nil) ⇒ Object



9
10
11
# File 'lib/astel/source_file.rb', line 9

def self.parse(path:, version: nil)
  from_string(File.binread(path), path: path, version: version)
end

Instance Method Details

#bofObject



38
39
40
# File 'lib/astel/source_file.rb', line 38

def bof
  Location.point(offset: 0, line: 1, column: 0)
end

#first_line_locationObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/astel/source_file.rb', line 42

def first_line_location
  line = lines.first || ''
  Location.new(
    start_offset: 0,
    end_offset: line.bytesize,
    start_line: 1,
    start_column: 0,
    end_line: 1,
    end_column: line.delete_suffix("\n").delete_suffix("\r").length
  )
end

#linesObject



30
31
32
# File 'lib/astel/source_file.rb', line 30

def lines
  @lines ||= source.lines(chomp: false).map!(&:freeze).freeze
end

#magic_comment?(name) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/astel/source_file.rb', line 54

def magic_comment?(name)
  pattern = /\A#\s*#{Regexp.escape(name.to_s)}\s*:\s*true\b/
  lines.first(2).any? do |line|
    candidate = line.start_with?("\uFEFF") ? line.delete_prefix("\uFEFF") : line
    candidate.match?(pattern)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/astel/source_file.rb', line 34

def valid?
  errors.empty?
end