Skip to content

Commit

Permalink
Handle RunTime exception thrown in TSCRuntime init.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Nov 30, 2023
1 parent b0b6362 commit de89241
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/main/java/org/openrewrite/javascript/JavaScriptParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.javascript.internal.JavetNativeBridge;
import org.openrewrite.javascript.internal.TsTreePrinter;
import org.openrewrite.javascript.internal.TypeScriptParserVisitor;
import org.openrewrite.javascript.internal.tsc.TSCRuntime;
import org.openrewrite.javascript.tree.JS;
Expand All @@ -41,7 +40,6 @@
import java.util.*;
import java.util.stream.Stream;

@SuppressWarnings("ConstantValue")
public class JavaScriptParser implements Parser {

@Nullable
Expand Down Expand Up @@ -118,34 +116,38 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
sourceTextsForTSC.put(relativePath, sourceText.sourceText);
});

//noinspection resource
runtime().parseSourceTexts(
sourceTextsForTSC,
(node, context) -> {
SourceWrapper source = sourcesByRelativePath.get(context.getRelativeSourcePath());

parsingListener.startedParsing(source.getInput());
TypeScriptParserVisitor fileMapper = new TypeScriptParserVisitor(
node,
context,
source.getSourcePath(),
new JavaTypeCache(),
source.getCharset().toString(),
source.isCharsetBomMarked(),
styles
);
SourceFile cu;
try {
cu = fileMapper.visitSourceFile();
parsingListener.parsed(source.getInput(), cu);
} catch (Throwable t) {
((ExecutionContext) pctx).getOnError().accept(t);
cu = ParseError.build(JavaScriptParser.builder().build(), source.getInput(), relativeTo, pctx, t);
try {
//noinspection resource
runtime().parseSourceTexts(
sourceTextsForTSC,
(node, context) -> {
SourceWrapper source = sourcesByRelativePath.get(context.getRelativeSourcePath());

parsingListener.startedParsing(source.getInput());
TypeScriptParserVisitor fileMapper = new TypeScriptParserVisitor(
node,
context,
source.getSourcePath(),
new JavaTypeCache(),
source.getCharset().toString(),
source.isCharsetBomMarked(),
styles
);
SourceFile cu;
try {
cu = fileMapper.visitSourceFile();
parsingListener.parsed(source.getInput(), cu);
} catch (Throwable t) {
((ExecutionContext) pctx).getOnError().accept(t);
cu = ParseError.build(JavaScriptParser.builder().build(), source.getInput(), relativeTo, pctx, t);
}

compilationUnits.add(cu);
}

compilationUnits.add(cu);
}
);
);
} catch (Exception e) {
return acceptedInputs(sources).map(input -> ParseError.build(this, input, relativeTo, ctx, e));
}
return compilationUnits.stream();
}

Expand Down

0 comments on commit de89241

Please sign in to comment.