Skip to content

Commit

Permalink
Compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Sep 10, 2023
1 parent f29d896 commit c53910e
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/main/java/org/openrewrite/javascript/internal/TSCMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.openrewrite.javascript.internal;

import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Parser;
Expand All @@ -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
Expand All @@ -47,6 +54,14 @@ public abstract class TSCMapper implements AutoCloseable {
private final ExecutionContext ctx;
private final Map<Path, SourceWrapper> sourcesByRelativePath = new LinkedHashMap<>();

public TSCMapper(@Nullable Path relativeTo, Collection<NamedStyles> 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();
Expand All @@ -66,15 +81,14 @@ public List<SourceFile> build() {
List<SourceFile> compilationUnits = new ArrayList<>(sourcesByRelativePath.size());
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
Map<Path, String> 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,
Expand All @@ -95,7 +109,6 @@ public List<SourceFile> build() {
compilationUnits.add(cu);
}
);

return compilationUnits;
}

Expand All @@ -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;
}
}

0 comments on commit c53910e

Please sign in to comment.