Registry / serialization / to-ast

to-ast

JSON →
library1.0.0jsnpmunverified

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.

npm install to-ast
INSTALL
IMPORT
SIG · TO-AST
T
to-ast
serializationjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

toAST
const toAST = require('to-ast');
import toAST from 'to-ast';
The package is CommonJS-only in v1.0.0. Use 'require' for correct module import.

Demonstrates converting primitive and custom JavaScript objects into ASTs, and then generating source code using escodegen.

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')
Debug
Known issues
gotchaThis 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.
fix
Use 'const toAST = require('to-ast');' for module import.
affects: >=1.0.0
gotchaThe 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.
fix
Verify compatibility with target AST tools or consider using an AST adapter if necessary.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: toAST is not a function
Attempting to import 'to-ast' using ESM 'import' syntax, which is not supported by this CommonJS-only package.
fix
Change your import statement to `const toAST = require('to-ast');`.
escodegen.generate is not a function
Mistaking `to-ast` for a code generator. `to-ast` only produces Abstract Syntax Trees (ASTs), it does not generate source code directly.
fix
Install 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);`
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
to-ast — npm install to-ast · libregistry