Skip to content

Commit

Permalink
Add startedParsing listener
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Sep 10, 2023
1 parent 45bf753 commit f29d896
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
for (Input source : sources) {
mapper.add(source);
}

outputs = mapper.build();
}
return outputs.stream();
Expand Down
45 changes: 20 additions & 25 deletions src/main/java/org/openrewrite/javascript/internal/TSCMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.javascript.internal;

import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Parser;
Expand All @@ -33,17 +34,9 @@
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 @@ -54,20 +47,12 @@ private static class SourceWrapper {
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) {
final EncodingDetectingInputStream is = input.getSource(ctx);
final String inputSourceText = is.readFully();
final Path relativePath = input.getRelativePath(relativeTo);
EncodingDetectingInputStream is = input.getSource(ctx);
String inputSourceText = is.readFully();
Path relativePath = input.getRelativePath(relativeTo);

final SourceWrapper source = new SourceWrapper(
SourceWrapper source = new SourceWrapper(
input,
relativePath,
is.getCharset(),
Expand All @@ -81,15 +66,16 @@ 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) -> {
sourcesByRelativePath.forEach((relativePath, sourceText) -> {
sourceTextsForTSC.put(relativePath, sourceText.sourceText);
});

this.runtime.parseSourceTexts(
runtime.parseSourceTexts(
sourceTextsForTSC,
(node, context) -> {
final SourceWrapper source = this.sourcesByRelativePath.get(context.getRelativeSourcePath());
final TypeScriptParserVisitor fileMapper = new TypeScriptParserVisitor(
SourceWrapper source = sourcesByRelativePath.get(context.getRelativeSourcePath());
parsingListener.startedParsing(source.getInput());
TypeScriptParserVisitor fileMapper = new TypeScriptParserVisitor(
node,
context,
source.getSourcePath(),
Expand All @@ -109,6 +95,7 @@ public List<SourceFile> build() {
compilationUnits.add(cu);
}
);

return compilationUnits;
}

Expand All @@ -117,4 +104,12 @@ public void close() {
this.runtime.close();
}

@Value
private static class SourceWrapper {
Parser.Input input;
Path sourcePath;
Charset charset;
boolean isCharsetBomMarked;
String sourceText;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public static TSCRuntime init(boolean forceWrappedV8Runtime) {
JavetException e) {
throw new RuntimeException(e);
}

}

public TSCRuntime setCompilerOptionOverride(String key, Object value) {
Expand Down

0 comments on commit f29d896

Please sign in to comment.