JavaScript Object to AST Converter
The `to-ast` package, currently at version 1.0.0, provides a utility for converting standard JavaScript objects and primitive values into a Mozilla Parser API-compatible Abstract Syntax Tree (AST) representation. This library is specifically designed to facilitate the generation of JavaScript source code from data structures, often used in conjunction with tools like `escodegen`. It supports a wide range of built-in types including primitives (numbers, strings, booleans, undefined, null), functions, arrays, buffers, dates, errors, regular expressions, and object literals. A key differentiator is its ability to handle custom types by respecting a `toAST` method on the object's prototype, allowing for custom AST representations. Given its singular purpose and stable version, its release cadence is likely low, operating more as a specialized, maintenance-mode utility.
Common errors
-
TypeError: toAST is not a function
cause Attempting to import 'to-ast' using ESM 'import' syntax, which is not supported by this CommonJS-only package.fixChange your import statement to `const toAST = require('to-ast');`. -
escodegen.generate is not a function
cause Mistaking `to-ast` for a code generator. `to-ast` only produces Abstract Syntax Trees (ASTs), it does not generate source code directly.fixInstall and use a separate code generation library like `escodegen` to convert the AST output from `to-ast` into executable JavaScript code. Example: `const escodegen = require('escodegen'); escodegen.generate(yourAst);`
Warnings
- gotcha This package is a CommonJS module and does not natively support ESM 'import' syntax in v1.0.0. Attempting to 'import' it will result in a module resolution error.
- gotcha The AST generated by 'to-ast' strictly adheres to the Mozilla Parser API. While compatible with 'escodegen', other AST transformation tools might require normalization or a different AST format.
Install
-
npm install to-ast -
yarn add to-ast -
pnpm add to-ast
Imports
- toAST
import toAST from 'to-ast';
const toAST = require('to-ast');
Quickstart
const toAST = require('to-ast');
const escodegen = require('escodegen'); // Often used in conjunction with to-ast
// Convert a number literal to its AST representation
const numberAst = toAST(2);
console.log('AST for 2:', JSON.stringify(numberAst, null, 2));
// Expected output: {"type":"Literal","value":2}
// Generate source code from the AST
const numberSource = escodegen.generate(numberAst);
console.log('Source for 2:', numberSource);
// Expected output: 2
// Demonstrate custom type conversion using a 'toAST' method
function Person(name) {
this.name = name;
}
Person.prototype.toAST = function() {
return {
type: 'NewExpression',
callee: { type: 'Identifier', name: 'Person' },
arguments: [{ type: 'Literal', value: this.name }]
};
};
const personInstance = new Person('Devon');
const personAst = toAST(personInstance);
console.log('AST for Person:', JSON.stringify(personAst, null, 2));
/* Expected output:
{
"type": "NewExpression",
"callee": {
"type": "Identifier",
"name": "Person"
},
"arguments": [
{
"type": "Literal",
"value": "Devon"
}
]
}
*/
const personSource = escodegen.generate(personAst);
console.log('Source for Person:', personSource);
// Expected output: new Person('Devon')