Skip to content

Commit 225ff0d

Browse files
makenowjustAry Borenszweig
authored andcommitted
Don't remove docs in block yielding of macro (#3778)
1 parent 7e9be8a commit 225ff0d

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

spec/compiler/parser/to_s_spec.cr

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
require "../../spec_helper"
22

3-
private def expect_to_s(original, expected = original, file = __FILE__, line = __LINE__)
3+
private def expect_to_s(original, expected = original, emit_doc = false, file = __FILE__, line = __LINE__)
44
it "does to_s of #{original.inspect}", file, line do
5-
Parser.parse(original).to_s.should eq(expected), file, line
5+
str = IO::Memory.new expected.bytesize
6+
parser = Parser.new original
7+
parser.wants_doc = emit_doc
8+
parser.parse.to_s(str, emit_doc: emit_doc)
9+
str.to_s.should eq(expected), file, line
610
end
711
end
812

@@ -88,4 +92,5 @@ describe "ASTNode#to_s" do
8892
expect_to_s "macro foo\n{% @type %}\nend"
8993
expect_to_s "macro foo\n\\{%@type %}\nend"
9094
expect_to_s "enum A : B\nend"
95+
expect_to_s "# doc\ndef foo\nend", emit_doc: true
9196
end

src/compiler/crystal/macros/interpreter.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module Crystal
103103
# are shown in the block instead of in the generated macro source
104104
is_yield = node.exp.is_a?(Yield) && !@last.is_a?(Nop)
105105
@str << " #<loc:push>begin " if is_yield
106-
@last.to_s(@str, emit_loc_pragma: is_yield)
106+
@last.to_s(@str, emit_loc_pragma: is_yield, emit_doc: is_yield)
107107
@str << " end#<loc:pop> " if is_yield
108108
end
109109

src/compiler/crystal/syntax/ast.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module Crystal
4040
clone = clone_without_location
4141
clone.location = location
4242
clone.end_location = end_location
43+
clone.doc = doc
4344
clone
4445
end
4546

src/compiler/crystal/syntax/to_s.cr

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,40 @@ module Crystal
77
to_s(io)
88
end
99

10-
def to_s(io, emit_loc_pragma = false)
11-
visitor = ToSVisitor.new(io, emit_loc_pragma: emit_loc_pragma)
10+
def to_s(io, emit_loc_pragma = false, emit_doc = false)
11+
visitor = ToSVisitor.new(io, emit_loc_pragma: emit_loc_pragma, emit_doc: emit_doc)
1212
self.accept visitor
1313
end
1414
end
1515

1616
class ToSVisitor < Visitor
1717
@str : IO
1818

19-
def initialize(@str = IO::Memory.new, @emit_loc_pragma = false)
19+
def initialize(@str = IO::Memory.new, @emit_loc_pragma = false, @emit_doc = false)
2020
@indent = 0
2121
@inside_macro = 0
2222
@inside_lib = false
2323
end
2424

2525
def visit_any(node)
26-
return true unless @emit_loc_pragma
27-
28-
location = node.location
29-
return true unless location
30-
31-
filename = location.filename
32-
return true unless filename.is_a?(String)
26+
if @emit_doc && (doc = node.doc) && !doc.empty?
27+
doc.each_line(chomp: false) do |line|
28+
append_indent
29+
@str << "# "
30+
@str << line
31+
end
32+
@str.puts
33+
end
3334

34-
@str << "#<loc:"
35-
filename.inspect(@str)
36-
@str << ","
37-
@str << location.line_number
38-
@str << ","
39-
@str << location.column_number
40-
@str << ">"
35+
if @emit_loc_pragma && (loc = node.location) && loc.filename.is_a?(String)
36+
@str << "#<loc:"
37+
loc.filename.inspect(@str)
38+
@str << ","
39+
@str << loc.line_number
40+
@str << ","
41+
@str << loc.column_number
42+
@str << ">"
43+
end
4144

4245
true
4346
end

0 commit comments

Comments
 (0)