Skip to content

Commit 957e34d

Browse files
committed
Make function body features library independent of CodeToFeatures AST
1 parent 9e50ce8 commit 957e34d

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/EndpointFeatures.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private string getTokenFeature(DataFlow::Node endpoint, string featureName) {
3131
featureName = "enclosingFunctionBody" and
3232
result =
3333
strictconcat(string token, Location l |
34-
FunctionBodyFeatures::bodyTokens(entity, l, token)
34+
FunctionBodyFeatures::bodyTokens(entity.getDefinedFunction(), l, token)
3535
|
3636
token, " "
3737
order by

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/FunctionBodyFeatures.qll

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import javascript
8-
private import CodeToFeatures
98
private import FeaturizationConfig
109

1110
string getTokenizedAstNode(ASTNode node) {
@@ -36,8 +35,8 @@ ASTNode getAnASTNodeToFeaturize(Function f) {
3635
}
3736

3837
/**
39-
* Get a function containing the endpoint that is suitable for featurization. In general,
40-
* this associates an endpoint to multiple functions, since there may be more than one multiple entities to a single endpoint.
38+
* Get a function containing the endpoint that is suitable for featurization. In general, this
39+
* can associate an endpoint to multiple functions, since functions can be nested in JavaScript.
4140
*/
4241
Function getAFunctionForEndpoint(DataFlow::Node endpoint) {
4342
result = endpoint.getContainer().getEnclosingContainer*()
@@ -108,19 +107,19 @@ Function getRepresentativeFunctionForEndpoint(DataFlow::Node endpoint) {
108107
)
109108
}
110109

111-
/** Holds if `location` is the location of an AST node within the entity `entity` and `token` is a node attribute associated with that AST node. */
112-
predicate bodyTokens(DatabaseFeatures::Entity entity, Location location, string token) {
113-
// Performance optimization: Restrict the set of entities to those containing an endpoint to featurize.
114-
entity.getDefinedFunction() =
110+
/** Holds if `location` is the location of an AST node within the function `function` and `token` is a node attribute associated with that AST node. */
111+
predicate bodyTokens(Function function, Location location, string token) {
112+
// Performance optimization: Restrict the set of functions to those containing an endpoint to featurize.
113+
function =
115114
getRepresentativeFunctionForEndpoint(any(FeaturizationConfig cfg).getAnEndpointToFeaturize()) and
116115
// Performance optimization: If a function has more than 256 body subtokens, then featurize it as absent. This
117116
// approximates the behavior of the classifer on non-generic body features where large body
118117
// features are replaced by the absent token.
119118
//
120119
// We count nodes instead of tokens because tokens are often not unique.
121-
strictcount(getAnASTNodeToFeaturize(entity.getDefinedFunction())) <= 256 and
120+
strictcount(getAnASTNodeToFeaturize(function)) <= 256 and
122121
exists(ASTNode node |
123-
node = getAnASTNodeToFeaturize(entity.getDefinedFunction()) and
122+
node = getAnASTNodeToFeaturize(function) and
124123
token = getTokenizedAstNode(node) and
125124
location = node.getLocation()
126125
)

0 commit comments

Comments
 (0)