-
-
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
Correct parsing string interpolation with trailing newline #4726
Correct parsing string interpolation with trailing newline #4726
Conversation
This fixes parsing puts "foo #{"bar"
}" to produce If we can come to a quick consensus on how this should be formatted we should include that change in this PR, if it gets to be a lengthy discussion we can fix it later. To me the above example should format to puts "foo #{"bar"}" but i'm not sure what this (and similar) should format to: puts "foo #{
"bar"
}" |
Also "Correctly parse ..." is correct English, instead of "Correct to parse ..." |
I am not sure if the formatter should remove newlines even in the first case. |
@straight-shoota Even if the first case's newline is intended, we can use backslash + newline escape in this case (and I think we should use it.) puts "foo #{"bar"}\
" |
@RX14 I can't understand the difference between "to do" and "doing"... |
I considered some ideas to format string interpolation with newline: Original source code: "foo #{
bar = 1
} baz" Idea 1: keep "foo #{
bar = 1
} baz" Idea 2: align with delimiter "foo #{
bar = 1
} baz" Idea 3: remove newline and indent "foo #{bar = 1} baz" I don't know which is the best and I can't decide it. I guess, the preference of idea 1 is unstable (and I don't like it). Idea 2 is the best if it follows no string, so: "foo #{
bar = 1
}" this indentation looks like |
I think the rule should be that if there's a newline after puts "foo #{"bar"}" but puts "foo #{
"bar"
}" formats as-is. |
Ok. I implemented formatting string interpolation in this way. |
@@ -340,6 +340,11 @@ describe Crystal::Formatter do | |||
assert_format "__DIR__", "__DIR__" | |||
assert_format "__LINE__", "__LINE__" | |||
|
|||
assert_format "\"\#{foo = 1\n}\"", "\"\#{foo = 1}\"" |
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.
You could use %(...)
to make the assertion more readable ?
e.g:
assert_format %("\#{foo = 1\n}"), %("\#{foo = 1}")
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.
fixed
5b66dd4
to
c8e0bbe
Compare
Thank you @makenowjust! |
Fixed #4713