summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSchneems <richard.schneeman+foo@gmail.com>2026-01-20 15:32:33 -0600
committernagachika <nagachika@ruby-lang.org>2026-05-09 15:16:03 +0900
commitb7d3e381f60e0bc43079f8e7c3db3445e70a8920 (patch)
treee4f6d3102ee854168dcbbae6bfd8baaee2cb9445
parentec0a6af126e8eae2868f94dabbeb4929e3f55dfd (diff)
[ruby/syntax_suggest] Refactor multi-prism version logic
The reason this logic for different methods branches in the class instead of internally was to be eagerly aggressive about runtime performance. This code is currently only used once for the document where it's invoked ~N times (where N is number of lines): ```ruby module SyntaxSuggest class CleanDocument # ... def join_trailing_slash! trailing_groups = @document.select(&:trailing_slash?).map do |code_line| take_while_including(code_line.index..) { |x| x.trailing_slash? } end join_groups(trailing_groups) self end ``` Since this is not currently a hot-spot I think merging the branches and using a case statement is a reasonable tradeoff and avoids the need to do specific version testing. An alternative idea was presented in #241 of behavior-based testing for branch logic (which I would prefer), however, calling the code triggered requiring a `DelegateClass` when the `syntax_suggest/api` is being required. https://github.com/ruby/syntax_suggest/commit/ab122c455f
-rw-r--r--lib/syntax_suggest/code_line.rb22
1 files changed, 9 insertions, 13 deletions
diff --git a/lib/syntax_suggest/code_line.rb b/lib/syntax_suggest/code_line.rb
index 892c273c41..76ca892ac3 100644
--- a/lib/syntax_suggest/code_line.rb
+++ b/lib/syntax_suggest/code_line.rb
@@ -180,21 +180,17 @@ module SyntaxSuggest
# EOM
# expect(lines.first.trailing_slash?).to eq(true)
#
- if SyntaxSuggest.use_prism_parser? && Prism::VERSION <= "1.8.0"
- # Older versions of prism didn't correctly emit on_sp
- def trailing_slash?
- last = @lex.last
- return false unless last
-
- last.type == :on_tstring_end || (last.type == :on_sp && last.token == TRAILING_SLASH)
- end
- else
- def trailing_slash?
- last = @lex.last
- return false unless last
- return false unless last.type == :on_sp
+ def trailing_slash?
+ last = @lex.last
+ # Older versions of prism diverged slightly from Ripper in compatibility mode
+ case last&.type
+ when :on_sp
last.token == TRAILING_SLASH
+ when :on_tstring_end
+ true
+ else
+ false
end
end