Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't remove docs in block yielding of macro #3778

Merged
Merged
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
9 changes: 7 additions & 2 deletions spec/compiler/parser/to_s_spec.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
require "../../spec_helper"

private def expect_to_s(original, expected = original, file = __FILE__, line = __LINE__)
private def expect_to_s(original, expected = original, emit_doc = false, file = __FILE__, line = __LINE__)
it "does to_s of #{original.inspect}", file, line do
Parser.parse(original).to_s.should eq(expected), file, line
str = IO::Memory.new expected.bytesize
parser = Parser.new original
parser.wants_doc = emit_doc
parser.parse.to_s(str, emit_doc: emit_doc)
str.to_s.should eq(expected), file, line
end
end

Expand Down Expand Up @@ -88,4 +92,5 @@ describe "ASTNode#to_s" do
expect_to_s "macro foo\n{% @type %}\nend"
expect_to_s "macro foo\n\\{%@type %}\nend"
expect_to_s "enum A : B\nend"
expect_to_s "# doc\ndef foo\nend", emit_doc: true
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module Crystal
# are shown in the block instead of in the generated macro source
is_yield = node.exp.is_a?(Yield) && !@last.is_a?(Nop)
@str << " #<loc:push>begin " if is_yield
@last.to_s(@str, emit_loc_pragma: is_yield)
@last.to_s(@str, emit_loc_pragma: is_yield, emit_doc: is_yield)
@str << " end#<loc:pop> " if is_yield
end

Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module Crystal
clone = clone_without_location
clone.location = location
clone.end_location = end_location
clone.doc = doc
clone
end

Expand Down
37 changes: 20 additions & 17 deletions src/compiler/crystal/syntax/to_s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,40 @@ module Crystal
to_s(io)
end

def to_s(io, emit_loc_pragma = false)
visitor = ToSVisitor.new(io, emit_loc_pragma: emit_loc_pragma)
def to_s(io, emit_loc_pragma = false, emit_doc = false)
visitor = ToSVisitor.new(io, emit_loc_pragma: emit_loc_pragma, emit_doc: emit_doc)
self.accept visitor
end
end

class ToSVisitor < Visitor
@str : IO

def initialize(@str = IO::Memory.new, @emit_loc_pragma = false)
def initialize(@str = IO::Memory.new, @emit_loc_pragma = false, @emit_doc = false)
@indent = 0
@inside_macro = 0
@inside_lib = false
end

def visit_any(node)
return true unless @emit_loc_pragma

location = node.location
return true unless location

filename = location.filename
return true unless filename.is_a?(String)
if @emit_doc && (doc = node.doc) && !doc.empty?
doc.each_line(chomp: false) do |line|
append_indent
@str << "# "
@str << line
end
@str.puts
end

@str << "#<loc:"
filename.inspect(@str)
@str << ","
@str << location.line_number
@str << ","
@str << location.column_number
@str << ">"
if @emit_loc_pragma && (loc = node.location) && loc.filename.is_a?(String)
@str << "#<loc:"
loc.filename.inspect(@str)
@str << ","
@str << loc.line_number
@str << ","
@str << loc.column_number
@str << ">"
end

true
end
Expand Down