Skip to content

State per file, init per file #1485

@DmitrySoshnikov

Description

@DmitrySoshnikov

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions