Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fraxken opened this issue Feb 1, 2024 · 1 comment · Fixed by #239
Closed

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

fraxken opened this issue Feb 1, 2024 · 1 comment · Fixed by #239
Assignees

Comments

@fraxken
Copy link
Member

fraxken commented Feb 1, 2024

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.

@fraxken
Copy link
Member Author

fraxken commented Feb 1, 2024

isBinaryExpression probe is also kinda an "exception" because of the complexity

function main(node, options) {
  const { sourceFile } = options;

  const [binaryExprDeepness, hasUnaryExpression] = walkBinaryExpression(node);
  if (binaryExprDeepness >= 3 && hasUnaryExpression) {
    sourceFile.counter.deepBinaryExpr++;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant