Skip to content

Commit ef32a03

Browse files
committed
JS: Extract from methods from PathString into a non-abstract base class
The new class 'FilePath' has bindingset[this] so one just has to cast a string to that type and you can use its methods.
1 parent 17aa522 commit ef32a03

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

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

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

9898
/**
99-
* A string value that represents a (relative or absolute) file system path.
100-
*
101-
* Each path string is associated with one or more root folders relative to
102-
* which the path may be resolved. For instance, paths inside a module are
103-
* usually resolved relative to the module's folder, with a default
104-
* lookup path as the fallback.
99+
* A `string` with some additional member predicates for extracting parts of a file path.
105100
*/
106-
abstract class PathString extends string {
101+
class FilePath extends string {
107102
bindingset[this]
108-
PathString() { any() }
109-
110-
/** Gets a root folder relative to which this path can be resolved. */
111-
abstract Folder getARootFolder();
103+
FilePath() { any() }
112104

113105
/** Gets the `i`th component of this path. */
106+
bindingset[this]
114107
string getComponent(int i) { result = this.splitAt("/", i) }
115108

116109
/** Gets the number of components of this path. */
110+
bindingset[this]
117111
int getNumComponent() { result = count(int i | exists(this.getComponent(i))) }
118112

119113
/** Gets the base name of the folder or file this path refers to. */
114+
bindingset[this]
120115
string getBaseName() { result = this.regexpCapture(pathRegex(), 2) }
121116

122117
/**
123118
* Gets stem of the folder or file this path refers to, that is, the prefix of its base name
124119
* up to (but not including) the last dot character if there is one, or the entire
125120
* base name if there is not
126121
*/
122+
bindingset[this]
127123
string getStem() { result = this.regexpCapture(pathRegex(), 3) }
128124

129125
/** Gets the path of the parent folder of the folder or file this path refers to. */
126+
bindingset[this]
130127
string getDirName() { result = this.regexpCapture(pathRegex(), 1) }
131128

132129
/**
@@ -135,8 +132,26 @@ abstract class PathString extends string {
135132
*
136133
* Has no result if the base name does not contain a dot.
137134
*/
135+
bindingset[this]
138136
string getExtension() { result = this.regexpCapture(pathRegex(), 4) }
139137

138+
}
139+
140+
/**
141+
* A string value that represents a (relative or absolute) file system path.
142+
*
143+
* Each path string is associated with one or more root folders relative to
144+
* which the path may be resolved. For instance, paths inside a module are
145+
* usually resolved relative to the module's folder, with a default
146+
* lookup path as the fallback.
147+
*/
148+
abstract class PathString extends FilePath {
149+
bindingset[this]
150+
PathString() { any() }
151+
152+
/** Gets a root folder relative to which this path can be resolved. */
153+
abstract Folder getARootFolder();
154+
140155
/**
141156
* Gets the absolute path that the sub-path consisting of the first `n`
142157
* components of this path refers to when resolved relative to the

0 commit comments

Comments
 (0)