Skip to content

Commit 1bf053e

Browse files
committed
JS: Move utility code into JSPaths
1 parent a215cfc commit 1bf053e

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

javascript/ql/lib/semmle/javascript/internal/paths/JSPaths.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
private import javascript
77
private import semmle.javascript.TSConfig
88

9+
/**
10+
* Holds if `path` is relative, in the sense that it should be resolved relative to its enclosing folder.
11+
*/
12+
bindingset[path]
13+
pragma[inline_late]
14+
predicate isRelativePath(string path) { path.regexpMatch("\\.\\.?(?:[/\\\\].*)?") }
15+
16+
/**
17+
* Gets the NPM package name from the beginning of the given import path, e.g.
18+
* gets `foo` from `foo/bar`, and `@example/foo` from `@example/foo/bar`.
19+
*/
20+
bindingset[path]
21+
string getPackagePrefixFromString(string path) {
22+
result = path.regexpFind("^(@[^/\\\\]+[/\\\\])?[^@./\\\\][^/\\\\]*", _, _)
23+
}
24+
925
/**
1026
* Gets a folder name that is a common source folder name.
1127
*/

javascript/ql/lib/semmle/javascript/internal/paths/PathExprResolver.qll

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,13 @@ module ResolveExpr<exprSig/1 shouldResolveExpr> {
8888
result.getAnAffectedFile() = expr.getFile()
8989
}
9090

91-
/**
92-
* Holds if `path` is relative, in the sense that it should be resolved relative to its enclosing folder.
93-
*/
94-
bindingset[path]
95-
pragma[inline_late]
96-
predicate isRelativePath(string path) { path.regexpMatch("\\.\\.?(?:[/\\\\].*)?") }
97-
9891
/**
9992
* Gets the NPM package name from the beginning of the given import path, e.g.
10093
* gets `foo` from `foo/bar`, and `@example/foo` from `@example/foo/bar`.
10194
*/
10295
pragma[nomagic]
10396
private string getPackagePrefixFromPathExpr(RelevantExpr expr) {
104-
result = expr.getValue().regexpFind("^(@[^/\\\\]+[/\\\\])?[^@./\\\\][^/\\\\]*", _, _)
97+
result = getPackagePrefixFromString(expr.getValue())
10598
}
10699

107100
/**

0 commit comments

Comments
 (0)