-
Notifications
You must be signed in to change notification settings - Fork 288
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
Function declaration with direct return and multiple statements throws IllegalStateException
#1979
Comments
Yep, I can see what the issue is. The format parts of the function body originally look like this:
The
This of course is now broken cause brackets are mismatched. Not sure what the correct solution here is (perhaps we check for the presence of multiple statements in the body and print the function normally if there are multiple statements?), but the obvious workaround is to merge statements into a single statement, either in code, or via Thanks for the report! |
Thanks for the suggestion! I can confirm that the workaround with @Test fun returnExpressionMultipleStatements() {
val spec = FunSpec.builder("three")
.returns(INT)
.addCode(listOf(
CodeBlock.of("return 1"),
CodeBlock.of(".plus(2)"),
).joinToCode("\n"))
.build()
assertThat(spec.toString()).isEqualTo(
"""
|public fun three(): kotlin.Int {
| return 1
| .plus(2)
|}
|""".trimMargin(),
)
} |
Note that |
What would be the correct way to write this? The example is oversimplified (to get a simple example that breaks), the real case is something on the lines of: fun foo(): Thing = Thing.builder()
.setX(123)
.setY(456)
.setZ("Hello")
.setW("World")
.build() I was basically trying to get this correctly indented and one function call per line (as they might be quite a few calls) |
Regular line breaks within a statement will indent subsequent lines. So |
Describe the bug
I tried generating a function where there are multiple
addStatement
calls, the first of which is a return, but somehow the first opening statement symbol is lost when callingtoString()
on the function spec, and the generation fails with the following exception:Note how the format parts are missing the initial
«
.To Reproduce
Note that this can be worked around changing
addStatement
toaddCode
for the first statement (the one containing thereturn
), however this makes the indentation a bit confusing in the final result, also not having a return in the first statement would make this compile (on aUnit
function, for example)Expected behavior
The function is generated without throwing.
The text was updated successfully, but these errors were encountered: