Skip to content

Commit

Permalink
Added ParsingExecutionContextView to the TSCMapper.
Browse files Browse the repository at this point in the history
Set inputs to parsed through the parserListener in the TSCMapper.
  • Loading branch information
traceyyoshima committed May 24, 2023
1 parent 33be45f commit ac86e00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
21 changes: 12 additions & 9 deletions src/main/java/org/openrewrite/javascript/JavaScriptParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ public List<JS.CompilationUnit> parse(@NonNull String... sources) {
}

@Override
public List<JS.CompilationUnit> parseInputs(Iterable<Input> inputs, @Nullable Path relativeTo, ExecutionContext ogCtx) {
ParsingExecutionContextView ctx = ParsingExecutionContextView.view(ogCtx);
public List<JS.CompilationUnit> parseInputs(Iterable<Input> inputs, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingExecutionContextView pctx = ParsingExecutionContextView.view(ctx);
List<JS.CompilationUnit> outputs;
try (TSCMapper mapper = new TSCMapper(relativeTo) {
try (TSCMapper mapper = new TSCMapper(relativeTo, pctx) {
@Override
protected void onParseFailure(Input input, Throwable error) {
ctx.parseFailure(input, relativeTo, JavaScriptParser.this, error);
pctx.parseFailure(input, relativeTo, JavaScriptParser.this, error);
ctx.getOnError().accept(error);
}
}) {

for (Input input : inputs) {
mapper.add(input, ctx);
mapper.add(input);
}

outputs = mapper.build();
Expand All @@ -81,19 +81,20 @@ protected void onParseFailure(Input input, Throwable error) {
}

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

@Override
public boolean accept(Path path) {
final String filename = path.getFileName().toString().toLowerCase();
if (path.toString().contains("/dist/")) {
// FIXME this is a workaround to not having tsconfig info
return false;
}

final String filename = path.getFileName().toString().toLowerCase();
for (String ext : EXTENSIONS) {
if (filename.endsWith("." + ext)) {
if (filename.endsWith(ext)) {
return true;
}
}
Expand All @@ -110,6 +111,8 @@ public static Builder builder() {
}

public static class Builder extends Parser.Builder {
// FIXME add logCompilationWarningsAndErrors.

public Builder() {
super(JS.CompilationUnit.class);
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/openrewrite/javascript/internal/TSCMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package org.openrewrite.javascript.internal;

import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Parser;
import org.openrewrite.internal.EncodingDetectingInputStream;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.javascript.internal.tsc.TSCRuntime;
import org.openrewrite.javascript.tree.JS;
import org.openrewrite.tree.ParsingExecutionContextView;

import java.nio.charset.Charset;
import java.nio.file.Path;
Expand All @@ -35,6 +35,7 @@ public abstract class TSCMapper implements AutoCloseable {

@Value
private static class SourceWrapper {
Parser.Input input;
Path sourcePath;
Charset charset;
boolean isCharsetBomMarked;
Expand All @@ -46,20 +47,23 @@ private static class SourceWrapper {
@Nullable
private final Path relativeTo;

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

public TSCMapper(@Nullable Path relativeTo) {
public TSCMapper(@Nullable Path relativeTo, ParsingExecutionContextView pctx) {
JavetNativeBridge.init();
this.runtime = TSCRuntime.init();
this.relativeTo = relativeTo;
this.pctx = pctx;
}

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

final SourceWrapper source = new SourceWrapper(
input,
relativePath,
is.getCharset(),
is.isCharsetBomMarked(),
Expand Down Expand Up @@ -90,7 +94,9 @@ public List<JS.CompilationUnit> build() {
source.getCharset().toString(),
source.isCharsetBomMarked()
);
compilationUnits.add(fileMapper.visitSourceFile());
JS.CompilationUnit cu = fileMapper.visitSourceFile();
pctx.getParsingListener().parsed(source.getInput(), cu);
compilationUnits.add(cu);
}
);
return compilationUnits;
Expand Down

0 comments on commit ac86e00

Please sign in to comment.