Skip to content

Commit

Permalink
[ruby/prism] Node#script_lines and supporting infra
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed May 2, 2024
1 parent 398453c commit 7c0cf71
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/prism/parse_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def encoding
source.encoding
end

# Returns the lines of the source code as an array of strings.
def lines
source.lines
end

# Perform a byteslice on the source code using the given byte offset and
# byte length.
def slice(byte_offset, length)
Expand Down Expand Up @@ -177,6 +182,11 @@ def inspect
"#<Prism::Location @start_offset=#{@start_offset} @length=#{@length} start_line=#{start_line}>"
end

# Returns all of the lines of the source code associated with this location.
def source_lines
source.lines
end

# The source code that this location represents.
def slice
source.slice(start_offset, length)
Expand Down
9 changes: 9 additions & 0 deletions prism/templates/lib/prism/node.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ module Prism
end
end

# Returns all of the lines of the source code associated with this node.
def source_lines
location.source_lines
end

# An alias for source_lines, used to mimic the API from
# RubyVM::AbstractSyntaxTree to make it easier to migrate.
alias script_lines source_lines

# Slice the location of the node from the source.
def slice
location.slice
Expand Down

0 comments on commit 7c0cf71

Please sign in to comment.