Skip to content

Commit bfedcb0

Browse files
committed
JS: Make TypeScript aware of custom extractor extensions
1 parent d55e330 commit bfedcb0

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ public int compare(File f1, File f2) {
569569
}
570570
};
571571

572-
class FileExtractors {
572+
public class FileExtractors {
573573
FileExtractor defaultExtractor;
574574
Map<String, FileExtractor> customExtractors = new LinkedHashMap<>();
575575

@@ -580,6 +580,10 @@ class FileExtractors {
580580
public FileExtractor forFile(Path f) {
581581
return customExtractors.getOrDefault(FileUtil.extension(f), defaultExtractor);
582582
}
583+
584+
public FileType fileType(Path f) {
585+
return forFile(f).getFileType(f.toFile());
586+
}
583587
}
584588

585589
/** Extract all supported candidate files that pass the filters. */
@@ -614,11 +618,11 @@ private void extractSource() throws IOException {
614618
if (!tsconfigFiles.isEmpty()) {
615619
dependencyInstallationResult = this.preparePackagesAndDependencies(filesToExtract);
616620
}
621+
Set<Path> extractedFiles = new LinkedHashSet<>();
617622

618623
// extract TypeScript projects and files
619-
Set<Path> extractedFiles =
620-
extractTypeScript(
621-
defaultExtractor, filesToExtract, tsconfigFiles, dependencyInstallationResult);
624+
extractTypeScript(filesToExtract, extractedFiles,
625+
extractors, tsconfigFiles, dependencyInstallationResult);
622626

623627
boolean hasTypeScriptFiles = extractedFiles.size() > 0;
624628

@@ -959,12 +963,11 @@ private ExtractorConfig mkExtractorConfig() {
959963
}
960964

961965
private Set<Path> extractTypeScript(
962-
FileExtractor extractor,
963966
Set<Path> files,
967+
Set<Path> extractedFiles,
968+
FileExtractors extractors,
964969
List<Path> tsconfig,
965970
DependencyInstallationResult deps) {
966-
Set<Path> extractedFiles = new LinkedHashSet<>();
967-
968971
if (hasTypeScriptFiles(files) || !tsconfig.isEmpty()) {
969972
ExtractorState extractorState = new ExtractorState();
970973
TypeScriptParser tsParser = extractorState.getTypeScriptParser();
@@ -993,7 +996,7 @@ private Set<Path> extractTypeScript(
993996
Path sourcePath = sourceFile.toPath();
994997
if (!files.contains(normalizePath(sourcePath))) continue;
995998
if (!project.getOwnFiles().contains(sourceFile) && explicitlyIncludedFiles.contains(sourceFile)) continue;
996-
if (!FileType.TYPESCRIPT.getExtensions().contains(FileUtil.extension(sourcePath))) {
999+
if (extractors.fileType(sourcePath) != FileType.TYPESCRIPT) {
9971000
// For the time being, skip non-TypeScript files, even if the TypeScript
9981001
// compiler can parse them for us.
9991002
continue;
@@ -1003,7 +1006,7 @@ private Set<Path> extractTypeScript(
10031006
}
10041007
}
10051008
typeScriptFiles.sort(PATH_ORDERING);
1006-
extractTypeScriptFiles(typeScriptFiles, extractedFiles, extractor, extractorState);
1009+
extractTypeScriptFiles(typeScriptFiles, extractedFiles, extractors, extractorState);
10071010
tsParser.closeProject(projectFile);
10081011
}
10091012

@@ -1017,12 +1020,12 @@ private Set<Path> extractTypeScript(
10171020
List<Path> remainingTypeScriptFiles = new ArrayList<>();
10181021
for (Path f : files) {
10191022
if (!extractedFiles.contains(f)
1020-
&& FileType.forFileExtension(f.toFile()) == FileType.TYPESCRIPT) {
1023+
&& extractors.fileType(f) == FileType.TYPESCRIPT) {
10211024
remainingTypeScriptFiles.add(f);
10221025
}
10231026
}
10241027
if (!remainingTypeScriptFiles.isEmpty()) {
1025-
extractTypeScriptFiles(remainingTypeScriptFiles, extractedFiles, extractor, extractorState);
1028+
extractTypeScriptFiles(remainingTypeScriptFiles, extractedFiles, extractors, extractorState);
10261029
}
10271030

10281031
// The TypeScript compiler instance is no longer needed.
@@ -1108,7 +1111,7 @@ public void verifyTypeScriptInstallation(ExtractorState extractorState) {
11081111
public void extractTypeScriptFiles(
11091112
List<Path> files,
11101113
Set<Path> extractedFiles,
1111-
FileExtractor extractor,
1114+
FileExtractors extractors,
11121115
ExtractorState extractorState) {
11131116
List<File> list = files
11141117
.stream()
@@ -1117,7 +1120,7 @@ public void extractTypeScriptFiles(
11171120
extractorState.getTypeScriptParser().prepareFiles(list);
11181121
for (Path path : files) {
11191122
extractedFiles.add(path);
1120-
extract(extractor, path, extractorState);
1123+
extract(extractors.forFile(path), path, extractorState);
11211124
}
11221125
}
11231126

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void verifyTypeScriptInstallation(ExtractorState state) {}
123123
public void extractTypeScriptFiles(
124124
java.util.List<Path> files,
125125
java.util.Set<Path> extractedFiles,
126-
FileExtractor extractor,
126+
FileExtractors extractors,
127127
ExtractorState extractorState) {
128128
for (Path f : files) {
129129
actual.add(f.toString());

0 commit comments

Comments
 (0)