|
5 | 5 | import javascript |
6 | 6 | import advanced_security.javascript.frameworks.cap.CDS |
7 | 7 |
|
8 | | -abstract class CdlObject extends JsonObject { } |
| 8 | +abstract class CdlObject extends JsonObject { |
| 9 | + predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { |
| 10 | + // If the cds.json file has a $location property, then use that, |
| 11 | + // otherwise fall back to the cds.json file itself |
| 12 | + if exists(this.getPropValue("$location")) |
| 13 | + then |
| 14 | + exists(Location loc, JsonValue locValue | |
| 15 | + loc = this.getLocation() and |
| 16 | + locValue = this.getPropValue("$location") and |
| 17 | + path = |
| 18 | + any(File f | |
| 19 | + f.getAbsolutePath() |
| 20 | + .matches("%" + locValue.getPropValue("file").getStringValue() + ".json") |
| 21 | + ).getAbsolutePath().regexpReplaceAll("\\.json$", "") and |
| 22 | + sl = locValue.getPropValue("line").getIntValue() and |
| 23 | + sc = locValue.getPropValue("col").getIntValue() and |
| 24 | + if exists(getObjectLocationName()) |
| 25 | + then |
| 26 | + // Currently $locations does not provide an end location. However, we can |
| 27 | + // automatically deduce the end location from the length of the name. |
| 28 | + el = sl and |
| 29 | + ec = sc + getObjectLocationName().length() - 1 |
| 30 | + else ( |
| 31 | + // We don't know where this entity ends, so mark the whole line |
| 32 | + el = sl + 1 and |
| 33 | + ec = 1 |
| 34 | + ) |
| 35 | + ) |
| 36 | + else super.getLocation().hasLocationInfo(path, sl, sc, el, ec) |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * The name of the object that should be highlighted as the location. |
| 41 | + * |
| 42 | + * This is used to deduce the length of the location. |
| 43 | + */ |
| 44 | + string getObjectLocationName() { none() } |
| 45 | +} |
9 | 46 |
|
10 | 47 | private newtype CdlKind = |
11 | 48 | CdlServiceKind(string value) { value = "service" } or |
@@ -34,28 +71,7 @@ abstract class CdlElement extends CdlObject { |
34 | 71 |
|
35 | 72 | CdlElement() { exists(CdlDefinitions definitions | this = definitions.getElement(name)) } |
36 | 73 |
|
37 | | - predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { |
38 | | - // If the cds.json file has a $location property, then use that, |
39 | | - // otherwise fall back to the cds.json file itself |
40 | | - if exists(this.getPropValue("$location")) |
41 | | - then |
42 | | - exists(Location loc, JsonValue locValue | |
43 | | - loc = this.getLocation() and |
44 | | - locValue = this.getPropValue("$location") and |
45 | | - path = |
46 | | - any(File f | |
47 | | - f.getAbsolutePath() |
48 | | - .matches("%" + locValue.getPropValue("file").getStringValue() + ".json") |
49 | | - ).getAbsolutePath().regexpReplaceAll("\\.json$", "") and |
50 | | - sl = locValue.getPropValue("line").getIntValue() and |
51 | | - sc = locValue.getPropValue("col").getIntValue() and |
52 | | - el = sl and |
53 | | - // Currently $locations does not provide an end location. However, we can |
54 | | - // automatically deduce the end location from the length of the name. |
55 | | - ec = sc + getUnqualifiedName().length() - 1 |
56 | | - ) |
57 | | - else super.getLocation().hasLocationInfo(path, sl, sc, el, ec) |
58 | | - } |
| 74 | + override string getObjectLocationName() { result = getUnqualifiedName() } |
59 | 75 |
|
60 | 76 | /** |
61 | 77 | * Gets the name of this CDL element. |
@@ -225,6 +241,8 @@ class CdlAttribute extends CdlObject { |
225 | 241 | exists(CdlElement entity | this = entity.getPropValue("elements").getPropValue(name)) |
226 | 242 | } |
227 | 243 |
|
| 244 | + override string getObjectLocationName() { result = getName() } |
| 245 | + |
228 | 246 | string getType() { result = this.getPropStringValue("type") } |
229 | 247 |
|
230 | 248 | int getLength() { result = this.getPropValue("length").(JsonPrimitiveValue).getIntValue() } |
|
0 commit comments