Skip to content

Commit

Permalink
Move and register validator
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Nov 22, 2024
1 parent 2e6aa04 commit 173d7bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.remote.Receiver;
import org.openrewrite.remote.Sender;
import org.openrewrite.remote.SenderReceiverProvider;
import org.openrewrite.remote.Validator;

@AutoService(SenderReceiverProvider.class)
public class JavaScriptSenderReceiverProvider implements SenderReceiverProvider<JS> {
Expand All @@ -37,4 +38,9 @@ public Sender<JS> newSender() {
public Receiver<JS> newReceiver() {
return new JavaScriptReceiver();
}

@Override
public Validator newValidator() {
return Validator.fromVisitor(new JavaScriptValidator<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@
* the code is regenerated.
*/

package org.openrewrite.javascript;
package org.openrewrite.javascript.remote;

import org.jspecify.annotations.Nullable;
import org.openrewrite.Tree;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.marker.Markers;
import org.openrewrite.tree.*;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.*;
import org.openrewrite.javascript.tree.JS;
import org.openrewrite.javascript.JavaScriptIsoVisitor;
import org.openrewrite.javascript.tree.*;

import java.util.List;

class JavaScriptValidator<P> extends JavaScriptIsoVisitor<P> {

private <T extends Tree> @Nullable T visitAndValidate(@Nullable T tree, Class<? extends Tree> expected, P p) {
if (tree != null && !expected.isInstance(tree)) {
throw new IllegalStateException("Type " + tree.getClass() + " is not assignable to " + expected);
throw new ClassCastException("Type " + tree.getClass() + " is not assignable to " + expected);
}
// noinspection unchecked
return (T) visit(tree, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openrewrite.remote.RemotingContext;
import org.openrewrite.remote.RemotingExecutionContextView;
import org.openrewrite.remote.RemotingMessenger;
import org.openrewrite.remote.Validator;
import org.openrewrite.remote.java.RemotingClient;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.text.PlainTextParser;
Expand Down Expand Up @@ -135,13 +136,14 @@ public Stream<SourceFile> parseInputs(Iterable<Input> inputs, @Nullable Path rel
}

private SourceFile validate(JS.CompilationUnit sourceFile, Input input, @Nullable Path relativeTo, ExecutionContext ctx) {
JavaScriptValidator<Integer> typeValidator = new JavaScriptValidator<>();
assert remotingContext != null;
Validator validator = remotingContext.getProvider(sourceFile.getClass()).newValidator();
try {
typeValidator.visit(sourceFile, 0);
return requirePrintEqualsInput(sourceFile, input, relativeTo, ctx);
validator.validate(sourceFile, ctx);
} catch (Exception e) {
return ParseError.build(this, input, relativeTo, ctx, new IllegalStateException("LST model has type validation errors", e));
}
return requirePrintEqualsInput(sourceFile, input, relativeTo, ctx);
}

private final static List<String> EXTENSIONS = Collections.unmodifiableList(Arrays.asList(
Expand Down

0 comments on commit 173d7bb

Please sign in to comment.