astral-pass

Pass system for Astral

2 个版本
安装
npm install astral-pass
yarn add astral-pass
pnpm add astral-pass
bun add astral-pass
README

Astral Pass

Pass system for Astral.

Pass Types

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

Info

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 (chunk, info) {
    info.foo = 'bar'; // persists

    chunk.foo = 'bar'; // does not persist
  });

Transform

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 (chunk, info) {
    info.foo = 'bar'; // does not persist

    chunk.foo = 'bar'; // persists
  });

Example

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 (chunk, info) {
    // return true or false
  }).
  transform(function (chunk, info) {

  });

More Complicated Passes

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 :.

License

MIT

版本列表
0.1.0 2013-06-05
0.0.1 2013-05-25