Skip to content

Commit 17aa522

Browse files
committed
JS: Add some helpers
1 parent 565cb43 commit 17aa522

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ class Folder extends Container, Impl::Folder {
7575
)
7676
}
7777

78+
/**
79+
* Gets an implementation file and/or a typings file from this folder that has the given `stem`.
80+
* This could be a single `.ts` file or a pair of `.js` and `.d.ts` files.
81+
*/
82+
File getJavaScriptFileOrTypings(string stem) {
83+
exists(File jsFile | jsFile = this.getJavaScriptFile(stem) |
84+
result = jsFile
85+
or
86+
not jsFile.getFileType().isTypeScript() and
87+
result = this.getFile(stem + ".d.ts")
88+
)
89+
}
90+
7891
/** Gets a subfolder contained in this folder. */
7992
Folder getASubFolder() { result = this.getAChildContainer() }
8093
}

javascript/ql/lib/semmle/javascript/NPM.qll

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@ class PackageJson extends JsonObject {
1212
this.isTopLevel()
1313
}
1414

15+
/** Gets the folder containing this `package.json` file. */
16+
Folder getFolder() { result = this.getJsonFile().getParentContainer() }
17+
18+
/**
19+
* Gets the name of this package as it appears in the `name` field.
20+
*/
21+
pragma[nomagic]
22+
string getDeclaredPackageName() { result = this.getPropStringValue("name") }
23+
1524
/**
1625
* Gets the name of this package.
1726
* If the package is located under the package `pkg1` and its relative path is `foo/bar`, then the resulting package name will be `pkg1/foo/bar`.
1827
*/
1928
string getPackageName() {
20-
result = this.getPropStringValue("name")
29+
result = this.getDeclaredPackageName()
2130
or
2231
exists(
2332
PackageJson parentPkg, Container currentDir, Container parentDir, string parentPkgName,

0 commit comments

Comments
 (0)