Skip to content

Commit

Permalink
Fix parsing erb with bare yield (#2444)
Browse files Browse the repository at this point in the history
Parse erb documents in an eval context

Some things that are valid in erb would not be valid in a standalone ruby file.
This particularly impacts `yield`.
Instead treat the provided code as being evaled

Closes #2442
  • Loading branch information
Earlopain authored Aug 14, 2024
1 parent 262c34b commit e18d456
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/ruby_lsp/erb_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def parse
@needs_parsing = false
scanner = ERBScanner.new(@source)
scanner.scan
@parse_result = Prism.parse(scanner.ruby)
# assigning empty scopes to turn Prism into eval mode
@parse_result = Prism.parse(scanner.ruby, scopes: [[]])
end

sig { override.returns(T::Boolean) }
Expand Down
21 changes: 21 additions & 0 deletions test/erb_document_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ def test_erb_file_is_properly_parsed
)
end

def test_erb_file_parses_in_eval_context
document = RubyLsp::ERBDocument.new(source: +<<~ERB, version: 1, uri: URI("file:///foo.erb"))
<html>
<head>
<%= yield :head %>
</head>
<body>
<%= yield %>
</body>
</html>
ERB

document.parse

refute_predicate(document, :syntax_error?)
assert_equal(
" \n \n yield :head \n \n \n yield \n \n \n",
document.parse_result.source.source,
)
end

def test_erb_document_handles_windows_newlines
document = RubyLsp::ERBDocument.new(source: "<%=\r\nbar %>", version: 1, uri: URI("file:///foo.erb"))
document.parse
Expand Down

0 comments on commit e18d456

Please sign in to comment.