@js-rocks/babel-tiny

0.1.0 • Public • Published

@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);

Readme

Keywords

Package Sidebar

Install

npm i @js-rocks/babel-tiny

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

686 kB

Total Files

5

Last publish

Collaborators

  • chenxiaoyao6228