summaryrefslogtreecommitdiff
path: root/lib/prism.rb
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2023-12-01 12:22:01 -0500
committergit <svn-admin@ruby-lang.org>2023-12-01 20:53:34 +0000
commit492c82cb417a92d1941f10b52e77ec0c4b2cc8a6 (patch)
tree3358d814456f46c6adb80921e968d1fb5258ce04 /lib/prism.rb
parentb77551adee831302f22dc7d9fdccd597923511c4 (diff)
[ruby/prism] Prism.parse_success?(source)
A lot of tools use Ripper/RubyVM::AbstractSyntaxTree to determine if a source is valid. These tools both create an AST instead of providing an API that will return a boolean only. This new API only creates the C structs, but doesn't bother reifying them into Ruby/the serialization API. Instead it only returns true/false, which is significantly more efficient. https://github.com/ruby/prism/commit/7014740118
Diffstat (limited to 'lib/prism.rb')
-rw-r--r--lib/prism.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/prism.rb b/lib/prism.rb
index 909b71d66d..b9e615df6c 100644
--- a/lib/prism.rb
+++ b/lib/prism.rb
@@ -64,6 +64,22 @@ module Prism
def self.load(source, serialized)
Serialize.load(source, serialized)
end
+
+ # :call-seq:
+ # Prism::parse_failure?(source, **options) -> bool
+ #
+ # Returns true if the source is invalid Ruby code.
+ def self.parse_failure?(source, **options)
+ !parse_success?(source, **options)
+ end
+
+ # :call-seq:
+ # Prism::parse_file_failure?(filepath, **options) -> bool
+ #
+ # Returns true if the file at filepath is invalid Ruby code.
+ def self.parse_file_failure?(filepath, **options)
+ !parse_file_success?(filepath, **options)
+ end
end
require_relative "prism/node"