fix(KT-52339): add newlines to SourceMap to calculate multiline comments in a right way.
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.js.backend;
|
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.annotations.Nullable;
|
||||||
import org.jetbrains.kotlin.js.backend.ast.*;
|
import org.jetbrains.kotlin.js.backend.ast.*;
|
||||||
import org.jetbrains.kotlin.js.backend.ast.JsDoubleLiteral;
|
import org.jetbrains.kotlin.js.backend.ast.JsDoubleLiteral;
|
||||||
@@ -1140,8 +1142,16 @@ public class JsToStringGenerationVisitor extends JsVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitMultiLineComment(@NotNull JsMultiLineComment comment) {
|
public void visitMultiLineComment(@NotNull JsMultiLineComment comment) {
|
||||||
|
List<String> lines = StringsKt.lines(comment.getText());
|
||||||
|
|
||||||
p.print("/*");
|
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("*/");
|
p.print("*/");
|
||||||
needSemi = false;
|
needSemi = false;
|
||||||
newline();
|
newline();
|
||||||
|
|||||||
@@ -702,7 +702,10 @@ public class Parser {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ts.matchToken(TokenStream.SEMI);
|
|
||||||
|
if (pn.type != TokenStream.SINGLE_LINE_COMMENT && pn.type != TokenStream.MULTI_LINE_COMMENT) {
|
||||||
|
ts.matchToken(TokenStream.SEMI);
|
||||||
|
}
|
||||||
|
|
||||||
return pn;
|
return pn;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.js.testOld.utils;
|
package org.jetbrains.kotlin.js.testOld.utils;
|
||||||
|
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
import com.intellij.openapi.util.text.StringUtil;
|
||||||
|
import kotlin.text.StringsKt;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.kotlin.js.backend.ast.*;
|
import org.jetbrains.kotlin.js.backend.ast.*;
|
||||||
@@ -308,14 +309,14 @@ public class DirectiveTestUtils {
|
|||||||
return isMultiLine ? new RecursiveJsVisitor() {
|
return isMultiLine ? new RecursiveJsVisitor() {
|
||||||
@Override
|
@Override
|
||||||
public void visitMultiLineComment(@NotNull JsMultiLineComment comment) {
|
public void visitMultiLineComment(@NotNull JsMultiLineComment comment) {
|
||||||
if (comment.getText().trim().equals(text)) {
|
if (isTheSameText(comment.getText(), text)) {
|
||||||
setElementExists(true);
|
setElementExists(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} : new RecursiveJsVisitor() {
|
} : new RecursiveJsVisitor() {
|
||||||
@Override
|
@Override
|
||||||
public void visitSingleLineComment(@NotNull JsSingleLineComment comment) {
|
public void visitSingleLineComment(@NotNull JsSingleLineComment comment) {
|
||||||
if (comment.getText().trim().equals(text)) {
|
if (isTheSameText(comment.getText(), text)) {
|
||||||
setElementExists(true);
|
setElementExists(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -324,9 +325,22 @@ public class DirectiveTestUtils {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadArguments(@NotNull ArgumentsHelper arguments) {
|
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"));
|
this.isMultiLine = Boolean.parseBoolean(arguments.findNamedArgument("multiline"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isTheSameText(String str1, String str2) {
|
||||||
|
List<String> lines1 = StringsKt.lines(str1);
|
||||||
|
List<String> 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) {
|
private static final DirectiveHandler CHECK_COMMENT_DOESNT_EXIST = new NodeExistenceDirective("CHECK_COMMENT_DOESNT_EXIST", false) {
|
||||||
@@ -531,10 +545,6 @@ public class DirectiveTestUtils {
|
|||||||
return node;
|
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)
|
public static void checkPropertyNotUsed(JsNode node, String propertyName, String scope, boolean isGetAllowed, boolean isSetAllowed)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
PropertyReferenceCollector counter = PropertyReferenceCollector.Companion.collect(findScope(node, scope));
|
PropertyReferenceCollector counter = PropertyReferenceCollector.Companion.collect(findScope(node, scope));
|
||||||
|
|||||||
@@ -1,18 +1,28 @@
|
|||||||
// EXPECTED_REACHABLE_NODES: 1282
|
// EXPECTED_REACHABLE_NODES: 1282
|
||||||
// CHECK_COMMENT_EXISTS: text="Single line comment" multiline=false
|
// 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="Multi line comment" multiline=true
|
||||||
// CHECK_COMMENT_EXISTS: text="Single line comment inside function" multiline=false
|
// 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="Multi line comment inside function" multiline=true
|
||||||
// CHECK_COMMENT_EXISTS: text="After call single line comment" multiline=false
|
// 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="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 1" multiline=true
|
||||||
// CHECK_COMMENT_DOESNT_EXIST: text="random position comment 2" 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_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
|
package foo
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
js("""
|
js("""
|
||||||
|
/* The header multiline
|
||||||
|
comment */
|
||||||
function foo() {
|
function foo() {
|
||||||
// Single line comment inside function
|
// Single line comment inside function
|
||||||
Object;
|
Object;
|
||||||
@@ -20,7 +30,10 @@ fun box(): String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Single line comment
|
// Single line comment
|
||||||
|
// Second single line comment
|
||||||
foo();
|
foo();
|
||||||
|
// Third single line comment
|
||||||
|
// Forth single line comment
|
||||||
|
|
||||||
/* Multi line comment */
|
/* Multi line comment */
|
||||||
foo();
|
foo();
|
||||||
@@ -30,6 +43,26 @@ fun box(): String {
|
|||||||
foo(); /* After call multi line comment */
|
foo(); /* After call multi line comment */
|
||||||
|
|
||||||
var /*random position comment 1*/ c /*random position comment 2*/ = /*random position comment 3*/ "Random position";
|
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"
|
return "OK"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user