0.1.0 • Published 1 year ago

@js-rocks/babel-tiny v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@js-rocks/babel-tiny

A simple babel implementation that can be used to parse javascript grammar, include parser, traverser, core and cli.

Install

yarn add @js-rocks/babel-tiny

Usage

define custom plugin and then use

const source = `
    const d = 2;
    const e = 4;
    function add(a, b) {
        const tmp = 1;
        return a + b;
    }
    add(c, d);
`;

function plugin1(api, options) {
  return {
    visitor: {
      Identifier(path) {
        if (path.findParent(p => p.isCallExpression())) {
          path.replaceWith(api.template.expression(options.replaceName));
        }
      }
    }
  };
}

const { code, map } = transformSync(source, {
  plugins: [
    [
      plugin1,
      {
        replaceName: 'minus'
      }
    ]
  ]
});

expect(code.indexOf('minus(c, d)') > -1).toEqual(true);