From 7c0cf71049e82f165ab847e50a358d3659c547bb Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Thu, 2 May 2024 11:12:43 -0400 Subject: [PATCH] [ruby/prism] Node#script_lines and supporting infra https://github.com/ruby/prism/commit/cb4a8ab772 --- lib/prism/parse_result.rb | 10 ++++++++++ prism/templates/lib/prism/node.rb.erb | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb index e01aa070c2..ff8b1dc8bf 100644 --- a/lib/prism/parse_result.rb +++ b/lib/prism/parse_result.rb @@ -27,6 +27,11 @@ module Prism 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) @@ -177,6 +182,11 @@ module Prism "#" 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) diff --git a/prism/templates/lib/prism/node.rb.erb b/prism/templates/lib/prism/node.rb.erb index ddbe53e594..6ffbf7b146 100644 --- a/prism/templates/lib/prism/node.rb.erb +++ b/prism/templates/lib/prism/node.rb.erb @@ -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