@@ -7,20 +7,58 @@ import advanced_security.javascript.frameworks.cap.CDS
7
7
8
8
abstract class CdlObject extends JsonObject {
9
9
predicate hasLocationInfo ( string path , int sl , int sc , int el , int ec ) {
10
- exists ( Location loc , JsonValue locValue |
11
- loc = this .getLocation ( ) and
12
- locValue = this .getPropValue ( "$location" ) and
13
- path =
14
- any ( File f |
15
- f .getAbsolutePath ( )
16
- .matches ( "%" + locValue .getPropValue ( "file" ) .getStringValue ( ) + ".json" )
17
- ) .getAbsolutePath ( ) .regexpReplaceAll ( "\\.json$" , "" ) and
18
- sl = locValue .getPropValue ( "line" ) .getIntValue ( ) and
19
- sc = locValue .getPropValue ( "col" ) .getIntValue ( ) and
20
- el = sl + 1 and
21
- ec = 1
22
- )
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
+ if
23
+ not exists ( locValue .getPropValue ( "line" ) ) and
24
+ not exists ( locValue .getPropValue ( "col" ) )
25
+ then
26
+ // We don't know where this entity starts, so mark the whole file
27
+ sl = 0 and
28
+ sc = 0 and
29
+ el = 0 and
30
+ ec = 0
31
+ else (
32
+ sl = locValue .getPropValue ( "line" ) .getIntValue ( ) and
33
+ (
34
+ if exists ( locValue .getPropValue ( "col" ) )
35
+ then sc = locValue .getPropValue ( "col" ) .getIntValue ( )
36
+ else
37
+ // We don't know where this entity starts, so mark the start of the line
38
+ sc = 0
39
+ ) and
40
+ el = sl and
41
+ (
42
+ if exists ( getObjectLocationName ( ) )
43
+ then
44
+ // Currently $locations does not provide an end location. However, we can
45
+ // automatically deduce the end location from the length of the name.
46
+ ec = sc + getObjectLocationName ( ) .length ( ) - 1
47
+ else
48
+ // Mark a single character if we cannot predicate the length
49
+ ec = sc + 1
50
+ )
51
+ )
52
+ )
53
+ else super .getLocation ( ) .hasLocationInfo ( path , sl , sc , el , ec )
23
54
}
55
+
56
+ /**
57
+ * The name of the object that should be highlighted as the location.
58
+ *
59
+ * This is used to deduce the length of the location.
60
+ */
61
+ string getObjectLocationName ( ) { none ( ) }
24
62
}
25
63
26
64
private newtype CdlKind =
@@ -31,21 +69,26 @@ private newtype CdlKind =
31
69
CdlFunctionKind ( string value ) { value = "function" }
32
70
33
71
/**
34
- * Any CDL element, including entities, event , actions, and more.
72
+ * A list of CDL definitions, which can include entities, events , actions and more.
35
73
*/
36
- class CdlDefinition extends CdlObject {
37
- CdlDefinition ( ) { exists ( JsonObject root | this = root .getPropValue ( "definitions" ) ) }
74
+ class CdlDefinitions extends CdlObject {
75
+ CdlDefinitions ( ) { exists ( JsonObject root | this = root .getPropValue ( "definitions" ) ) }
38
76
39
77
JsonObject getElement ( string elementName ) { result = this .getPropValue ( elementName ) }
40
78
41
79
JsonObject getAnElement ( ) { result = this .getElement ( _) }
42
80
}
43
81
82
+ /**
83
+ * A CDL definition element.
84
+ */
44
85
abstract class CdlElement extends CdlObject {
45
86
CdlKind kind ;
46
87
string name ;
47
88
48
- CdlElement ( ) { exists ( CdlDefinition definition | this = definition .getElement ( name ) ) }
89
+ CdlElement ( ) { exists ( CdlDefinitions definitions | this = definitions .getElement ( name ) ) }
90
+
91
+ override string getObjectLocationName ( ) { result = getUnqualifiedName ( ) }
49
92
50
93
/**
51
94
* Gets the name of this CDL element.
@@ -215,6 +258,8 @@ class CdlAttribute extends CdlObject {
215
258
exists ( CdlElement entity | this = entity .getPropValue ( "elements" ) .getPropValue ( name ) )
216
259
}
217
260
261
+ override string getObjectLocationName ( ) { result = getName ( ) }
262
+
218
263
string getType ( ) { result = this .getPropStringValue ( "type" ) }
219
264
220
265
int getLength ( ) { result = this .getPropValue ( "length" ) .( JsonPrimitiveValue ) .getIntValue ( ) }
0 commit comments