Skip to content

Commit 5010f89

Browse files
committed
move resolveMainPath into a separate helper predicate
1 parent aee7235 commit 5010f89

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

javascript/ql/lib/semmle/javascript/NodeModuleResolutionImpl.qll

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,18 @@ bindingset[name]
9090
private string getStem(string name) { result = name.regexpCapture("(.+?)(?:\\.([^.]+))?", 1) }
9191

9292
/**
93-
* Gets the main module described by `pkg` with the given `priority`.
93+
* Gets a file that a main module from `pkg` exported as `mainPath` with the given `priority`.
94+
* `mainPath` is "." if it's the main module of the package.
9495
*/
95-
File resolveMainModule(PackageJson pkg, int priority) {
96-
exists(PathExpr main, int subPriority, string mainPath |
97-
main = MainModulePath::of(pkg, mainPath) and
98-
if mainPath = "." then subPriority = priority else priority = subPriority + 1000
99-
|
100-
result = main.resolve() and subPriority = 0
96+
private File resolveMainPath(PackageJson pkg, string mainPath, int priority) {
97+
exists(PathExpr main | main = MainModulePath::of(pkg, mainPath) |
98+
result = main.resolve() and priority = 0
10199
or
102-
result = tryExtensions(main.resolve(), "index", subPriority)
100+
result = tryExtensions(main.resolve(), "index", priority)
103101
or
104102
not main.resolve() instanceof File and
105103
exists(int n | n = main.getNumComponent() |
106-
result =
107-
tryExtensions(main.resolveUpTo(n - 1), getStem(main.getComponent(n - 1)), subPriority)
104+
result = tryExtensions(main.resolveUpTo(n - 1), getStem(main.getComponent(n - 1)), priority)
108105
)
109106
or
110107
// assuming the files get moved from one dir to another during compilation:
@@ -114,9 +111,19 @@ File resolveMainModule(PackageJson pkg, int priority) {
114111
// is in one folder below the package.json, and has the right basename
115112
result =
116113
tryExtensions(subFolder, getStem(main.getComponent(main.getNumComponent() - 1)),
117-
subPriority - 999)
114+
priority - 999) // very high priority, to make sure everything else is tried first
118115
)
119116
)
117+
}
118+
119+
/**
120+
* Gets the main module described by `pkg` with the given `priority`.
121+
*/
122+
File resolveMainModule(PackageJson pkg, int priority) {
123+
exists(int subPriority, string mainPath |
124+
result = resolveMainPath(pkg, mainPath, subPriority) and
125+
if mainPath = "." then subPriority = priority else priority = subPriority + 1000
126+
)
120127
or
121128
exists(Folder folder, Folder child |
122129
child = folder or

0 commit comments

Comments
 (0)