@@ -7,20 +7,58 @@ import advanced_security.javascript.frameworks.cap.CDS
77
88abstract class CdlObject extends JsonObject {
99 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 )
2354 }
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 ( ) }
2462}
2563
2664private newtype CdlKind =
@@ -31,21 +69,26 @@ private newtype CdlKind =
3169 CdlFunctionKind ( string value ) { value = "function" }
3270
3371/**
34- * Any CDL element, including entities, event , actions, and more.
72+ * A list of CDL definitions, which can include entities, events , actions and more.
3573 */
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" ) ) }
3876
3977 JsonObject getElement ( string elementName ) { result = this .getPropValue ( elementName ) }
4078
4179 JsonObject getAnElement ( ) { result = this .getElement ( _) }
4280}
4381
82+ /**
83+ * A CDL definition element.
84+ */
4485abstract class CdlElement extends CdlObject {
4586 CdlKind kind ;
4687 string name ;
4788
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 ( ) }
4992
5093 /**
5194 * Gets the name of this CDL element.
@@ -215,6 +258,8 @@ class CdlAttribute extends CdlObject {
215258 exists ( CdlElement entity | this = entity .getPropValue ( "elements" ) .getPropValue ( name ) )
216259 }
217260
261+ override string getObjectLocationName ( ) { result = getName ( ) }
262+
218263 string getType ( ) { result = this .getPropStringValue ( "type" ) }
219264
220265 int getLength ( ) { result = this .getPropValue ( "length" ) .( JsonPrimitiveValue ) .getIntValue ( ) }
0 commit comments