move resolveMainPath into a separate helper predicate

This commit is contained in:
erik-krogh
2022-09-09 14:26:07 +02:00
parent aee72357b8
commit 5010f89683

View File

@@ -90,21 +90,18 @@ bindingset[name]
private string getStem(string name) { result = name.regexpCapture("(.+?)(?:\\.([^.]+))?", 1) }
/**
* Gets the main module described by `pkg` with the given `priority`.
* Gets a file that a main module from `pkg` exported as `mainPath` with the given `priority`.
* `mainPath` is "." if it's the main module of the package.
*/
File resolveMainModule(PackageJson pkg, int priority) {
exists(PathExpr main, int subPriority, string mainPath |
main = MainModulePath::of(pkg, mainPath) and
if mainPath = "." then subPriority = priority else priority = subPriority + 1000
|
result = main.resolve() and subPriority = 0
private File resolveMainPath(PackageJson pkg, string mainPath, int priority) {
exists(PathExpr main | main = MainModulePath::of(pkg, mainPath) |
result = main.resolve() and priority = 0
or
result = tryExtensions(main.resolve(), "index", subPriority)
result = tryExtensions(main.resolve(), "index", priority)
or
not main.resolve() instanceof File and
exists(int n | n = main.getNumComponent() |
result =
tryExtensions(main.resolveUpTo(n - 1), getStem(main.getComponent(n - 1)), subPriority)
result = tryExtensions(main.resolveUpTo(n - 1), getStem(main.getComponent(n - 1)), priority)
)
or
// assuming the files get moved from one dir to another during compilation:
@@ -114,9 +111,19 @@ File resolveMainModule(PackageJson pkg, int priority) {
// is in one folder below the package.json, and has the right basename
result =
tryExtensions(subFolder, getStem(main.getComponent(main.getNumComponent() - 1)),
subPriority - 999)
priority - 999) // very high priority, to make sure everything else is tried first
)
)
}
/**
* Gets the main module described by `pkg` with the given `priority`.
*/
File resolveMainModule(PackageJson pkg, int priority) {
exists(int subPriority, string mainPath |
result = resolveMainPath(pkg, mainPath, subPriority) and
if mainPath = "." then subPriority = priority else priority = subPriority + 1000
)
or
exists(Folder folder, Folder child |
child = folder or