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

Fix ASTNode#to_s for multiline macro expression #6666

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
1 change: 1 addition & 0 deletions spec/compiler/parser/to_s_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,5 @@ describe "ASTNode#to_s" do
expect_to_s "1.&*"
expect_to_s "1.&**"
expect_to_s "1.~(2)"
expect_to_s %({% for foo in bar %}\n {{ if true\n foo\n bar\nend }}\n{% end %})
end
14 changes: 14 additions & 0 deletions spec/compiler/semantic/macro_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1355,4 +1355,18 @@ describe "Semantic: macro" do
Foo(Int32).foo
)) { array_of(int32).metaclass }
end

it "expands multiline macro expression in verbatim (#6643)" do
assert_type(%(
{% verbatim do %}
{{
if true
1
"2"
3
end
}}
{% end %}
)) { int32 }
end
end
11 changes: 10 additions & 1 deletion src/compiler/crystal/syntax/to_s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,9 @@ module Crystal
def visit(node : MacroExpression)
@str << (node.output? ? "{{" : "{% ")
@str << ' ' if node.output?
node.exp.accept self
outside_macro do
node.exp.accept self
end
@str << ' ' if node.output?
@str << (node.output? ? "}}" : " %}")
false
Expand Down Expand Up @@ -1541,6 +1543,13 @@ module Crystal
@inside_macro -= 1
end

def outside_macro
old_inside_macro = @inside_macro
@inside_macro = 0
yield
@inside_macro = old_inside_macro
end

def to_s
@str.to_s
end
Expand Down