Merge pull request #19391 from asgerf/js/typescript-path-resolution

JS: Overhaul import resolution
This commit is contained in:
Asger F
2025-05-13 15:46:38 +02:00
committed by GitHub
159 changed files with 1855 additions and 471 deletions

View File

@@ -152,4 +152,4 @@ where cycleAlert(mod, import_, importedModule, access)
select access,
access.getName() + " is uninitialized if $@ is loaded first in the cyclic import:" + " " +
repr(import_) + " => " + min(pathToModule(importedModule, mod, _)) + " => " + repr(import_) +
".", import_.getImportedPath(), importedModule.getName()
".", import_.getImportedPathExpr(), importedModule.getName()

View File

@@ -24,7 +24,7 @@ PackageJson getClosestPackageJson(Folder f) {
from Require r, string path, string mod
where
path = r.getImportedPath().getValue() and
path = r.getImportedPathString() and
// the imported module is the initial segment of the path, up to
// `/` or the end of the string, whichever comes first; we exclude
// local paths starting with `.` or `/`, since they might refer to files

View File

@@ -23,7 +23,17 @@ predicate declaresDependency(NpmPackage pkg, string name, JsonValue dep) {
/**
* Gets a path expression in a module belonging to `pkg`.
*/
PathExpr getAPathExpr(NpmPackage pkg) { result.getEnclosingModule() = pkg.getAModule() }
Expr getAPathExpr(NpmPackage pkg) {
exists(Import imprt |
result = imprt.getImportedPathExpr() and
pkg.getAModule() = imprt.getEnclosingModule()
)
or
exists(ReExportDeclaration decl |
result = decl.getImportedPath() and
pkg.getAModule() = decl.getEnclosingModule()
)
}
/**
* Gets a URL-valued attribute in a module or HTML file belonging to `pkg`.
@@ -56,9 +66,8 @@ predicate usesDependency(NpmPackage pkg, string name) {
(
// there is a path expression (e.g., in a `require` or `import`) that
// references `pkg`
exists(PathExpr path | path = getAPathExpr(pkg) |
// check whether the path is `name` or starts with `name/`, ignoring a prefix that ends with '!' (example: "scriptloader!moment")
path.getValue().regexpMatch("(.*!)?\\Q" + name + "\\E(/.*)?")
exists(Expr path | path = getAPathExpr(pkg) |
path.getStringValue().(FilePath).getPackagePrefix() = name
)
or
// there is an HTML URL attribute that may reference `pkg`