This issue is to discuss design of a state per file, and init hook per file in each transform.
The API of a trasnformer today is an object which properties are names of the nodes, and corresponding handlers of those nodes:
var funcsPerFile = [];
new Transformer('Foo', {
Function: function(node, parent) {
funcsPerFile.push(node.name.id);
...
}
});
The problems is, the transform instance is created once, and then is ran for all files. This means that collections at module-level (like funcsPerFile) are going to be preserved between different files runs. In order to initialize some data "per-file", today's "workaround" is to have the Program handler:
var callsPerFile;
new Transformer('Foo', {
Program: function(node) {
callsPerFile = [];
},
Function: function(node, parent) {
funcsPerFile.push(node.name.id);
...
}
});
This works well today, however some people don't like having the module-level data. Also, if by some reason you need to do two pass of the program in one transform, the data will be overridden.
Alternative to storing this state at module level, we could have just a property _callsPerFile sitting on the transformer, but this looks a bit weird, because usually there sit node handlers (and _callsPerFile doesn't sound like a node name).
Not an urgent thing (since "Program + module-level vars" pattern works), but something to discuss.
CC @amasad
This issue is to discuss design of a state per file, and init hook per file in each transform.
The API of a trasnformer today is an object which properties are names of the nodes, and corresponding handlers of those nodes:
The problems is, the transform instance is created once, and then is ran for all files. This means that collections at module-level (like
funcsPerFile) are going to be preserved between different files runs. In order to initialize some data "per-file", today's "workaround" is to have theProgramhandler:This works well today, however some people don't like having the module-level data. Also, if by some reason you need to do two pass of the program in one transform, the data will be overridden.
Alternative to storing this state at module level, we could have just a property
_callsPerFilesitting on the transformer, but this looks a bit weird, because usually there sit node handlers (and_callsPerFiledoesn't sound like a node name).Not an urgent thing (since "
Program+ module-level vars" pattern works), but something to discuss.CC @amasad