JS: Add some helpers

This commit is contained in:
Asger F
2025-04-24 09:04:18 +02:00
parent 565cb434fc
commit 17aa5220a6
2 changed files with 23 additions and 1 deletions

View File

@@ -75,6 +75,19 @@ class Folder extends Container, Impl::Folder {
)
}
/**
* Gets an implementation file and/or a typings file from this folder that has the given `stem`.
* This could be a single `.ts` file or a pair of `.js` and `.d.ts` files.
*/
File getJavaScriptFileOrTypings(string stem) {
exists(File jsFile | jsFile = this.getJavaScriptFile(stem) |
result = jsFile
or
not jsFile.getFileType().isTypeScript() and
result = this.getFile(stem + ".d.ts")
)
}
/** Gets a subfolder contained in this folder. */
Folder getASubFolder() { result = this.getAChildContainer() }
}

View File

@@ -12,12 +12,21 @@ class PackageJson extends JsonObject {
this.isTopLevel()
}
/** Gets the folder containing this `package.json` file. */
Folder getFolder() { result = this.getJsonFile().getParentContainer() }
/**
* Gets the name of this package as it appears in the `name` field.
*/
pragma[nomagic]
string getDeclaredPackageName() { result = this.getPropStringValue("name") }
/**
* Gets the name of this package.
* 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`.
*/
string getPackageName() {
result = this.getPropStringValue("name")
result = this.getDeclaredPackageName()
or
exists(
PackageJson parentPkg, Container currentDir, Container parentDir, string parentPkgName,