Registry / serialization / babel-literal-to-ast

babel-literal-to-ast

JSON →
library2.1.0jsnpmunverified

This utility package, `babel-literal-to-ast`, converts standard JavaScript literals and simple objects directly into their Abstract Syntax Tree (AST) representation, formatted according to Babel's specific AST specification. Unlike general-purpose AST parsers that convert source code strings, this library takes a live JavaScript value (like a number, string, boolean, array, or plain object) and returns the corresponding Babel AST node structure. It currently sits at version 2.1.0, with its last update in February 2019, suggesting a stable but no longer actively developed status. Its primary use case is within Babel transformers or other tools that manipulate Babel ASTs, providing a convenient way to inject or create AST nodes from known literal values without having to manually construct the AST objects or parse stringified code. A key differentiator is its direct output to Babel AST format, which has specific deviations from the more generic ESTree specification.

npm install babel-literal-to-ast
INSTALL
IMPORT
SIG · BABEL-LITERAL-TO-A
B
babel-literal-to-ast
serializationjavascriptv2.1.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.

serialize
import serialize from 'babel-literal-to-ast';
const serialize = require('babel-literal-to-ast');
The package is primarily designed for ESM usage as per the README example, though CJS `require` might work in older Node.js environments. For modern projects, use `import`.
Node types (indirect)
import * as t from '@babel/types';
While `babel-literal-to-ast` generates nodes, you'll often need `@babel/types` for manual AST manipulation or checking node types when working with the output. This is a common companion import in Babel projects.

Demonstrates converting various JavaScript literals and objects into their Babel AST representation and then generating code from them using `@babel/generator`.

import serialize from 'babel-literal-to-ast'; import generate from '@babel/generator'; // Basic literal: string let astString = serialize('Hello, Babel!'); console.log('String AST:', generate(astString).code); // Expected output: String AST: "Hello, Babel!" // Basic literal: number let astNumber = serialize(12345); console.log('Number AST:', generate(astNumber).code); // Expected output: Number AST: 12345 // Object literal let astObject = serialize({ message: 'Hello', version: 2.1, enabled: true, items: [1, 'two', null] }); console.log('Object AST:', generate(astObject).code); /* Expected output: Object AST: ({ message: 'Hello', version: 2.1, enabled: true, items: [1, 'two', null] }); (Note: The wrapping parentheses are often added by @babel/generator for ExpressionStatement compatibility.) */ // Null and undefined (undefined usually becomes Identifier 'undefined') let astNull = serialize(null); let astUndefined = serialize(undefined); console.log('Null AST:', generate(astNull).code); console.log('Undefined AST:', generate(astUndefined).code); // Expected output: Null AST: null // Expected output: Undefined AST: undefined
Debug
Known issues
gotchaThe AST generated by `babel-literal-to-ast` adheres to Babel's specific AST format, which has notable deviations from the standard ESTree specification (e.g., `Literal` vs. `StringLiteral`, `NumericLiteral`, etc.). This means the output may not be directly compatible with tools or libraries expecting strict ESTree ASTs without an additional transformation step (e.g., using `babel-to-estree`).
fix
If strict ESTree compatibility is required, consider using `babel-to-estree` to convert the generated AST, or manually adjust the nodes. Ensure all downstream tooling is aware of the Babel AST format.
affects: >=1.0.0
gotchaThe package has not been updated since February 2019 (v2.1.0). While its core functionality for converting simple literals is stable, it may not support newer JavaScript syntax features or be compatible with future major versions of `@babel/core` without updates.
fix
Test thoroughly with newer Babel versions. For complex or very recent JavaScript features as input, manual AST construction or alternative, actively maintained libraries might be necessary. Monitor its GitHub for any activity or forks.
affects: <=2.1.0
gotchaThe `serialize` function directly converts JavaScript values. For complex types like Functions, Classes, or Dates, it will generally produce AST nodes that represent their literal form, not their executable code. For example, a function will become an `Identifier` named 'Function'.
fix
Understand the limitations: this package is for *literals and plain objects/arrays of literals*. For converting arbitrary JavaScript code into AST, use `@babel/parser`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: serialize is not a function
Attempting to use `require` in an ESM module context or a bundling issue where `default` export is not handled correctly.
fix
Ensure you are using `import serialize from 'babel-literal-to-ast';` for ESM. If in CommonJS, try `const serialize = require('babel-literal-to-ast').default;` or ensure your bundler is configured correctly for default exports.
The 'type' of a node must be a string. Received 'Literal'
This error often occurs when a Babel transformer receives an AST node type that is not valid in its expected context, or when mixing Babel ASTs with strict ESTree expectations. For instance, a generic 'Literal' type from an older parser might clash with Babel's more specific `StringLiteral`, `NumericLiteral`, etc.
fix
Verify that all AST nodes conform to the Babel AST specification. If integrating with non-Babel tools, ensure any conversions (e.g., via `babel-to-estree`) are applied correctly to align node types.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
@babel/corerequiredPeer dependency, required for full Babel ecosystem integration and consistent AST handling.
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
babel-literal-to-ast — npm install babel-literal-to-ast · libregistry