Skip to content

Commit 855c896

Browse files
committed
JavaScript: Use shared FileSystem library.
1 parent 7bc0c54 commit 855c896

File tree

1 file changed

+16
-156
lines changed

1 file changed

+16
-156
lines changed

javascript/ql/lib/semmle/javascript/Files.qll

Lines changed: 16 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -2,166 +2,34 @@
22

33
import javascript
44
private import NodeModuleResolutionImpl
5+
private import codeql.util.FileSystem
56

6-
/** A file or folder. */
7-
abstract class Container extends @container {
8-
/**
9-
* Gets the absolute, canonical path of this container, using forward slashes
10-
* as path separator.
11-
*
12-
* The path starts with a _root prefix_ followed by zero or more _path
13-
* segments_ separated by forward slashes.
14-
*
15-
* The root prefix is of one of the following forms:
16-
*
17-
* 1. A single forward slash `/` (Unix-style)
18-
* 2. An upper-case drive letter followed by a colon and a forward slash,
19-
* such as `C:/` (Windows-style)
20-
* 3. Two forward slashes, a computer name, and then another forward slash,
21-
* such as `//FileServer/` (UNC-style)
22-
*
23-
* Path segments are never empty (that is, absolute paths never contain two
24-
* contiguous slashes, except as part of a UNC-style root prefix). Also, path
25-
* segments never contain forward slashes, and no path segment is of the
26-
* form `.` (one dot) or `..` (two dots).
27-
*
28-
* Note that an absolute path never ends with a forward slash, except if it is
29-
* a bare root prefix, that is, the path has no path segments. A container
30-
* whose absolute path has no segments is always a `Folder`, not a `File`.
31-
*/
32-
abstract string getAbsolutePath();
33-
34-
/**
35-
* Gets a URL representing the location of this container.
36-
*
37-
* For more information see [Providing URLs](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/#providing-urls).
38-
*/
39-
abstract string getURL();
7+
private module FsInput implements InputSig {
8+
abstract class ContainerBase extends @container {
9+
abstract string getAbsolutePath();
4010

41-
/**
42-
* Gets the relative path of this file or folder from the root folder of the
43-
* analyzed source location. The relative path of the root folder itself is
44-
* the empty string.
45-
*
46-
* This has no result if the container is outside the source root, that is,
47-
* if the root folder is not a reflexive, transitive parent of this container.
48-
*/
49-
string getRelativePath() {
50-
exists(string absPath, string pref |
51-
absPath = this.getAbsolutePath() and sourceLocationPrefix(pref)
52-
|
53-
absPath = pref and result = ""
54-
or
55-
absPath = pref.regexpReplaceAll("/$", "") + "/" + result and
56-
not result.matches("/%")
57-
)
58-
}
11+
ContainerBase getParentContainer() { containerparent(result, this) }
5912

60-
/**
61-
* Gets the base name of this container including extension, that is, the last
62-
* segment of its absolute path, or the empty string if it has no segments.
63-
*
64-
* Here are some examples of absolute paths and the corresponding base names
65-
* (surrounded with quotes to avoid ambiguity):
66-
*
67-
* <table border="1">
68-
* <tr><th>Absolute path</th><th>Base name</th></tr>
69-
* <tr><td>"/tmp/tst.js"</td><td>"tst.js"</td></tr>
70-
* <tr><td>"C:/Program Files (x86)"</td><td>"Program Files (x86)"</td></tr>
71-
* <tr><td>"/"</td><td>""</td></tr>
72-
* <tr><td>"C:/"</td><td>""</td></tr>
73-
* <tr><td>"D:/"</td><td>""</td></tr>
74-
* <tr><td>"//FileServer/"</td><td>""</td></tr>
75-
* </table>
76-
*/
77-
string getBaseName() {
78-
result = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(\\.([^.]*))?)", 1)
79-
}
80-
81-
/**
82-
* Gets the extension of this container, that is, the suffix of its base name
83-
* after the last dot character, if any.
84-
*
85-
* In particular,
86-
*
87-
* - if the name does not include a dot, there is no extension, so this
88-
* predicate has no result;
89-
* - if the name ends in a dot, the extension is the empty string;
90-
* - if the name contains multiple dots, the extension follows the last dot.
91-
*
92-
* Here are some examples of absolute paths and the corresponding extensions
93-
* (surrounded with quotes to avoid ambiguity):
94-
*
95-
* <table border="1">
96-
* <tr><th>Absolute path</th><th>Extension</th></tr>
97-
* <tr><td>"/tmp/tst.js"</td><td>"js"</td></tr>
98-
* <tr><td>"/tmp/.classpath"</td><td>"classpath"</td></tr>
99-
* <tr><td>"/bin/bash"</td><td>not defined</td></tr>
100-
* <tr><td>"/tmp/tst2."</td><td>""</td></tr>
101-
* <tr><td>"/tmp/x.tar.gz"</td><td>"gz"</td></tr>
102-
* </table>
103-
*/
104-
string getExtension() {
105-
result = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(\\.([^.]*))?)", 4)
13+
string toString() { result = this.getAbsolutePath() }
10614
}
10715

108-
/**
109-
* Gets the stem of this container, that is, the prefix of its base name up to
110-
* (but not including) the last dot character if there is one, or the entire
111-
* base name if there is not.
112-
*
113-
* Here are some examples of absolute paths and the corresponding stems
114-
* (surrounded with quotes to avoid ambiguity):
115-
*
116-
* <table border="1">
117-
* <tr><th>Absolute path</th><th>Stem</th></tr>
118-
* <tr><td>"/tmp/tst.js"</td><td>"tst"</td></tr>
119-
* <tr><td>"/tmp/.classpath"</td><td>""</td></tr>
120-
* <tr><td>"/bin/bash"</td><td>"bash"</td></tr>
121-
* <tr><td>"/tmp/tst2."</td><td>"tst2"</td></tr>
122-
* <tr><td>"/tmp/x.tar.gz"</td><td>"x.tar"</td></tr>
123-
* </table>
124-
*/
125-
string getStem() {
126-
result = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(\\.([^.]*))?)", 2)
16+
class FolderBase extends ContainerBase, @folder {
17+
override string getAbsolutePath() { folders(this, result) }
12718
}
12819

129-
/** Gets the parent container of this file or folder, if any. */
130-
Container getParentContainer() { containerparent(result, this) }
131-
132-
/** Gets a file or sub-folder in this container. */
133-
Container getAChildContainer() { this = result.getParentContainer() }
134-
135-
/** Gets a file in this container. */
136-
File getAFile() { result = this.getAChildContainer() }
137-
138-
/** Gets the file in this container that has the given `baseName`, if any. */
139-
File getFile(string baseName) {
140-
result = this.getAFile() and
141-
result.getBaseName() = baseName
20+
class FileBase extends ContainerBase, @file {
21+
override string getAbsolutePath() { files(this, result) }
14222
}
14323

144-
/** Gets a sub-folder in this container. */
145-
Folder getAFolder() { result = this.getAChildContainer() }
24+
predicate hasSourceLocationPrefix = sourceLocationPrefix/1;
25+
}
14626

147-
/** Gets the sub-folder in this container that has the given `baseName`, if any. */
148-
Folder getFolder(string baseName) {
149-
result = this.getAFolder() and
150-
result.getBaseName() = baseName
151-
}
27+
private module Impl = Make<FsInput>;
15228

153-
/**
154-
* Gets a textual representation of the path of this container.
155-
*
156-
* This is the absolute path of the container.
157-
*/
158-
string toString() { result = this.getAbsolutePath() }
159-
}
29+
class Container = Impl::Container;
16030

16131
/** A folder. */
162-
class Folder extends Container, @folder {
163-
override string getAbsolutePath() { folders(this, result) }
164-
32+
class Folder extends Container, Impl::Folder {
16533
/** Gets the file or subfolder in this folder that has the given `name`, if any. */
16634
Container getChildContainer(string name) {
16735
result = this.getAChildContainer() and
@@ -206,22 +74,17 @@ class Folder extends Container, @folder {
20674

20775
/** Gets a subfolder contained in this folder. */
20876
Folder getASubFolder() { result = this.getAChildContainer() }
209-
210-
/** Gets the URL of this folder. */
211-
override string getURL() { result = "folder://" + this.getAbsolutePath() }
21277
}
21378

21479
/** A file. */
215-
class File extends Container, @file {
80+
class File extends Container, Impl::File {
21681
/**
21782
* Gets the location of this file.
21883
*
21984
* Note that files have special locations starting and ending at line zero, column zero.
22085
*/
22186
Location getLocation() { hasLocation(this, result) }
22287

223-
override string getAbsolutePath() { files(this, result) }
224-
22588
/** Gets the number of lines in this file. */
22689
int getNumberOfLines() { result = sum(int loc | numlines(this, loc, _, _) | loc) }
22790

@@ -234,9 +97,6 @@ class File extends Container, @file {
23497
/** Gets a toplevel piece of JavaScript code in this file. */
23598
TopLevel getATopLevel() { result.getFile() = this }
23699

237-
/** Gets the URL of this file. */
238-
override string getURL() { result = "file://" + this.getAbsolutePath() + ":0:0:0:0" }
239-
240100
/**
241101
* Holds if line number `lineno` of this file is indented to depth `d`
242102
* using character `c`.

0 commit comments

Comments
 (0)