Skip to content

Commit 14f5088

Browse files
committed
JavaScript: Don't extract files in tsconfig.json outDir
1 parent 4de3817 commit 14f5088

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.semmle.js.dependencies.tsconfig;
2+
3+
public class CompilerOptions {
4+
private String outDir;
5+
6+
public String getOutDir() {
7+
return outDir;
8+
}
9+
10+
public void setOutDir(String outDir) {
11+
this.outDir = outDir;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.semmle.js.dependencies.tsconfig;
2+
3+
public class TsConfigJson {
4+
private CompilerOptions compilerOptions;
5+
6+
public CompilerOptions getCompilerOptions() {
7+
return compilerOptions;
8+
}
9+
10+
public void setCompilerOptions(CompilerOptions compilerOptions) {
11+
this.compilerOptions = compilerOptions;
12+
}
13+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
import com.google.gson.Gson;
4141
import com.google.gson.JsonParseException;
42+
import com.semmle.js.dependencies.tsconfig.TsConfigJson;
43+
import com.semmle.js.dependencies.tsconfig.CompilerOptions;
4244
import com.semmle.js.dependencies.AsyncFetcher;
4345
import com.semmle.js.dependencies.DependencyResolver;
4446
import com.semmle.js.dependencies.packument.PackageJson;
@@ -745,6 +747,26 @@ private CompletableFuture<?> extractSource() throws IOException {
745747
.filter(p -> !isFileTooLarge(p))
746748
.sorted(PATH_ORDERING)
747749
.collect(Collectors.toCollection(() -> new LinkedHashSet<>()));
750+
// exclude files in output directories as configured in tsconfig.json
751+
final List<Path> outDirs = new ArrayList<>();
752+
for (Path cfg : tsconfigFiles) {
753+
try {
754+
String txt = new WholeIO().read(cfg);
755+
TsConfigJson root = new Gson().fromJson(txt, TsConfigJson.class);
756+
if (root != null && root.getCompilerOptions() != null) {
757+
if (root.getCompilerOptions().getOutDir() == null) {
758+
// no outDir specified, so skip this tsconfig.json
759+
continue;
760+
}
761+
Path odir = cfg.getParent().resolve(root.getCompilerOptions().getOutDir()).toAbsolutePath().normalize();
762+
outDirs.add(odir);
763+
}
764+
} catch (Exception e) {
765+
// ignore malformed tsconfig or missing fields
766+
}
767+
}
768+
// exclude files in output directories as configured in tsconfig.json
769+
filesToExtract.removeIf(f -> outDirs.stream().anyMatch(od -> f.startsWith(od)));
748770

749771
DependencyInstallationResult dependencyInstallationResult = DependencyInstallationResult.empty;
750772
if (!tsconfigFiles.isEmpty()) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,24 @@ public void typescriptWrongConfig() throws IOException {
203203
runTest();
204204
}
205205

206+
@Test
207+
public void skipFilesInTsconfigOutDir() throws IOException {
208+
envVars.put("LGTM_INDEX_TYPESCRIPT", "basic");
209+
// Files under outDir in tsconfig.json should be excluded
210+
// Create tsconfig.json with outDir set to "dist"
211+
addFile(true, LGTM_SRC, "tsconfig.json");
212+
Path config = Paths.get(LGTM_SRC.toString(), "tsconfig.json");
213+
Files.write(config,
214+
"{\"compilerOptions\":{\"outDir\":\"dist\"}}".getBytes(StandardCharsets.UTF_8));
215+
// Add files outside outDir (should be extracted)
216+
addFile(true, LGTM_SRC, "src", "app.ts");
217+
addFile(true, LGTM_SRC, "main.js");
218+
// Add files under dist/outDir (should be skipped)
219+
addFile(false, LGTM_SRC, "dist", "generated.js");
220+
addFile(false, LGTM_SRC, "dist", "sub", "x.js");
221+
runTest();
222+
}
223+
206224
@Test
207225
public void includeFile() throws IOException {
208226
envVars.put("LGTM_INDEX_INCLUDE", "tst.js");

0 commit comments

Comments
 (0)