55require "tmpdir"
66require "stringio"
77require "pathname"
8- require "ripper"
98require "timeout"
109
10+ # We need Ripper loaded for `Prism.lex_compat` even if we're using Prism
11+ # for lexing and parsing
12+ require "ripper"
13+
14+ # Prism is the new parser, replacing Ripper
15+ #
16+ # We need to "dual boot" both for now because syntax_suggest
17+ # supports older rubies that do not ship with syntax suggest.
18+ #
19+ # We also need the ability to control loading of this library
20+ # so we can test that both modes work correctly in CI.
21+ if ( value = ENV [ "SYNTAX_SUGGEST_DISABLE_PRISM" ] )
22+ warn "Skipping loading prism due to SYNTAX_SUGGEST_DISABLE_PRISM=#{ value } "
23+ else
24+ begin
25+ require "prism"
26+ rescue LoadError
27+ end
28+ end
29+
1130module SyntaxSuggest
1231 # Used to indicate a default value that cannot
1332 # be confused with another input.
@@ -16,6 +35,14 @@ module SyntaxSuggest
1635 class Error < StandardError ; end
1736 TIMEOUT_DEFAULT = ENV . fetch ( "SYNTAX_SUGGEST_TIMEOUT" , 1 ) . to_i
1837
38+ # SyntaxSuggest.use_prism_parser? [Private]
39+ #
40+ # Tells us if the prism parser is available for use
41+ # or if we should fallback to `Ripper`
42+ def self . use_prism_parser?
43+ defined? ( Prism )
44+ end
45+
1946 # SyntaxSuggest.handle_error [Public]
2047 #
2148 # Takes a `SyntaxError` exception, uses the
@@ -129,11 +156,20 @@ def self.valid_without?(without_lines:, code_lines:)
129156 # SyntaxSuggest.invalid? [Private]
130157 #
131158 # Opposite of `SyntaxSuggest.valid?`
132- def self . invalid? ( source )
133- source = source . join if source . is_a? ( Array )
134- source = source . to_s
159+ if defined? ( Prism )
160+ def self . invalid? ( source )
161+ source = source . join if source . is_a? ( Array )
162+ source = source . to_s
135163
136- Ripper . new ( source ) . tap ( &:parse ) . error?
164+ Prism . parse ( source ) . failure?
165+ end
166+ else
167+ def self . invalid? ( source )
168+ source = source . join if source . is_a? ( Array )
169+ source = source . to_s
170+
171+ Ripper . new ( source ) . tap ( &:parse ) . error?
172+ end
137173 end
138174
139175 # SyntaxSuggest.valid? [Private]
@@ -191,7 +227,6 @@ def self.valid?(source)
191227require_relative "code_line"
192228require_relative "code_block"
193229require_relative "block_expand"
194- require_relative "ripper_errors"
195230require_relative "priority_queue"
196231require_relative "unvisited_lines"
197232require_relative "around_block_scan"
0 commit comments