Skip to content

Commit

Permalink
Merge pull request #67 from openrewrite/revised_mapper_to_add_parser_…
Browse files Browse the repository at this point in the history
…errors

Revised TSCMapper to create ParseErrors.
  • Loading branch information
traceyyoshima authored Jul 12, 2023
2 parents 07a46f0 + 940fe00 commit 45bf753
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.openrewrite.javascript;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Parser;
Expand Down Expand Up @@ -67,12 +65,7 @@ public Stream<SourceFile> parse(@NonNull String... sources) {
public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingExecutionContextView pctx = ParsingExecutionContextView.view(ctx);
List<SourceFile> outputs;
try (TSCMapper mapper = new TSCMapper(relativeTo, styles, pctx) {
@Override
protected void onParseFailure(Input input, Throwable error) {
ctx.getOnError().accept(error);
}
}) {
try (TSCMapper mapper = new TSCMapper(relativeTo, styles, pctx) {}) {
for (Input source : sources) {
mapper.add(source);
}
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/org/openrewrite/javascript/internal/TSCMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
package org.openrewrite.javascript.internal;

import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Parser;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.EncodingDetectingInputStream;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.javascript.JavaScriptParser;
import org.openrewrite.javascript.internal.tsc.TSCRuntime;
import org.openrewrite.javascript.tree.JS;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.tree.ParseError;
import org.openrewrite.tree.ParsingEventListener;
import org.openrewrite.tree.ParsingExecutionContextView;

import java.nio.charset.Charset;
Expand All @@ -48,19 +51,19 @@ private static class SourceWrapper {

private final Collection<NamedStyles> styles;

private final ParsingExecutionContextView pctx;
private final ExecutionContext ctx;
private final Map<Path, SourceWrapper> sourcesByRelativePath = new LinkedHashMap<>();

public TSCMapper(@Nullable Path relativeTo, Collection<NamedStyles> styles, ParsingExecutionContextView pctx) {
public TSCMapper(@Nullable Path relativeTo, Collection<NamedStyles> styles, ExecutionContext ctx) {
JavetNativeBridge.init();
this.runtime = TSCRuntime.init();
this.relativeTo = relativeTo;
this.styles = styles;
this.pctx = pctx;
this.ctx = ctx;
}

public void add(Parser.Input input) {
final EncodingDetectingInputStream is = input.getSource(pctx);
final EncodingDetectingInputStream is = input.getSource(ctx);
final String inputSourceText = is.readFully();
final Path relativePath = input.getRelativePath(relativeTo);

Expand All @@ -74,11 +77,9 @@ public void add(Parser.Input input) {
sourcesByRelativePath.put(relativePath, source);
}

protected abstract void onParseFailure(Parser.Input input, Throwable error);

public List<SourceFile> build() {
List<SourceFile> compilationUnits = new ArrayList<>(sourcesByRelativePath.size());

ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
Map<Path, String> sourceTextsForTSC = new LinkedHashMap<>();
this.sourcesByRelativePath.forEach((relativePath, sourceText) -> {
sourceTextsForTSC.put(relativePath, sourceText.sourceText);
Expand All @@ -97,8 +98,14 @@ public List<SourceFile> build() {
source.isCharsetBomMarked(),
styles
);
JS.CompilationUnit cu = fileMapper.visitSourceFile();
pctx.getParsingListener().parsed(source.getInput(), cu);
SourceFile cu;
try {
cu = fileMapper.visitSourceFile();
parsingListener.parsed(source.getInput(), cu);
} catch (Throwable t) {
ctx.getOnError().accept(t);
cu = ParseError.build(JavaScriptParser.builder().build(), source.getInput(), relativeTo, ctx, t);
}
compilationUnits.add(cu);
}
);
Expand Down

0 comments on commit 45bf753

Please sign in to comment.