Skip to content

Commit

Permalink
Implement type validation as part of parser
Browse files Browse the repository at this point in the history
Make sure that all LST elements or of the expected type. Type inconsistencies can occur due to the type erasure of Java generics.
  • Loading branch information
knutwannheden committed Nov 20, 2024
1 parent 0f8c089 commit 40ca0f2
Show file tree
Hide file tree
Showing 2 changed files with 696 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public Stream<SourceFile> parseInputs(Iterable<Input> inputs, @Nullable Path rel
return parsed;
}

JS.CompilationUnit py = (JS.CompilationUnit) parsed;
parsingListener.parsed(input, py);
SourceFile sourceFile = requirePrintEqualsInput(py, input, relativeTo, ctx);
JS.CompilationUnit js = (JS.CompilationUnit) parsed;
parsingListener.parsed(input, js);
SourceFile sourceFile = validate(js, input, relativeTo, ctx);
if (sourceFile instanceof ParseError) {
return ((ParseError) sourceFile).withErroneous(null);
}
Expand All @@ -134,6 +134,16 @@ 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<>();
try {
typeValidator.visit(sourceFile, 0);
return requirePrintEqualsInput(sourceFile, input, relativeTo, ctx);
} catch (Exception e) {
return ParseError.build(this, input, relativeTo, ctx, new IllegalStateException("LST model has type validation errors", e));
}
}

private final static List<String> EXTENSIONS = Collections.unmodifiableList(Arrays.asList(
".js", ".jsx", ".mjs", ".cjs",
".ts", ".tsx", ".mts", ".cts"
Expand Down
Loading

0 comments on commit 40ca0f2

Please sign in to comment.