Skip to content

Commit

Permalink
Formatter: escape backslashes in macro literals when subformatting (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 11, 2022
1 parent b01d9eb commit 5cf5e5a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2225,4 +2225,15 @@ describe Crystal::Formatter do
break
end
CODE

# #12378
assert_format <<-CODE
macro foo
macro bar
\\{% begin %}
\\\\{% puts %}
\\{% end %}
end
end
CODE
end
11 changes: 10 additions & 1 deletion src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,16 @@ module Crystal
@no_rstrip_lines.add line
end

write @token.raw
raw = @token.raw

# If the macro literal has a backlash, but we are subformatting
# a macro, we need to escape it (if it got here unescaped it was escaped to
# begin with.)
if @subformat_nesting > 0
raw = raw.gsub("\\", "\\" * (@subformat_nesting + 1))
end

write raw
next_macro_token
false
end
Expand Down

0 comments on commit 5cf5e5a

Please sign in to comment.