diff --git a/src/main/java/org/openrewrite/javascript/internal/TSCMapper.java b/src/main/java/org/openrewrite/javascript/internal/TSCMapper.java index e368e4b8..661a3760 100644 --- a/src/main/java/org/openrewrite/javascript/internal/TSCMapper.java +++ b/src/main/java/org/openrewrite/javascript/internal/TSCMapper.java @@ -15,7 +15,6 @@ */ package org.openrewrite.javascript.internal; -import lombok.RequiredArgsConstructor; import lombok.Value; import org.openrewrite.ExecutionContext; import org.openrewrite.Parser; @@ -34,9 +33,17 @@ import java.nio.file.Path; import java.util.*; -@RequiredArgsConstructor public abstract class TSCMapper implements AutoCloseable { + @Value + private static class SourceWrapper { + Parser.Input input; + Path sourcePath; + Charset charset; + boolean isCharsetBomMarked; + String sourceText; + } + private final TSCRuntime runtime; @Nullable @@ -47,6 +54,14 @@ public abstract class TSCMapper implements AutoCloseable { private final ExecutionContext ctx; private final Map sourcesByRelativePath = new LinkedHashMap<>(); + public TSCMapper(@Nullable Path relativeTo, Collection styles, ExecutionContext ctx) { + JavetNativeBridge.init(); + this.runtime = TSCRuntime.init(); + this.relativeTo = relativeTo; + this.styles = styles; + this.ctx = ctx; + } + public void add(Parser.Input input) { EncodingDetectingInputStream is = input.getSource(ctx); String inputSourceText = is.readFully(); @@ -66,15 +81,14 @@ public List build() { List compilationUnits = new ArrayList<>(sourcesByRelativePath.size()); ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener(); Map sourceTextsForTSC = new LinkedHashMap<>(); - sourcesByRelativePath.forEach((relativePath, sourceText) -> { + this.sourcesByRelativePath.forEach((relativePath, sourceText) -> { sourceTextsForTSC.put(relativePath, sourceText.sourceText); }); - runtime.parseSourceTexts( + this.runtime.parseSourceTexts( sourceTextsForTSC, (node, context) -> { - SourceWrapper source = sourcesByRelativePath.get(context.getRelativeSourcePath()); - parsingListener.startedParsing(source.getInput()); + SourceWrapper source = this.sourcesByRelativePath.get(context.getRelativeSourcePath()); TypeScriptParserVisitor fileMapper = new TypeScriptParserVisitor( node, context, @@ -95,7 +109,6 @@ public List build() { compilationUnits.add(cu); } ); - return compilationUnits; } @@ -104,12 +117,4 @@ public void close() { this.runtime.close(); } - @Value - private static class SourceWrapper { - Parser.Input input; - Path sourcePath; - Charset charset; - boolean isCharsetBomMarked; - String sourceText; - } }