transpiler

AST-based transpiler wrapper

Transpiler

AST-based transpiler wrapper.

Usage

var myTranspiler = require('transpiler').create(<spec>);

var myResult = myTranspiler.transpile(<AST>, <DATA|null>, [<OPTIONS>]);

Here's a transpiler for a very simple AST spec that represents expressions:

var transpilerSpec = {
    nodes: {
        'EXPR': function (node, transpile) {
            return transpile(node.left) + ' ' + node.operator + ' ' + transpile(node.right);
        },
        'OPERAND': function (node) {
            return node.value;
        }
    }
};

We can create a transpiler that uses this spec like so:

var expressionTranspiler = require('transpiler').create(transpilerSpec);

Now we're ready to perform a transpile. Let's start with a simple example, here's my AST:

var myAST = {
    name: 'EXPR',
    left: {
        name: 'OPERAND',
        value: 21
    },
    operator: '+',
    right: {
        name: 'OPERAND',
        value: 27
    }
};

And let's transpile:

console.log(expressionTranspiler.transpile(myAST)); // Logs '21 + 27'

Overrides

It's possible to override or customise the way certain nodes are transpiled. As an example, let's change it so that operands with the value 27 are wrapped in yes(...). Here's my AST again:

var myAST = {
    name: 'EXPR',
    left: {
        name: 'OPERAND',
        value: 21
    },
    operator: '+',
    right: {
        name: 'OPERAND',
        value: 27
    }
};

Now let's transpile, but this time we'll specify a custom way to transpile OPERANDnodes:

console.log(expressionTranspiler.transpile(myAST, null, {
    nodes: {
        'OPERAND': function (node, transpile, context, original) {
            if (node.value === 27) {
                return 'yes(' + original(node) + ')';
            }

            return original(node);
        }
    }
})); // Logs '21 + yes(27)'

Keeping up to date

HomePage

https://github.com/asmblah/transpiler

Repository

https://github.com/asmblah/transpiler


上一篇:bundly
下一篇:phptojs

相关推荐

  • wxss-transpiler

    transpile wxss wxsstranspiler Build Statusciimgci Port of wcsc.exe to Javascript. Test only work...

    1 年前
  • wxml-transpiler

    template compiler for wxml wxmltranspiler This package can be used to compile wxml templates.(vie...

    1 年前
  • nuclide-node-transpiler

    Transpiles JavaScript files nuclidenodetranspiler CDN by jsDelivr A free, fast, and re...

    2 年前
  • gulp-es6-transpiler

    Transpile ES6 to ES5 gulp(http://gulpjs.com)es6transpiler Build Status(https://travisci.org/sindre...

    1 年前
  • grunt-es6-transpiler

    Transpile ES6 to ES5 gruntes6transpiler Build Status(https://travisci.org/sindresorhus/gruntes6tra...

    1 年前
  • es6-transpiler

    es6 es5 es6transpiler.js es6 es5 status Beta Goal different output than traceur...

    1 年前
  • es6-module-transpiler-system-formatter

    ES6 Module Transpiler Extension to Output System.register() Format. es6moduletranspilersystemformat...

    1 年前
  • es6-module-transpiler-npm-resolver

    ES6 Module Transpiler Extension to Resolver from NPM modules with jsnext:main flag. es6moduletransp...

    1 年前
  • es6-module-transpiler

    es6moduletranspiler is an experimental compiler that allows you to write your JavaScript using a sub...

    1 年前
  • broccoli-babel-transpiler

    A Broccoli plugin which transpile ES6 to readable ES5 by using babel. broccolibabeltranspiler Bui...

    1 年前

官方社区

扫码加入 JavaScript 社区