Skip to content

Commit e974b10

Browse files
committed
f
1 parent 4138873 commit e974b10

File tree

6 files changed

+60
-10
lines changed

6 files changed

+60
-10
lines changed

lib/unparser/buffer.rb

+14
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def unindent
7777
self
7878
end
7979

80+
def ensure_nl
81+
nl unless fresh_line?
82+
end
83+
8084
# Write newline
8185
#
8286
# @return [self]
@@ -95,6 +99,16 @@ def final_newline
9599
write(NL) unless @lines.zero?
96100
end
97101

102+
def nl_flush_heredocs
103+
return if @heredocs.empty?
104+
105+
if fresh_line?
106+
flush_heredocs
107+
else
108+
nl
109+
end
110+
end
111+
98112
def flush_heredocs
99113
@heredocs.each(&method(:write))
100114
@heredocs = []

lib/unparser/generation.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def emit_body_inner(node)
149149
emit_body_member(head)
150150

151151
tail.each do |child|
152-
nl
152+
buffer.ensure_nl
153153

154154
nl if EXTRA_NL.include?(child.type)
155155

lib/unparser/writer.rb

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ def round_trips?(source:)
3535
buffer.write_encoding(explicit_encoding) if explicit_encoding
3636
buffer.write(source)
3737

38-
puts "Candidate source:\n#{buffer.content}"
39-
4038
node.eql?(parser.parse(Unparser.buffer(buffer.content)))
4139
rescue Parser::SyntaxError
4240
false

lib/unparser/writer/dynamic_string.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class DynamicString
1212
HEREDOC_THRESHOLD = 8
1313
HEREDOC_DELIMITER = 'HEREDOC'
1414
HEREDOC_HEADER = "<<-#{HEREDOC_DELIMITER}"
15+
HEREDOC_FOOTER = "#{HEREDOC_DELIMITER}\n"
1516

1617
private_constant(*constants(false))
1718

@@ -115,16 +116,11 @@ class Heredoc
115116

116117
def emit
117118
emit_heredoc_body
118-
emit_heredoc_footer
119-
nl
119+
write(HEREDOC_FOOTER)
120120
end
121121

122122
private
123123

124-
def emit_heredoc_footer
125-
write(HEREDOC_DELIMITER)
126-
end
127-
128124
def emit_heredoc_body
129125
buffer.root_indent do
130126
children.each do |child|

lib/unparser/writer/regexp.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def dispatch
4141
body.each(&method(:emit_body))
4242
write(token_close)
4343
emit_options
44+
buffer.nl_flush_heredocs
4445
end
4546
end
4647

@@ -87,7 +88,6 @@ def render_with_delimiter(token_close:, token_open:)
8788
)
8889

8990
writer.dispatch
90-
buffer.flush_heredocs
9191
buffer.content.freeze
9292
end
9393
end # Regexp

spec/unit/unparser/buffer_spec.rb

+42
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,46 @@
166166

167167
it_should_behave_like 'a command method'
168168
end
169+
170+
describe '#nl_flush_heredocs' do
171+
let(:object) { described_class.new }
172+
173+
subject { object.nl_flush_heredocs }
174+
175+
context 'on unbuffered heredoc' do
176+
context 'on fresh line' do
177+
it 'does nothing' do
178+
subject
179+
expect(object.content).to eql('')
180+
end
181+
end
182+
183+
context 'outside fresh line' do
184+
it 'does nothing' do
185+
object.write('foo')
186+
subject
187+
expect(object.content).to eql('foo')
188+
end
189+
end
190+
end
191+
192+
context 'on buffered heredocs' do
193+
context 'on fresh line' do
194+
it 'flushes heredoc' do
195+
object.push_heredoc('HEREDOC')
196+
subject
197+
expect(object.content).to eql('HEREDOC')
198+
end
199+
end
200+
201+
context 'outside fresh line' do
202+
it 'flushes heredoc, with new line' do
203+
object.write('foo')
204+
object.push_heredoc('HEREDOC')
205+
subject
206+
expect(object.content).to eql("foo\nHEREDOC")
207+
end
208+
end
209+
end
210+
end
169211
end

0 commit comments

Comments
 (0)