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 454586d3d61..689f293a890 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 @@ -4,6 +4,8 @@ package org.jetbrains.kotlin.js.backend; +import com.intellij.openapi.util.text.StringUtil; +import kotlin.text.StringsKt; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.js.backend.ast.*; import org.jetbrains.kotlin.js.backend.ast.JsDoubleLiteral; @@ -1140,8 +1142,16 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public void visitMultiLineComment(@NotNull JsMultiLineComment comment) { + List lines = StringsKt.lines(comment.getText()); + p.print("/*"); - p.print(comment.getText()); + p.print(lines.get(0).trim()); + + for (int i = 1; i < lines.size(); i++) { + newline(); + p.print(lines.get(i).trim()); + } + p.print("*/"); needSemi = false; newline(); diff --git a/js/js.parser/src/com/google/gwt/dev/js/rhino/Parser.java b/js/js.parser/src/com/google/gwt/dev/js/rhino/Parser.java index 2970d915d8d..836b816e570 100644 --- a/js/js.parser/src/com/google/gwt/dev/js/rhino/Parser.java +++ b/js/js.parser/src/com/google/gwt/dev/js/rhino/Parser.java @@ -702,7 +702,10 @@ public class Parser { break; } } - ts.matchToken(TokenStream.SEMI); + + if (pn.type != TokenStream.SINGLE_LINE_COMMENT && pn.type != TokenStream.MULTI_LINE_COMMENT) { + ts.matchToken(TokenStream.SEMI); + } return pn; } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/testOld/utils/DirectiveTestUtils.java b/js/js.tests/test/org/jetbrains/kotlin/js/testOld/utils/DirectiveTestUtils.java index 3eaa45237a8..0df4d78f6ff 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/testOld/utils/DirectiveTestUtils.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/testOld/utils/DirectiveTestUtils.java @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.js.testOld.utils; import com.intellij.openapi.util.text.StringUtil; +import kotlin.text.StringsKt; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.js.backend.ast.*; @@ -308,14 +309,14 @@ public class DirectiveTestUtils { return isMultiLine ? new RecursiveJsVisitor() { @Override public void visitMultiLineComment(@NotNull JsMultiLineComment comment) { - if (comment.getText().trim().equals(text)) { + if (isTheSameText(comment.getText(), text)) { setElementExists(true); } } } : new RecursiveJsVisitor() { @Override public void visitSingleLineComment(@NotNull JsSingleLineComment comment) { - if (comment.getText().trim().equals(text)) { + if (isTheSameText(comment.getText(), text)) { setElementExists(true); } } @@ -324,9 +325,22 @@ public class DirectiveTestUtils { @Override protected void loadArguments(@NotNull ArgumentsHelper arguments) { - this.text = arguments.findNamedArgument("text"); + this.text = arguments.findNamedArgument("text").replace("\\n", System.lineSeparator()); this.isMultiLine = Boolean.parseBoolean(arguments.findNamedArgument("multiline")); } + + private boolean isTheSameText(String str1, String str2) { + List lines1 = StringsKt.lines(str1); + List lines2 = StringsKt.lines(str2); + + if (lines1.size() != lines2.size()) return false; + + for (int i = 0; i < lines1.size(); i++) { + if (!lines1.get(i).trim().equals(lines2.get(i).trim())) return false; + } + + return true; + } }; private static final DirectiveHandler CHECK_COMMENT_DOESNT_EXIST = new NodeExistenceDirective("CHECK_COMMENT_DOESNT_EXIST", false) { @@ -531,10 +545,6 @@ public class DirectiveTestUtils { return node; } - public static void checkCommentExists(JsNode node, String content, boolean isMultiline) { - - } - public static void checkPropertyNotUsed(JsNode node, String propertyName, String scope, boolean isGetAllowed, boolean isSetAllowed) throws Exception { PropertyReferenceCollector counter = PropertyReferenceCollector.Companion.collect(findScope(node, scope)); diff --git a/js/js.translator/testData/box/jsCode/comments.kt b/js/js.translator/testData/box/jsCode/comments.kt index 834a17f138b..ee58bb15465 100644 --- a/js/js.translator/testData/box/jsCode/comments.kt +++ b/js/js.translator/testData/box/jsCode/comments.kt @@ -1,18 +1,28 @@ // EXPECTED_REACHABLE_NODES: 1282 // CHECK_COMMENT_EXISTS: text="Single line comment" multiline=false +// CHECK_COMMENT_EXISTS: text="Second single line comment" multiline=false +// CHECK_COMMENT_EXISTS: text="Third single line comment" multiline=false +// CHECK_COMMENT_EXISTS: text="Forth single line comment" multiline=false // CHECK_COMMENT_EXISTS: text="Multi line comment" multiline=true // CHECK_COMMENT_EXISTS: text="Single line comment inside function" multiline=false // CHECK_COMMENT_EXISTS: text="Multi line comment inside function" multiline=true // CHECK_COMMENT_EXISTS: text="After call single line comment" multiline=false // CHECK_COMMENT_EXISTS: text="After call multi line comment" multiline=true +// CHECK_COMMENT_EXISTS: text="The header multiline\ncomment" multiline=true // CHECK_COMMENT_DOESNT_EXIST: text="random position comment 1" multiline=true // CHECK_COMMENT_DOESNT_EXIST: text="random position comment 2" multiline=true // CHECK_COMMENT_DOESNT_EXIST: text="random position comment 3" multiline=true +// CHECK_COMMENT_EXISTS: text="1Multi line comment\n" multiline=true +// CHECK_COMMENT_EXISTS: text="2Multi line comment\n\n\n" multiline=true +// CHECK_COMMENT_EXISTS: text="3Multi line\n\n\n\n\ncomment\n" multiline=true +// CHECK_COMMENT_EXISTS: text="" multiline=true package foo fun box(): String { js(""" + /* The header multiline + comment */ function foo() { // Single line comment inside function Object; @@ -20,7 +30,10 @@ fun box(): String { } // Single line comment + // Second single line comment foo(); + // Third single line comment + // Forth single line comment /* Multi line comment */ foo(); @@ -30,6 +43,26 @@ fun box(): String { foo(); /* After call multi line comment */ var /*random position comment 1*/ c /*random position comment 2*/ = /*random position comment 3*/ "Random position"; + + /* 1Multi line comment + */ + foo(); + /* 2Multi line comment + + + */ + foo(); + /* 3Multi line + + + + + comment + */ + foo(); + + /**/ + foo(); """) return "OK" } \ No newline at end of file