-
Notifications
You must be signed in to change notification settings - Fork 1.7k
JavaScript: Don't extract obviously generated files #19680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tausbn
wants to merge
5
commits into
main
Choose a base branch
from
tausbn/javascript-exclude-obviously-generated-files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14f5088
JavaScript: Don't extract files in `tsconfig.json` `outDir`
tausbn 8829f78
JavaScript: Don't extract files with TypeScript progenitors
tausbn 619256e
JavaScript: Fix existing tests and test runner
tausbn 281ccf7
JavaScript: Extract `tsconfig.json` also in `basic` mode
tausbn b8772bc
JavaScript: Add change note
tausbn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
javascript/extractor/src/com/semmle/js/dependencies/tsconfig/CompilerOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.semmle.js.dependencies.tsconfig; | ||
|
||
public class CompilerOptions { | ||
private String outDir; | ||
|
||
public String getOutDir() { | ||
return outDir; | ||
} | ||
|
||
public void setOutDir(String outDir) { | ||
this.outDir = outDir; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
javascript/extractor/src/com/semmle/js/dependencies/tsconfig/TsConfigJson.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.semmle.js.dependencies.tsconfig; | ||
|
||
public class TsConfigJson { | ||
private CompilerOptions compilerOptions; | ||
|
||
public CompilerOptions getCompilerOptions() { | ||
return compilerOptions; | ||
} | ||
|
||
public void setCompilerOptions(CompilerOptions compilerOptions) { | ||
this.compilerOptions = compilerOptions; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -39,6 +39,8 @@ | |||
|
||||
import com.google.gson.Gson; | ||||
import com.google.gson.JsonParseException; | ||||
import com.semmle.js.dependencies.tsconfig.TsConfigJson; | ||||
import com.semmle.js.dependencies.tsconfig.CompilerOptions; | ||||
import com.semmle.js.dependencies.AsyncFetcher; | ||||
import com.semmle.js.dependencies.DependencyResolver; | ||||
import com.semmle.js.dependencies.packument.PackageJson; | ||||
|
@@ -745,6 +747,26 @@ private CompletableFuture<?> extractSource() throws IOException { | |||
.filter(p -> !isFileTooLarge(p)) | ||||
.sorted(PATH_ORDERING) | ||||
.collect(Collectors.toCollection(() -> new LinkedHashSet<>())); | ||||
// exclude files in output directories as configured in tsconfig.json | ||||
final List<Path> outDirs = new ArrayList<>(); | ||||
for (Path cfg : tsconfigFiles) { | ||||
try { | ||||
String txt = new WholeIO().read(cfg); | ||||
TsConfigJson root = new Gson().fromJson(txt, TsConfigJson.class); | ||||
if (root != null && root.getCompilerOptions() != null) { | ||||
if (root.getCompilerOptions().getOutDir() == null) { | ||||
// no outDir specified, so skip this tsconfig.json | ||||
continue; | ||||
} | ||||
Path odir = cfg.getParent().resolve(root.getCompilerOptions().getOutDir()).toAbsolutePath().normalize(); | ||||
outDirs.add(odir); | ||||
} | ||||
} catch (Exception e) { | ||||
// ignore malformed tsconfig or missing fields | ||||
} | ||||
} | ||||
// exclude files in output directories as configured in tsconfig.json | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] This comment duplicates the one above; consider removing it to reduce redundancy and keep the code concise.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||
filesToExtract.removeIf(f -> outDirs.stream().anyMatch(od -> f.startsWith(od))); | ||||
|
||||
DependencyInstallationResult dependencyInstallationResult = DependencyInstallationResult.empty; | ||||
if (!tsconfigFiles.isEmpty()) { | ||||
|
@@ -796,9 +818,19 @@ private CompletableFuture<?> extractFiles( | |||
*/ | ||||
private boolean isFileDerivedFromTypeScriptFile(Path path, Set<Path> extractedFiles) { | ||||
String name = path.getFileName().toString(); | ||||
if (!name.endsWith(".js")) | ||||
// only skip JS variants when a corresponding TS/TSX file was already extracted | ||||
if (!(name.endsWith(".js") | ||||
|| name.endsWith(".cjs") | ||||
|| name.endsWith(".mjs") | ||||
|| name.endsWith(".jsx") | ||||
|| name.endsWith(".cjsx") | ||||
|| name.endsWith(".mjsx"))) { | ||||
return false; | ||||
String stem = name.substring(0, name.length() - ".js".length()); | ||||
} | ||||
// strip off extension | ||||
int dot = name.lastIndexOf('.'); | ||||
String stem = dot != -1 ? name.substring(0, dot) : name; | ||||
// if a TS/TSX file with same base name was extracted, skip this file | ||||
for (String ext : FileType.TYPESCRIPT.getExtensions()) { | ||||
if (extractedFiles.contains(path.getParent().resolve(stem + ext))) { | ||||
return true; | ||||
|
@@ -1154,7 +1186,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) | |||
} | ||||
|
||||
// extract TypeScript projects from 'tsconfig.json' | ||||
if (typeScriptMode == TypeScriptMode.FULL | ||||
if (typeScriptMode != TypeScriptMode.NONE | ||||
&& treatAsTSConfig(file.getFileName().toString()) | ||||
&& !excludes.contains(file) | ||||
&& isFileIncluded(file)) { | ||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
javascript/ql/lib/change-notes/2025-06-05-skip-obviously-generated-files.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* The JavaScript extractor now skips generated JavaScript files if the original TypeScript files are already present. It also skips any files in the output directory specified in the `compilerOptions` part of the `tsconfig.json` file. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Silently swallowing all exceptions during tsconfig parsing can obscure errors; consider logging a warning or including the exception message.
Copilot uses AI. Check for mistakes.