-
Notifications
You must be signed in to change notification settings - Fork 31
Closed
Description
Current SourceFile class implement a bunch of counter and also identifiersName that keep the trace of all kind of declaration identifiers across the file.
Lines 25 to 34 in e711bb2
| 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.
js-x-ray/src/probes/isVariableDeclaration.js
Lines 14 to 25 in e711bb2
| 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
js-x-ray/src/probes/isFunction.js
Lines 20 to 33 in e711bb2
| 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); // integerThen 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