transpiler

raw JSON →
1.2.0 verified Fri May 01 auth: no javascript maintenance

AST-based transpiler wrapper for Node.js, version 1.2.0 (last release unknown, appears stable but rarely updated). It provides a simple way to define node handlers that transform AST nodes into strings. Unlike full-featured compiler toolkits like Babel, it is focused on lightweight, minimal transpilation without dependencies. Supports overrides per transpile call.

error TypeError: transpiler.transpile is not a function
cause Imported the module incorrectly, e.g., const transpiler = require('transpiler'); instead of calling create().
fix
Use const transpiler = require('transpiler').create(spec); or destructure create.
error TypeError: node.left is undefined
cause AST node missing expected properties or handler not receiving the full node structure.
fix
Ensure AST has the required fields (e.g., left, right).
error Error: Unknown node type 'XYZ'
cause Transpiler spec does not define a handler for the node type 'XYZ'.
fix
Add a handler for that node type in the spec or in options overrides.
error RangeError: Maximum call stack size exceeded
cause Recursive or circular AST structure causing infinite recursion in transpile.
fix
Check AST for cycles or ensure base cases in handlers.
breaking In version 1.0.0, the 'transpile' method changed signature from (ast, options) to (ast, data, options).
fix Update calls to pass null as second argument if no data is needed: transpile(ast, null, options).
gotcha Node handlers receive 'transpile' as second argument; easily confused with the instance's 'transpile' method.
fix Inside handler, use the local 'transpile' function to compile child nodes, not the instance method.
gotcha The 'original' function in overrides refers to the default handler for that node; calling 'original(node)' without arguments may cause issues if the handler expects more parameters.
fix Always pass the node to original: original(node).
gotcha Package is purely synchronous; cannot handle async transpilation steps.
fix Use a different library if you need async; this one is synchronous only.
npm install transpiler
yarn add transpiler
pnpm add transpiler

Creates a basic arithmetic transpiler from AST spec and transpiles an expression.

const { create } = require('transpiler');

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

const transpiler = create(spec);
const ast = {
  name: 'EXPR',
  left: { name: 'OPERAND', value: 21 },
  operator: '+',
  right: { name: 'OPERAND', value: 27 }
};

console.log(transpiler.transpile(ast)); // '21 + 27'