Skip to content

Commit

Permalink
Catch exceptions and create a J.Unknown in visitKtFile. (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima authored Nov 11, 2023
1 parent bbea9f7 commit 940cdd4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.jetbrains.kotlin.types.Variance;
import org.openrewrite.ExecutionContext;
import org.openrewrite.FileAttributes;
import org.openrewrite.ParseExceptionResult;
import org.openrewrite.Tree;
import org.openrewrite.internal.EncodingDetectingInputStream;
import org.openrewrite.internal.ListUtils;
Expand All @@ -48,6 +49,7 @@
import org.openrewrite.java.marker.OmitParentheses;
import org.openrewrite.java.marker.TrailingComma;
import org.openrewrite.java.tree.*;
import org.openrewrite.kotlin.KotlinParser;
import org.openrewrite.kotlin.marker.*;
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.marker.Markers;
Expand All @@ -62,6 +64,7 @@
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static org.openrewrite.Tree.randomId;
import static org.openrewrite.java.tree.Space.EMPTY;

/**
* PSI based parser
Expand Down Expand Up @@ -1644,7 +1647,22 @@ public J visitKtFile(KtFile file, ExecutionContext data) {
List<JRightPadded<Statement>> statements = new ArrayList<>(file.getDeclarations().size());
List<KtDeclaration> declarations = file.getDeclarations();
for (KtDeclaration declaration : declarations) {
Statement statement = convertToStatement(declaration.accept(this, data));
Statement statement;
try {
statement = convertToStatement(declaration.accept(this, data));
} catch (Exception e) {
statement = new J.Unknown(
randomId(),
deepPrefix(declaration),
Markers.EMPTY,
new J.Unknown.Source(
randomId(),
EMPTY,
Markers.build(singletonList(ParseExceptionResult.build(KotlinParser.builder().build(), e)
.withTreeType(declaration.getClass().getName()))),
file.getText().substring(PsiUtilsKt.getStartOffsetSkippingComments(declaration),
declaration.getTextRange().getEndOffset())));
}
statements.add(padRight(statement, Space.EMPTY));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@
@SuppressWarnings("ALL")
class ClassDeclarationTest implements RewriteTest {

@Test
void whitespaceInPackage() {
rewriteRun(
kotlin(
"package foo . bar"
)
);
}

@Test
void whitespaceInImport() {
rewriteRun(
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/openrewrite/kotlin/tree/PackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@

class PackageTest implements RewriteTest {

@Test
void whitespaceInPackage() {
rewriteRun(
kotlin(
"package foo . bar"
)
);
}

@Test
void regular() {
rewriteRun(
Expand Down

1 comment on commit 940cdd4

@kunli2
Copy link
Contributor

@kunli2 kunli2 commented on 940cdd4 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not create J.Unknown for any case, but let it roar but not hide, so we will know and fix it, what do you think?

Please sign in to comment.