Skip to content

Commit 1297d0f

Browse files
committed
JS: Extract HTML before TypeScript
1 parent 2c1567a commit 1297d0f

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.Set;
29+
import java.util.concurrent.CompletableFuture;
2930
import java.util.concurrent.ExecutorService;
3031
import java.util.concurrent.Executors;
3132
import java.util.concurrent.TimeUnit;
@@ -621,6 +622,13 @@ private void extractSource() throws IOException {
621622
dependencyInstallationResult = this.preparePackagesAndDependencies(filesToExtract);
622623
}
623624
Set<Path> extractedFiles = new LinkedHashSet<>();
625+
626+
// Extract HTML files as they may contain TypeScript
627+
CompletableFuture<?> htmlFuture = extractFiles(
628+
filesToExtract, extractedFiles, extractors,
629+
f -> extractors.fileType(f) == FileType.HTML);
630+
631+
htmlFuture.join(); // Wait for HTML extraction to be finished.
624632

625633
// extract TypeScript projects and files
626634
extractTypeScript(filesToExtract, extractedFiles,
@@ -634,21 +642,23 @@ private void extractSource() throws IOException {
634642
f -> !(hasTypeScriptFiles && isFileDerivedFromTypeScriptFile(f, extractedFiles)));
635643
}
636644

637-
private void extractFiles(
645+
private CompletableFuture<?> extractFiles(
638646
Set<Path> filesToExtract,
639647
Set<Path> extractedFiles,
640648
FileExtractors extractors,
641649
Predicate<Path> shouldExtract) {
642650

651+
List<CompletableFuture<?>> futures = new ArrayList<>();
643652
for (Path f : filesToExtract) {
644653
if (extractedFiles.contains(f))
645654
continue;
646655
if (!shouldExtract.test(f)) {
647656
continue;
648657
}
649658
extractedFiles.add(f);
650-
extract(extractors.forFile(f), f, null);
659+
futures.add(extract(extractors.forFile(f), f, null));
651660
}
661+
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
652662
}
653663

654664
/**
@@ -1164,10 +1174,13 @@ private SourceType getSourceType() {
11641174
* <p>If the state is {@code null}, the extraction job will be submitted to the {@link
11651175
* #threadPool}, otherwise extraction will happen on the main thread.
11661176
*/
1167-
protected void extract(FileExtractor extractor, Path file, ExtractorState state) {
1168-
if (state == null && threadPool != null)
1169-
threadPool.submit(() -> doExtract(extractor, file, state));
1170-
else doExtract(extractor, file, state);
1177+
protected CompletableFuture<?> extract(FileExtractor extractor, Path file, ExtractorState state) {
1178+
if (state == null && threadPool != null) {
1179+
return CompletableFuture.runAsync(() -> doExtract(extractor, file, state), threadPool);
1180+
} else {
1181+
doExtract(extractor, file, state);
1182+
return CompletableFuture.completedFuture(null);
1183+
}
11711184
}
11721185

11731186
private void doExtract(FileExtractor extractor, Path file, ExtractorState state) {

javascript/extractor/src/com/semmle/js/extractor/test/AutoBuildTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.List;
1616
import java.util.Map;
1717
import java.util.Set;
18+
import java.util.concurrent.CompletableFuture;
1819

1920
import org.junit.After;
2021
import org.junit.Assert;
@@ -109,11 +110,12 @@ private void runTest() throws IOException {
109110
Set<String> actual = new LinkedHashSet<>();
110111
new AutoBuild() {
111112
@Override
112-
protected void extract(FileExtractor extractor, Path file, ExtractorState state) {
113+
protected CompletableFuture<?> extract(FileExtractor extractor, Path file, ExtractorState state) {
113114
String extracted = file.toString();
114115
if (extractor.getConfig().hasFileType())
115116
extracted += ":" + extractor.getFileType(file.toFile());
116117
actual.add(extracted);
118+
return CompletableFuture.completedFuture(null);
117119
}
118120

119121
@Override

0 commit comments

Comments
 (0)