JS: Avoid materializing JSONValue.getFile()

This commit is contained in:
Asger F
2019-08-29 12:44:25 +01:00
parent 6c0f9be6df
commit 5874c14a9c
3 changed files with 15 additions and 7 deletions

View File

@@ -32,6 +32,14 @@ class JSONValue extends @json_value, Locatable {
predicate isTopLevel() { not exists(getParent()) }
override string toString() { json(this, _, _, _, result) }
/** Gets the JSON file containing this value. */
File getJsonFile() {
exists(Location loc |
json_locations(this, loc) and
result = loc.getFile()
)
}
}
/**

View File

@@ -8,7 +8,7 @@ private import NodeModuleResolutionImpl
/** A `package.json` configuration object. */
class PackageJSON extends JSONObject {
PackageJSON() {
getFile().getBaseName() = "package.json" and
getJsonFile().getBaseName() = "package.json" and
isTopLevel()
}
@@ -274,7 +274,7 @@ class NPMPackage extends @folder {
/** The `package.json` file of this package. */
PackageJSON pkg;
NPMPackage() { pkg.getFile().getParentContainer() = this }
NPMPackage() { pkg.getJsonFile().getParentContainer() = this }
/** Gets a textual representation of this package. */
string toString() { result = this.(Folder).toString() }

View File

@@ -11,7 +11,7 @@ module Babel {
*/
class Config extends JSONObject {
Config() {
isTopLevel() and getFile().getBaseName().matches(".babelrc%")
isTopLevel() and getJsonFile().getBaseName().matches(".babelrc%")
or
this = any(PackageJSON pkg).getPropValue("babel")
}
@@ -34,12 +34,12 @@ module Babel {
* Gets a file affected by this Babel configuration.
*/
Container getAContainerInScope() {
result = getFile().getParentContainer()
result = getJsonFile().getParentContainer()
or
result = getAContainerInScope().getAChildContainer() and
// File-relative .babelrc search stops at any package.json or .babelrc file.
not result.getAChildContainer() = any(PackageJSON pkg).getFile() and
not result.getAChildContainer() = any(Config pkg).getFile()
not result.getAChildContainer() = any(PackageJSON pkg).getJsonFile() and
not result.getAChildContainer() = any(Config pkg).getJsonFile()
}
/**
@@ -133,7 +133,7 @@ module Babel {
/**
* Gets the folder in which this configuration is located.
*/
Folder getFolder() { result = getFile().getParentContainer() }
Folder getFolder() { result = getJsonFile().getParentContainer() }
}
/**