Skip to content
Open
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
8 changes: 8 additions & 0 deletions lib/ruumba/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def extract(contents)
# left so they match the original again
extracted_ruby.gsub!(/ raw/, 'raw')

unless region_start_marker
add_newline = extracted_ruby =~ /\n\z/

extracted_ruby.gsub!(/\s+$/, '')

extracted_ruby << "\n" if add_newline
end

extracted_ruby
end

Expand Down
31 changes: 22 additions & 9 deletions spec/ruumba/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
it 'extracts one line of Ruby code from an ERB template' do
erb = "<%= puts 'Hello, world!' %>"

expect(parser.extract(erb)).to eq(" puts 'Hello, world!' ")
expect(parser.extract(erb)).to eq(" puts 'Hello, world!'")
end

it 'extracts many lines of Ruby code from an ERB template' do
Expand All @@ -19,13 +19,26 @@
<% baz %>
RHTML

expect(parser.extract(erb)).to eq(" puts 'foo' \n puts 'bar' \n baz \n")
expect(parser.extract(erb)).to eq(" puts 'foo'\n puts 'bar'\n baz\n")
end

it 'extracts removes multiple trailing newlines' do
erb = <<~RHTML
<%= puts 'foo' %>
<%= puts 'bar' %>
<% baz %>

<br />
<br />
RHTML

expect(parser.extract(erb)).to eq(" puts 'foo'\n puts 'bar'\n baz\n")
end

it 'extracts multiple interpolations per line' do
erb = "<%= puts 'foo' %> then <% bar %>"

expect(parser.extract(erb)).to eq(" puts 'foo' ; bar ")
expect(parser.extract(erb)).to eq(" puts 'foo' ; bar")
end

it 'does extract single line ruby comments from an ERB template' do
Expand All @@ -40,7 +53,7 @@
<<~RUBY
puts 'foo'
# that puts is ruby code
bar
bar
RUBY

expect(parser.extract(erb)).to eq(parsed)
Expand All @@ -59,8 +72,8 @@
<<~RUBY
# this is a multiline comment
# interpolated in the ERB template
# it should be inside a comment
puts 'foo'
# it should be inside a comment
puts 'foo'
RUBY

expect(parser.extract(erb)).to eq(parsed)
Expand All @@ -72,20 +85,20 @@
RHTML

expect(parser.extract(erb))
.to eq(" raw 'style=\"display: none;\"' if num.even? \n")
.to eq(" raw 'style=\"display: none;\"' if num.even?\n")
end

it 'does not extract code from lines without ERB interpolation' do
erb = "<h1>Dead or alive, you're coming with me.</h1>"

expect(parser.extract(erb)).to eq(' ' * 46)
expect(parser.extract(erb)).to eq('')
end

it 'extracts comments on the same line' do
erb = '<% if (foo = bar) %><%# should always be truthy %>'

expect(parser.extract(erb))
.to eq(' if (foo = bar) ; # should always be truthy ')
.to eq(' if (foo = bar) ; # should always be truthy')
end

context 'when configured with a region marker' do
Expand Down