astral-pass

Pass system for Astral

Astral Pass

Pass system for Astral.

There are two kinds of passes: info, and transform.

Info passes operate on a copy of the AST, but are allowed to modify the info object passed into the function as a parameter.

myPass.
  info(function (chunkinfo) {
    info.foo = 'bar'; // persists 
 
    chunk.foo = 'bar'; // does not persist 
  });

Transforms, on the other hand, allow you to modify the structure of the part of the AST that the pass matches, but prevents you from making changes to the info parameter.

myPass.
  info(function (chunkinfo) {
    info.foo = 'bar'; // does not persist 
 
    chunk.foo = 'bar'; // persists 
  });

Below is a simple example.

var myPass = require('astral-pass')();
 
myPass.name = 'myPass';
myPass.prereqs = [ /* other pass names here */ ];
 
myPass.
  when({
    // ... AST chunk 
  }).
  when(function (chunkinfo) {
    // return true or false 
  }).
  transform(function (chunkinfo) {
 
  });

You can introduce more complicated behavior by composing passes. This is done by modifying a pass's prereques property.

var astralPass = require('astral-pass');
 
// p1 needs some info to do its transformations 
 
var p1 = astralPass();
p1.name = 'myPass';
p1.prereqs = [ 'p2' ];
 
p1.
  when( ... ).
  transform( ... );
 
// p2 gathers the info for p1 
 
var p2 = astralPass();
p2.name = 'myPass';
 
p2.
  when( ... ).
  info( ... );
 

I recommend namespacing passes with :.

MIT