From d46db2a9f372872b7c4e28bb2739ba7175874417 Mon Sep 17 00:00:00 2001 From: Artem Kobzar Date: Thu, 7 Apr 2022 21:53:52 +0200 Subject: [PATCH] fix: block statements rendering. --- .../backend/JsToStringGenerationVisitor.java | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/js/js.ast/src/org/jetbrains/kotlin/js/backend/JsToStringGenerationVisitor.java b/js/js.ast/src/org/jetbrains/kotlin/js/backend/JsToStringGenerationVisitor.java index 7385ad92790..ac46b64d598 100644 --- a/js/js.ast/src/org/jetbrains/kotlin/js/backend/JsToStringGenerationVisitor.java +++ b/js/js.ast/src/org/jetbrains/kotlin/js/backend/JsToStringGenerationVisitor.java @@ -458,11 +458,13 @@ public class JsToStringGenerationVisitor extends JsVisitor { popSourceInfo(); - nestedPush(x.getBody()); + JsStatement body = materialize(x.getBody()); + + nestedPush(body); sourceLocationConsumer.pushSourceInfo(null); - accept(x.getBody()); + accept(body); sourceLocationConsumer.popSourceInfo(); - nestedPop(x.getBody()); + nestedPop(body); } @Override @@ -470,10 +472,13 @@ public class JsToStringGenerationVisitor extends JsVisitor { sourceLocationConsumer.pushSourceInfo(null); p.print(CHARS_DO); - nestedPush(x.getBody()); - accept(x.getBody()); + + JsStatement body = materialize(x.getBody()); + + nestedPush(body); + accept(body); sourceLocationConsumer.popSourceInfo(); - nestedPop(x.getBody()); + nestedPop(body); pushSourceInfo(x.getCondition().getSource()); if (needSemi) { @@ -557,13 +562,15 @@ public class JsToStringGenerationVisitor extends JsVisitor { popSourceInfo(); - nestedPush(x.getBody()); - if (x.getBody() != null) { + JsStatement body = materialize(x.getBody()); + + nestedPush(body); + if (body != null) { sourceLocationConsumer.pushSourceInfo(null); - accept(x.getBody()); + accept(body); sourceLocationConsumer.popSourceInfo(); } - nestedPop(x.getBody()); + nestedPop(body); } @Override @@ -601,11 +608,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { popSourceInfo(); - nestedPush(x.getBody()); + JsStatement body = materialize(x.getBody()); + nestedPush(body); sourceLocationConsumer.pushSourceInfo(null); - accept(x.getBody()); + accept(body); sourceLocationConsumer.popSourceInfo(); - nestedPop(x.getBody()); + nestedPop(body); } @Override @@ -715,7 +723,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } sourceLocationConsumer.pushSourceInfo(null); - accept(thenStmt); + accept(materialize(thenStmt)); sourceLocationConsumer.popSourceInfo(); nestedPop(thenStmt); @@ -757,6 +765,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { return false; } + private static JsStatement materialize(@NotNull JsStatement statement) { + return statement instanceof JsCompositeBlock && ((JsCompositeBlock) statement).getStatements().size() > 1 + ? new JsBlock(statement) + : statement; + } + @Override public void visitInvocation(@NotNull JsInvocation invocation) { pushSourceInfo(invocation.getSource());