Skip to content

Implement a new way to count and store occurences of Nodes #235

@fraxken

Description

@fraxken

Current SourceFile class implement a bunch of counter and also identifiersName that keep the trace of all kind of declaration identifiers across the file.

varkinds = { var: 0, let: 0, const: 0 };
idtypes = { assignExpr: 0, property: 0, variableDeclarator: 0, functionDeclaration: 0 };
counter = {
identifiers: 0,
doubleUnaryArray: 0,
computedMemberExpr: 0,
memberExpr: 0,
deepBinaryExpr: 0,
encodedArrayValue: 0
};

Today, these "counters" are updated in probes that are often used for no other purpose.

function main(mainNode, options) {
const { sourceFile } = options;
sourceFile.varkinds[mainNode.kind]++;
for (const node of mainNode.declarations) {
sourceFile.idtypes.variableDeclarator++;
for (const { name } of getVariableDeclarationIdentifiers(node.id)) {
sourceFile.identifiersName.push({ name, type: "variableDeclarator" });
}
}
}

Or

function main(node, options) {
const { sourceFile } = options;
kIdExtractor(
({ name }) => sourceFile.identifiersName.push({ name, type: "params" }),
node.params
);
if (node.id === null || node.id.type !== "Identifier") {
return;
}
sourceFile.idtypes.functionDeclaration++;
sourceFile.identifiersName.push({ name: node.id.name, type: "functionDeclaration" });
}

For me, these should not be managed by probes but by another mechanism/abstraction

const isIdentifier = (node) => node !== null && node.type === "Identifier";

const nc = new NodeCounter("FunctionDeclaration", (node) => isIdentifier(node.id));
console.log(nc.type); // FunctionDeclaration

nc.walk(astNode);

console.log(nc.nodes); // integer

Then we could probably create a CounterAggregator or something like that to get an Object at the end.

But we still need a way to also manage identifiersName.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions