From bf80f0798bcfd35a32dc87bd260825e5c2aab063 Mon Sep 17 00:00:00 2001 From: Asger F Date: Wed, 29 Jan 2025 10:53:18 +0100 Subject: [PATCH 1/5] JS: Treat more file patterns as as tsconfig.json-like --- javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java b/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java index ca7fa6c06c0e..0d4dba7b6dcc 100644 --- a/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java +++ b/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java @@ -1140,7 +1140,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) // extract TypeScript projects from 'tsconfig.json' if (typeScriptMode == TypeScriptMode.FULL - && file.getFileName().endsWith("tsconfig.json") + && (file.getFileName().toString().contains("tsconfig.") && file.getFileName().toString().endsWith(".json")) && !excludes.contains(file) && isFileIncluded(file)) { tsconfigFiles.add(file); From 8182190120c7453d603e8363b8bc11ed36e01451 Mon Sep 17 00:00:00 2001 From: Asger F Date: Wed, 29 Jan 2025 10:53:26 +0100 Subject: [PATCH 2/5] JS: Remove trailing whitespace --- .../extractor/src/com/semmle/js/extractor/AutoBuild.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java b/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java index 0d4dba7b6dcc..808837e55e4d 100644 --- a/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java +++ b/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java @@ -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"); @@ -895,7 +895,7 @@ protected DependencyInstallationResult preparePackagesAndDependencies(Set // 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) { From d66d1a79d6fb98e04e2d79ed6d7d908279bb6593 Mon Sep 17 00:00:00 2001 From: Asger F Date: Wed, 29 Jan 2025 11:14:10 +0100 Subject: [PATCH 3/5] JS: Also update legacy entry point used by qltest --- .../extractor/src/com/semmle/js/extractor/AutoBuild.java | 6 +++++- javascript/extractor/src/com/semmle/js/extractor/Main.java | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java b/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java index 808837e55e4d..7247489ee693 100644 --- a/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java +++ b/javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java @@ -1108,6 +1108,10 @@ private boolean hasTypeScriptFiles(Set filesToExtract) { return false; } + public static boolean treatAsTSConfig(String basename) { + return basename.contains("tsconfig.") && basename.endsWith(".json"); + } + private void findFilesToExtract( FileExtractor extractor, final Set filesToExtract, final List tsconfigFiles) throws IOException { @@ -1140,7 +1144,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) // extract TypeScript projects from 'tsconfig.json' if (typeScriptMode == TypeScriptMode.FULL - && (file.getFileName().toString().contains("tsconfig.") && file.getFileName().toString().endsWith(".json")) + && treatAsTSConfig(file.getFileName().toString()) && !excludes.contains(file) && isFileIncluded(file)) { tsconfigFiles.add(file); diff --git a/javascript/extractor/src/com/semmle/js/extractor/Main.java b/javascript/extractor/src/com/semmle/js/extractor/Main.java index 57198709460e..4fbfa1518188 100644 --- a/javascript/extractor/src/com/semmle/js/extractor/Main.java +++ b/javascript/extractor/src/com/semmle/js/extractor/Main.java @@ -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); } From 6d04425790f32fc6154a46badf9176a89068d1c7 Mon Sep 17 00:00:00 2001 From: Asger F Date: Wed, 29 Jan 2025 11:14:21 +0100 Subject: [PATCH 4/5] JS: Add test --- .../TypeScript/TSConfigReferences/src/main.ts | 4 ++++ .../TypeScript/TSConfigReferences/test.expected | 9 +++++++++ .../library-tests/TypeScript/TSConfigReferences/test.ql | 5 +++++ .../TypeScript/TSConfigReferences/tsconfig.foo.json | 9 +++++++++ .../TypeScript/TSConfigReferences/tsconfig.json | 7 +++++++ 5 files changed, 34 insertions(+) create mode 100644 javascript/ql/test/library-tests/TypeScript/TSConfigReferences/src/main.ts create mode 100644 javascript/ql/test/library-tests/TypeScript/TSConfigReferences/test.expected create mode 100644 javascript/ql/test/library-tests/TypeScript/TSConfigReferences/test.ql create mode 100644 javascript/ql/test/library-tests/TypeScript/TSConfigReferences/tsconfig.foo.json create mode 100644 javascript/ql/test/library-tests/TypeScript/TSConfigReferences/tsconfig.json diff --git a/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/src/main.ts b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/src/main.ts new file mode 100644 index 000000000000..403c78e0d0d5 --- /dev/null +++ b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/src/main.ts @@ -0,0 +1,4 @@ +export function main(foo: string) { + let x = foo; + console.log(x); +} diff --git a/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/test.expected b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/test.expected new file mode 100644 index 000000000000..18dbed450ba1 --- /dev/null +++ b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/test.expected @@ -0,0 +1,9 @@ +types +| (...data: any[]) => void | +| (foo: string) => void | +| Console | +| any | +| any[] | +| string | +| void | +jsonFiles diff --git a/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/test.ql b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/test.ql new file mode 100644 index 000000000000..25ace33c8a38 --- /dev/null +++ b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/test.ql @@ -0,0 +1,5 @@ +import javascript + +query predicate types(Type type) { any() } + +query predicate jsonFiles(File file) { file.getExtension() = "json" } diff --git a/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/tsconfig.foo.json b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/tsconfig.foo.json new file mode 100644 index 000000000000..658aef62ac0e --- /dev/null +++ b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/tsconfig.foo.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "include": [ + "src" + ], + "compilerOptions": { + "composite": true + } +} diff --git a/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/tsconfig.json b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/tsconfig.json new file mode 100644 index 000000000000..b0f85a63f5f2 --- /dev/null +++ b/javascript/ql/test/library-tests/TypeScript/TSConfigReferences/tsconfig.json @@ -0,0 +1,7 @@ +{ + "include": [], + "files": [], + "references": [ + { "path": "./tsconfig.foo.json" }, + ], +} From 2e65fe9597bbbdce13cf1d6df7d5bf171ecca16a Mon Sep 17 00:00:00 2001 From: Asger F Date: Thu, 30 Jan 2025 20:46:30 +0100 Subject: [PATCH 5/5] JS: Change note --- .../src/change-notes/2025-01-30-typescript-tsconfig-names.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 javascript/ql/src/change-notes/2025-01-30-typescript-tsconfig-names.md diff --git a/javascript/ql/src/change-notes/2025-01-30-typescript-tsconfig-names.md b/javascript/ql/src/change-notes/2025-01-30-typescript-tsconfig-names.md new file mode 100644 index 000000000000..3f7c572d0742 --- /dev/null +++ b/javascript/ql/src/change-notes/2025-01-30-typescript-tsconfig-names.md @@ -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.