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-astVerified import paths — ran on the pinned version, not inferred.
Demonstrates converting various JavaScript literals and objects into their Babel AST representation and then generating code from them using `@babel/generator`.
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.
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.
Understand the limitations: this package is for *literals and plain objects/arrays of literals*. For converting arbitrary JavaScript code into AST, use `@babel/parser`.
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.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.