Skip to content

Commit 486937f

Browse files
committed
Rename StringAsPath->FilePath, isRelativePath->isDotRelativePath
1 parent ca877c9 commit 486937f

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

javascript/ql/lib/semmle/javascript/AMD.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ private class AmdDependencyImport extends Import {
282282
* adding well-known JavaScript file extensions like `.js`.
283283
*/
284284
private File guessTarget() {
285-
exists(StringAsPath imported, string abspath, string dirname, string basename |
285+
exists(FilePath imported, string abspath, string dirname, string basename |
286286
this.targetCandidate(result, abspath, imported, dirname, basename)
287287
|
288288
abspath.regexpMatch(".*/\\Q" + imported + "\\E")
@@ -304,7 +304,7 @@ private class AmdDependencyImport extends Import {
304304
* `dirname` and `basename` to the dirname and basename (respectively) of `imported`.
305305
*/
306306
private predicate targetCandidate(
307-
File f, string abspath, StringAsPath imported, string dirname, string basename
307+
File f, string abspath, FilePath imported, string dirname, string basename
308308
) {
309309
imported = this.getImportedPathString() and
310310
f.getStem() = imported.getStem() and

javascript/ql/lib/semmle/javascript/Paths.qll

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ deprecated private class ConsPath extends Path, TConsPath {
9696
private string pathRegex() { result = "(.*)(?:/|^)(([^/]*?)(\\.([^.]*))?)" }
9797

9898
/**
99-
* Providers helper predicates for extracting parts of a path string.
99+
* A `string` with some additional member predicates for extracting parts of a path string.
100100
*/
101-
class StringAsPath extends string {
101+
class FilePath extends string {
102102
bindingset[this]
103-
StringAsPath() { any() }
103+
FilePath() { any() }
104104

105105
/** Gets the `i`th component of this path. */
106106
bindingset[this]
@@ -136,11 +136,15 @@ class StringAsPath extends string {
136136
string getExtension() { result = this.regexpCapture(pathRegex(), 4) }
137137

138138
/**
139-
* Holds if this path is relative, in the sense that it should be resolved relative to its enclosing folder.
139+
* Holds if this is a relative path starting with an explicit `./` or similar syntax meaning it
140+
* must be resolved relative to its enclosing folder.
141+
*
142+
* Specifically this holds when the string is `.` or `..`, or starts with `./` or `../` or
143+
* `.\` or `..\`.
140144
*/
141145
bindingset[this]
142146
pragma[inline_late]
143-
predicate isRelativePath() { this.regexpMatch("\\.\\.?(?:[/\\\\].*)?") }
147+
predicate isDotRelativePath() { this.regexpMatch("\\.\\.?(?:[/\\\\].*)?") }
144148

145149
/**
146150
* Gets the NPM package name from the beginning of the given import path, e.g.
@@ -160,7 +164,7 @@ class StringAsPath extends string {
160164
* usually resolved relative to the module's folder, with a default
161165
* lookup path as the fallback.
162166
*/
163-
abstract deprecated class PathString extends StringAsPath {
167+
abstract deprecated class PathString extends FilePath {
164168
bindingset[this]
165169
PathString() { any() }
166170

javascript/ql/lib/semmle/javascript/dependencies/Dependencies.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ class ExternalNpmDependency extends NpmDependency {
152152
}
153153

154154
pragma[nomagic]
155-
private string getImportedPackage(Import i) {
156-
result = i.getImportedPathString().(StringAsPath).getPackagePrefix()
155+
private string getPackagePrefix(Import i) {
156+
result = i.getImportedPathString().(FilePath).getPackagePrefix()
157157
}
158158

159159
/**
@@ -164,7 +164,7 @@ private int importsDependency(Import i, NpmPackage pkg, NpmDependency dep) {
164164
exists(string name |
165165
dep = pkg.getPackageJson().getADependenciesObject(_).getPropValue(name) and
166166
not exists(i.getImportedModule()) and
167-
name = getImportedPackage(i) and
167+
name = getPackagePrefix(i) and
168168
i.getEnclosingModule() = pkg.getAModule() and
169169
result = distance(pkg, i.getFile())
170170
)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module ResolveExpr<exprSig/1 shouldResolveExpr> {
9494
*/
9595
pragma[nomagic]
9696
private string getPackagePrefixFromPathExpr(RelevantExpr expr) {
97-
result = expr.getValue().(StringAsPath).getPackagePrefix()
97+
result = expr.getValue().(FilePath).getPackagePrefix()
9898
}
9999

100100
/**
@@ -141,10 +141,10 @@ module ResolveExpr<exprSig/1 shouldResolveExpr> {
141141
}
142142

143143
pragma[noopt]
144-
private predicate relativePathExpr(RelevantExpr expr, Container base, StringAsPath path) {
144+
private predicate relativePathExpr(RelevantExpr expr, Container base, FilePath path) {
145145
expr instanceof RelevantExpr and
146146
path = expr.getValue() and
147-
path.isRelativePath() and
147+
path.isDotRelativePath() and
148148
exists(File file |
149149
file = expr.getFile() and
150150
base = file.getParentContainer()
@@ -164,15 +164,15 @@ module ResolveExpr<exprSig/1 shouldResolveExpr> {
164164
* Holds if `expr` should be resolved as `path` relative to `base`.
165165
*/
166166
pragma[nomagic]
167-
private predicate shouldResolve(RelevantExpr expr, Container base, StringAsPath path) {
167+
private predicate shouldResolve(RelevantExpr expr, Container base, FilePath path) {
168168
// Relative paths are resolved from their enclosing folder
169169
relativePathExpr(expr, base, path)
170170
or
171171
resolveViaPathMapping(expr, base, path)
172172
or
173173
// Resolve from baseUrl of relevant tsconfig.json file
174174
path = expr.getValue() and
175-
not path.isRelativePath() and
175+
not path.isDotRelativePath() and
176176
getAPathMappingFromPathExpr(expr).hasBaseUrl(base)
177177
or
178178
// If the path starts with the name of a package, but did not match any path mapping,

0 commit comments

Comments
 (0)