(JS/TS) Resolve FQNs for Definitions
Suppose we've already captured definitions with ast-grep, we'll need to compute a fully qualified name (FQN) for each one.
If our input file is src/utils.js:
function topLevel() {
return true;
}
class Calculator {
add(a, b) {
return a + b;
}
class MathUtils {
static PI = 3.14;
square(x) {
return x * x;
}
}
}
const print = () => { console.log("Hello World") }
const config = {
timeout: 5000,
validate() {
return this.timeout > 0;
}
};Here's what we'd expect for FQNs for our input file:
src.utils.topLevelsrc.utils.Calculatorsrc.utils.Calculator.addsrc.utils.Calculator.MathUtilssrc.utils.Calculator.MathUtils.PIsrc.utils.Calculator.MathUtils.squaresrc.utils.printsrc.utils.configsrc.utils.config.validate
Keep in mind that if we have anonymous functions accessible via object like:
const authMiddleware = {
jwt: {
verify: async (token, options = {}) => {
// Do Something
}
}
};Our FQN would be src.utils.authMiddleware.jwt.verify, but if the underlying implementation of verify gets overwritten at runtime, or authMiddleware gets aliased, we'll need to account for it. Open to suggestions here.
Edited by Michael Usachenko