Skip to content

Commit 1efeca4

Browse files
committed
JS: Add two more helpers to FilePath class
1 parent 20e9eeb commit 1efeca4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,31 @@ class FilePath extends string {
135135
bindingset[this]
136136
string getExtension() { result = this.regexpCapture(pathRegex(), 4) }
137137

138+
/**
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 `..\`.
144+
*/
145+
bindingset[this]
146+
pragma[inline_late]
147+
predicate isDotRelativePath() { this.regexpMatch("\\.\\.?(?:[/\\\\].*)?") }
148+
149+
/**
150+
* Gets the NPM package name from the beginning of the given import path.
151+
*
152+
* Has no result for paths starting with a `.` or `/`
153+
*
154+
* For example:
155+
* - `foo/bar` maps to `foo`
156+
* - `@example/foo/bar` maps to `@example/foo`
157+
* - `./foo` maps to nothing.
158+
*/
159+
bindingset[this]
160+
string getPackagePrefix() {
161+
result = this.regexpFind("^(@[^/\\\\]+[/\\\\])?[^@./\\\\][^/\\\\]*", _, _)
162+
}
138163
}
139164

140165
/**

0 commit comments

Comments
 (0)