Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD (unreleased)

- Fix: Correctly identify trailing slashes when using Prism > 1.8.0. (https://github.com/ruby/syntax_suggest/pull/241)

## 2.0.2

- Fix: Separate multiple parser errors by newline. (https://github.com/ruby/syntax_suggest/pull/232)
Expand Down
7 changes: 5 additions & 2 deletions lib/syntax_suggest/code_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,13 @@ def ignore_newline_not_beg?
# EOM
# expect(lines.first.trailing_slash?).to eq(true)
#
if SyntaxSuggest.use_prism_parser?
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
last&.type == :on_tstring_end
return false unless last

last.type == :on_tstring_end || (last.type == :on_sp && last.token == TRAILING_SLASH)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The (last.type == :on_sp && last.token == TRAILING_SLASH) part is to handle the fact there is no Prism 1.8.1/1.9.0 yet and so no easy way to detect Prism has :on_sp vs not, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's right. I tried feature detection (Prism.lex_compat(" ").value.dig(0, 1) == :on_sp) but then some other test here fails because prism loads delegate and that adds a method to Kernel which it didn't like

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's right. I tried feature detection (Prism.lex_compat(" ").value.dig(0, 1) == :on_sp) but then some other test here fails because prism loads delegate and that adds a method to Kernel which it didn't like

Explaining that test a bit: I have regression tests that make sure I'm not accidentally messing with people's requires so they don't come to rely on me loading something by accident. Otherwise, if I require pathname someone might come to rely on that always being available and have their code break if I were to change usage.

The two ways to use syntax suggest is either by requiring syntax_suggest which sets up core extensions that only trigger those loads on syntax error. Or you can also require syntax_suggest/api which was added for support for sorbet (not sure if they ended up using it or not) so they can manually invoke syntax suggest on parsing.

The test that fails is when requiring syntax_suggest/api, which is less worrisome than the more common syntax_suggest (loaded by default). But it's still better not to do it at all.

end
else
def trailing_slash?
Expand Down