-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Formatter: try to format macros that don't interpolate content #12378
Conversation
# Only format the macro contents if it's valid Crystal code | ||
Parser.new(source).parse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here any \{{
or \{%
MacroLiteral
s from source
will be unescaped. Is there a situation where a macro's body is valid Crystal code if it has the escaped forms, but becomes invalid when they are unescaped? (This should only affect a very small number of macros though.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that this might be already handled. I tried formatting this:
macro foo
puts(\{{@type}} )
end
And it actually produced this:
macro foo
puts(\{{@type}})
end
There's a write_macro_slashes
method. I didn't take a look at how it works (I might have written that but now I can't remember.)
I'd say, let's first find a case where it fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On further thought, this is probably fine because it just means the macro contents are invalid after any interpolation. This is what I got:
{\{{ 1 }}} # a TupleLiteral wrapping an escaped MacroLiteral
# becomes:
{{{ 1 }}} # a MacroExpression wrapping a TupleLiteral
{ \{{ 1 }} } # a TupleLiteral wrapping an escaped MacroLiteral
# becomes:
{ {{ 1 }} } # a TupleLiteral wrapping a MacroExpression
{\{{ 1 }} } # a TupleLiteral wrapping an escaped MacroLiteral
# becomes:
{{{ 1 }} } # invalid code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I'm not understanding. Is there something to fix? Based on the comment above, it looks so but I'm not sure 😅
Even if the macro contents are invalid, I don't think we should format those to something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can leave this check as is.
Thanks for fix this issue, it works! |
Fixes #12164