I have a collection of user provided scripts I would like to analyze. In particular, I want to determine whether a specific API was used. The only way for a user to use the API is to interact with a parameter I inject into their script.
In this script, the API in question was used.
function main(api: ProvidedApi) {
api.doSomething();
}
In this script, the API in question was not used.
function main(api: ProvidedApi) {
return 5;
}
This is the format that the user scripts always take, though they can have more functions, classes, whatever separate from this 'main' function.
I've tried using the Typescript compiler to traverse the SourceFile and calling
const symbl = typeChecker.getSymbolAtLocation(node);
if (typeChecker.getFullyQualifiedName(symbl).includes('ProvidedApi')) {
return true;
}
but that doesn't work if there's no typing information, for example in the following:
function main(api: ProvidedApi) {
doSomethingFunc(api);
}
function doSomethingFunc(api) {
api.doSomething();
}
Though the API in question was used, the fully qualified name will not include ProvidedApi.
It sounds like you should have the API itself record whether it was called. If you don't control the API, if I understand what you are after, could you call the various API's through a wrapper function? Then the wrapper can record what has been called. (I'm not sure where you are planning to use that info, so your mileage may vary)