Skip to content

Commit

Permalink
Do not skip proto3 files, as it warns about out of date LSTs (#3681)
Browse files Browse the repository at this point in the history
* Do not skip proto3 files, as it warns about out of date LSTs

* Update the _one_ test that we have
  • Loading branch information
timtebeek authored Nov 10, 2023
1 parent aaf1993 commit bdaaa25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@

import org.antlr.v4.runtime.*;
import org.intellij.lang.annotations.Language;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Parser;
import org.openrewrite.*;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.EncodingDetectingInputStream;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.Markers;
import org.openrewrite.protobuf.internal.ProtoParserVisitor;
import org.openrewrite.protobuf.internal.grammar.Protobuf2Lexer;
import org.openrewrite.protobuf.internal.grammar.Protobuf2Parser;
import org.openrewrite.protobuf.tree.Proto;
import org.openrewrite.text.PlainText;
import org.openrewrite.tree.ParseError;
import org.openrewrite.tree.ParsingEventListener;
import org.openrewrite.tree.ParsingExecutionContextView;

import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Stream;

import static org.openrewrite.Tree.randomId;

public class ProtoParser implements Parser {

@Override
Expand All @@ -51,7 +56,18 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Pat
parser.addErrorListener(new ForwardingErrorListener(input.getPath(), ctx));

if (sourceStr.contains("proto3")) {
return null;
// Pending Proto3 support, the best we can do is plain text & not skip files
return new PlainText(
randomId(),
path,
Markers.EMPTY,
is.getCharset().name(),
is.isCharsetBomMarked(),
input.getFileAttributes(),
null,
sourceStr,
null
);
}

Proto.Document document = new ProtoParserVisitor(
Expand All @@ -67,9 +83,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Pat
ctx.getOnError().accept(t);
return ParseError.build(this, input, relativeTo, ctx, t);
}
})
// filter out the nulls produced for `proto3` sources
.filter(Objects::nonNull);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.SourceFile;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.text.PlainText;

import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

public class ProtoParserTest implements RewriteTest {

class ProtoParserTest implements RewriteTest {
@Test
void noNullsForProto3Files() {
List<SourceFile> sources = ProtoParser.builder().build().parse("syntax = \"proto3\";")
.collect(Collectors.toList());
assertThat(sources).isEmpty();
List<SourceFile> sources = ProtoParser.builder().build().parse("syntax = \"proto3\";").toList();
assertThat(sources).singleElement().isInstanceOf(PlainText.class);
}
}

0 comments on commit bdaaa25

Please sign in to comment.