mirror of
https://github.com/github/codeql.git
synced 2026-04-29 10:45:15 +02:00
JS: Add PackageJson.getTypingsModule
This commit is contained in:
@@ -180,6 +180,35 @@ class PackageJson extends JsonObject {
|
||||
Module getMainModule() {
|
||||
result = min(Module m, int prio | m.getFile() = resolveMainModule(this, prio) | m order by prio)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the `types` or `typings` field of this package.
|
||||
*/
|
||||
string getTypings() { result = this.getPropStringValue(["types", "typings"]) }
|
||||
|
||||
/**
|
||||
* Gets the file containing the typings of this package, which can either be from the `types` or
|
||||
* `typings` field, or derived from the `main` or `module` fields.
|
||||
*/
|
||||
File getTypingsFile() {
|
||||
result =
|
||||
TypingsModulePathString::of(this).resolve(this.getFile().getParentContainer()).getContainer()
|
||||
or
|
||||
not exists(TypingsModulePathString::of(this)) and
|
||||
exists(File mainFile |
|
||||
mainFile = this.getMainModule().getFile() and
|
||||
result =
|
||||
mainFile
|
||||
.getParentContainer()
|
||||
.getFile(mainFile.getStem().regexpReplaceAll("\\.d$", "") + ".d.ts")
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the module containing the typings of this package, which can either be from the `types` or
|
||||
* `typings` field, or derived from the `main` or `module` fields.
|
||||
*/
|
||||
Module getTypingsModule() { result.getFile() = this.getTypingsFile() }
|
||||
}
|
||||
|
||||
/** DEPRECATED: Alias for PackageJson */
|
||||
|
||||
@@ -198,3 +198,29 @@ private class FilesPath extends PathExpr, @json_string {
|
||||
private module FilesPath {
|
||||
FilesPath of(PackageJson pkg) { result.getPackageJson() = pkg }
|
||||
}
|
||||
|
||||
/**
|
||||
* A JSON string in a `package.json` file specifying the path of the
|
||||
* TypeScript typings entry point.
|
||||
*/
|
||||
class TypingsModulePathString extends PathString {
|
||||
PackageJson pkg;
|
||||
|
||||
TypingsModulePathString() {
|
||||
this = pkg.getTypings()
|
||||
or
|
||||
not exists(pkg.getTypings()) and
|
||||
this = pkg.getMain().regexpReplaceAll("\\.[mc]?js$", ".d.ts")
|
||||
}
|
||||
|
||||
/** Gets the `package.json` file containing this path. */
|
||||
PackageJson getPackageJson() { result = pkg }
|
||||
|
||||
override Folder getARootFolder() { result = pkg.getFile().getParentContainer() }
|
||||
}
|
||||
|
||||
/** Companion module to the `TypingsModulePathString` class. */
|
||||
module TypingsModulePathString {
|
||||
/** Get the typings path for the given `package.json` file. */
|
||||
TypingsModulePathString of(PackageJson pkg) { result.getPackageJson() = pkg }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user