diff --git a/lib/ruumba/parser.rb b/lib/ruumba/parser.rb index b22c8fd..0cc9a52 100644 --- a/lib/ruumba/parser.rb +++ b/lib/ruumba/parser.rb @@ -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 diff --git a/spec/ruumba/parser_spec.rb b/spec/ruumba/parser_spec.rb index 2416f44..0c0de23 100644 --- a/spec/ruumba/parser_spec.rb +++ b/spec/ruumba/parser_spec.rb @@ -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 @@ -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 %> + +
+
+ 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 @@ -40,7 +53,7 @@ <<~RUBY puts 'foo' # that puts is ruby code - bar + bar RUBY expect(parser.extract(erb)).to eq(parsed) @@ -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) @@ -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 = "

Dead or alive, you're coming with me.

" - 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