Skip to content

Commit e18d456

Browse files
authored
Fix parsing erb with bare yield (#2444)
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
1 parent 262c34b commit e18d456

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/ruby_lsp/erb_document.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def parse
1212
@needs_parsing = false
1313
scanner = ERBScanner.new(@source)
1414
scanner.scan
15-
@parse_result = Prism.parse(scanner.ruby)
15+
# assigning empty scopes to turn Prism into eval mode
16+
@parse_result = Prism.parse(scanner.ruby, scopes: [[]])
1617
end
1718

1819
sig { override.returns(T::Boolean) }

test/erb_document_test.rb

+21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ def test_erb_file_is_properly_parsed
2323
)
2424
end
2525

26+
def test_erb_file_parses_in_eval_context
27+
document = RubyLsp::ERBDocument.new(source: +<<~ERB, version: 1, uri: URI("file:///foo.erb"))
28+
<html>
29+
<head>
30+
<%= yield :head %>
31+
</head>
32+
<body>
33+
<%= yield %>
34+
</body>
35+
</html>
36+
ERB
37+
38+
document.parse
39+
40+
refute_predicate(document, :syntax_error?)
41+
assert_equal(
42+
" \n \n yield :head \n \n \n yield \n \n \n",
43+
document.parse_result.source.source,
44+
)
45+
end
46+
2647
def test_erb_document_handles_windows_newlines
2748
document = RubyLsp::ERBDocument.new(source: "<%=\r\nbar %>", version: 1, uri: URI("file:///foo.erb"))
2849
document.parse

0 commit comments

Comments
 (0)