[ruby/prism] Relax Location#source to be optional

https://github.com/ruby/prism/commit/9f00fe7510
This commit is contained in:
Gopal Patel 2024-01-09 21:13:46 -08:00 committed by git
parent bfbaafbd9c
commit 8fa1843523
2 changed files with 18 additions and 14 deletions

View File

@ -168,13 +168,13 @@ module Prism
# The source code that this location represents. # The source code that this location represents.
def slice def slice
source.slice(start_offset, length) source!.slice(start_offset, length)
end end
# The character offset from the beginning of the source where this location # The character offset from the beginning of the source where this location
# starts. # starts.
def start_character_offset def start_character_offset
source.character_offset(start_offset) source!.character_offset(start_offset)
end end
# The offset from the start of the file in code units of the given encoding. # The offset from the start of the file in code units of the given encoding.
@ -190,7 +190,7 @@ module Prism
# The character offset from the beginning of the source where this location # The character offset from the beginning of the source where this location
# ends. # ends.
def end_character_offset def end_character_offset
source.character_offset(end_offset) source!.character_offset(end_offset)
end end
# The offset from the start of the file in code units of the given encoding. # The offset from the start of the file in code units of the given encoding.
@ -200,30 +200,30 @@ module Prism
# The line number where this location starts. # The line number where this location starts.
def start_line def start_line
source.line(start_offset) source!.line(start_offset)
end end
# The content of the line where this location starts before this location. # The content of the line where this location starts before this location.
def start_line_slice def start_line_slice
offset = source.line_start(start_offset) offset = source!.line_start(start_offset)
source.slice(offset, start_offset - offset) source!.slice(offset, start_offset - offset)
end end
# The line number where this location ends. # The line number where this location ends.
def end_line def end_line
source.line(end_offset) source!.line(end_offset)
end end
# The column number in bytes where this location starts from the start of # The column number in bytes where this location starts from the start of
# the line. # the line.
def start_column def start_column
source.column(start_offset) source!.column(start_offset)
end end
# The column number in characters where this location ends from the start of # The column number in characters where this location ends from the start of
# the line. # the line.
def start_character_column def start_character_column
source.character_column(start_offset) source!.character_column(start_offset)
end end
# The column number in code units of the given encoding where this location # The column number in code units of the given encoding where this location
@ -235,13 +235,13 @@ module Prism
# The column number in bytes where this location ends from the start of the # The column number in bytes where this location ends from the start of the
# line. # line.
def end_column def end_column
source.column(end_offset) source!.column(end_offset)
end end
# The column number in characters where this location ends from the start of # The column number in characters where this location ends from the start of
# the line. # the line.
def end_character_column def end_character_column
source.character_column(end_offset) source!.character_column(end_offset)
end end
# The column number in code units of the given encoding where this location # The column number in code units of the given encoding where this location
@ -281,8 +281,13 @@ module Prism
# the beginning of the file. Useful for when you want a location object but # the beginning of the file. Useful for when you want a location object but
# do not care where it points. # do not care where it points.
def self.null def self.null
source = nil #: Source new(nil, 0, 0)
new(source, 0, 0) end
private
def source!
source or raise "Missing source"
end end
end end

View File

@ -36,7 +36,6 @@ module Prism
# Create a new Location object # Create a new Location object
def Location(source = nil, start_offset = 0, length = 0) def Location(source = nil, start_offset = 0, length = 0)
# @type var source: Source
Location.new(source, start_offset, length) Location.new(source, start_offset, length)
end end
<%- nodes.each do |node| -%> <%- nodes.each do |node| -%>