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) } 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) { private File resolveMainPath(PackageJson pkg, string mainPath, int priority) {
exists(PathExpr main, int subPriority, string mainPath | exists(PathExpr main | main = MainModulePath::of(pkg, mainPath) |
main = MainModulePath::of(pkg, mainPath) and result = main.resolve() and priority = 0
if mainPath = "." then subPriority = priority else priority = subPriority + 1000
|
result = main.resolve() and subPriority = 0
or or
result = tryExtensions(main.resolve(), "index", subPriority) result = tryExtensions(main.resolve(), "index", priority)
or or
not main.resolve() instanceof File and not main.resolve() instanceof File and
exists(int n | n = main.getNumComponent() | exists(int n | n = main.getNumComponent() |
result = result = tryExtensions(main.resolveUpTo(n - 1), getStem(main.getComponent(n - 1)), priority)
tryExtensions(main.resolveUpTo(n - 1), getStem(main.getComponent(n - 1)), subPriority)
) )
or or
// assuming the files get moved from one dir to another during compilation: // 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 // is in one folder below the package.json, and has the right basename
result = result =
tryExtensions(subFolder, getStem(main.getComponent(main.getNumComponent() - 1)), 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 or
exists(Folder folder, Folder child | exists(Folder folder, Folder child |
child = folder or child = folder or