Skip to content

Commit 8829f78

Browse files
committed
JavaScript: Don't extract files with TypeScript progenitors
1 parent 14f5088 commit 8829f78

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,19 @@ private CompletableFuture<?> extractFiles(
818818
*/
819819
private boolean isFileDerivedFromTypeScriptFile(Path path, Set<Path> extractedFiles) {
820820
String name = path.getFileName().toString();
821-
if (!name.endsWith(".js"))
821+
// only skip JS variants when a corresponding TS/TSX file was already extracted
822+
if (!(name.endsWith(".js")
823+
|| name.endsWith(".cjs")
824+
|| name.endsWith(".mjs")
825+
|| name.endsWith(".jsx")
826+
|| name.endsWith(".cjsx")
827+
|| name.endsWith(".mjsx"))) {
822828
return false;
823-
String stem = name.substring(0, name.length() - ".js".length());
829+
}
830+
// strip off extension
831+
int dot = name.lastIndexOf('.');
832+
String stem = dot != -1 ? name.substring(0, dot) : name;
833+
// if a TS/TSX file with same base name was extracted, skip this file
824834
for (String ext : FileType.TYPESCRIPT.getExtensions()) {
825835
if (extractedFiles.contains(path.getParent().resolve(stem + ext))) {
826836
return true;

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

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

206+
@Test
207+
public void skipJsFilesDerivedFromTypeScriptFiles() throws IOException {
208+
// JS-derived files (.js, .cjs, .mjs, .jsx, .cjsx, .mjsx) should be skipped when TS indexing
209+
envVars.put("LGTM_INDEX_TYPESCRIPT", "basic");
210+
// Add TypeScript sources
211+
addFile(true, LGTM_SRC, "foo.ts");
212+
addFile(true, LGTM_SRC, "bar.tsx");
213+
// Add derived JS variants (should be skipped)
214+
addFile(false, LGTM_SRC, "foo.js");
215+
addFile(false, LGTM_SRC, "bar.jsx");
216+
addFile(false, LGTM_SRC, "foo.cjs");
217+
addFile(false, LGTM_SRC, "foo.mjs");
218+
addFile(false, LGTM_SRC, "bar.cjsx");
219+
addFile(false, LGTM_SRC, "bar.mjsx");
220+
// A normal JS file without TS counterpart should be extracted
221+
addFile(true, LGTM_SRC, "normal.js");
222+
runTest();
223+
}
224+
206225
@Test
207226
public void skipFilesInTsconfigOutDir() throws IOException {
208227
envVars.put("LGTM_INDEX_TYPESCRIPT", "basic");

0 commit comments

Comments
 (0)