Skip to content

Commit

Permalink
Merge pull request #18622 from asgerf/js/typescript-tsconfig-names
Browse files Browse the repository at this point in the history
JS: Treat more file patterns as tsconfig-like files
  • Loading branch information
asgerf authored Jan 31, 2025
2 parents a45da05 + 2e65fe9 commit a0af4c9
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 4 deletions.
10 changes: 7 additions & 3 deletions javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ private void setupFilters() {
for (FileType filetype : defaultExtract)
for (String extension : filetype.getExtensions()) patterns.add("**/*" + extension);

// include .eslintrc files, .xsaccess files, package.json files,
// include .eslintrc files, .xsaccess files, package.json files,
// tsconfig.json files, and codeql-javascript-*.json files
patterns.add("**/.eslintrc*");
patterns.add("**/.xsaccess");
Expand Down Expand Up @@ -895,7 +895,7 @@ protected DependencyInstallationResult preparePackagesAndDependencies(Set<Path>
// For named packages, find the main file.
String name = packageJson.getName();
if (name != null) {
Path entryPoint = null;
Path entryPoint = null;
try {
entryPoint = guessPackageMainFile(path, packageJson, FileType.TYPESCRIPT.getExtensions());
if (entryPoint == null) {
Expand Down Expand Up @@ -1108,6 +1108,10 @@ private boolean hasTypeScriptFiles(Set<Path> filesToExtract) {
return false;
}

public static boolean treatAsTSConfig(String basename) {
return basename.contains("tsconfig.") && basename.endsWith(".json");
}

private void findFilesToExtract(
FileExtractor extractor, final Set<Path> filesToExtract, final List<Path> tsconfigFiles)
throws IOException {
Expand Down Expand Up @@ -1140,7 +1144,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)

// extract TypeScript projects from 'tsconfig.json'
if (typeScriptMode == TypeScriptMode.FULL
&& file.getFileName().endsWith("tsconfig.json")
&& treatAsTSConfig(file.getFileName().toString())
&& !excludes.contains(file)
&& isFileIncluded(file)) {
tsconfigFiles.add(file);
Expand Down
2 changes: 1 addition & 1 deletion javascript/extractor/src/com/semmle/js/extractor/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ private void collectFiles(File root, boolean explicit) {
}

if (extractorConfig.getTypeScriptMode() == TypeScriptMode.FULL
&& root.getName().equals("tsconfig.json")
&& AutoBuild.treatAsTSConfig(root.getName())
&& !excludeMatcher.matches(path)) {
projectFiles.add(root);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
category: majorAnalysis
---
* TypeScript extraction is now better at analyzing projects where the main `tsconfig.json` file does not include any
source files, but references other `tsconfig.json`-like files that do include source files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function main(foo: string) {
let x = foo;
console.log(x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
types
| (...data: any[]) => void |
| (foo: string) => void |
| Console |
| any |
| any[] |
| string |
| void |
jsonFiles
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import javascript

query predicate types(Type type) { any() }

query predicate jsonFiles(File file) { file.getExtension() = "json" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"include": [
"src"
],
"compilerOptions": {
"composite": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"include": [],
"files": [],
"references": [
{ "path": "./tsconfig.foo.json" },
],
}

0 comments on commit a0af4c9

Please sign in to comment.