@nodesecure/js-x-ray
Version:
JavaScript AST XRay analysis
47 lines • 1.57 kB
JavaScript
import { CALL_EXPRESSION_DATA } from "../contants.js";
import { generateWarning } from "../warnings.js";
import { toArrayLocation } from "../utils/toArrayLocation.js";
// CONSTANTS
const kLogUsageMethods = new Set(["console.log", "console.info", "console.warn", "console.error", "console.debug"]);
function validateNode(_node, ctx) {
const identifierOrMemberExpr = ctx.context?.[CALL_EXPRESSION_DATA]?.identifierOrMemberExpr;
return [
kLogUsageMethods.has(identifierOrMemberExpr),
identifierOrMemberExpr
];
}
function initialize(ctx) {
const { sourceFile } = ctx;
for (const logUsageMethod of kLogUsageMethods) {
sourceFile.tracer.trace(logUsageMethod, {
followConsecutiveAssignment: true
});
}
}
function main(node, ctx) {
const logIdentifer = ctx.data;
const arrayLocation = ctx.context?.[logIdentifer];
if (arrayLocation) {
arrayLocation.push(toArrayLocation(node.loc ?? undefined));
}
else {
ctx.context[logIdentifer] = [toArrayLocation(node.loc ?? undefined)];
}
}
function finalize(ctx) {
const { sourceFile, context } = ctx;
if (context && Object.keys(context).length > 0) {
const warning = generateWarning("log-usage", { value: Object.keys(context).join(", ") });
sourceFile.warnings.push({ ...warning, location: Object.values(context).flat() });
}
}
export default {
name: "log-usage",
validateNode,
initialize,
main,
finalize,
breakOnMatch: false,
context: {}
};
//# sourceMappingURL=log-usage.js.map