{"id":10537,"library":"babel-literal-to-ast","title":"Babel Literal to AST","description":"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.","status":"maintenance","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/izaakschroeder/babel-literal-to-ast","tags":["javascript"],"install":[{"cmd":"npm install babel-literal-to-ast","lang":"bash","label":"npm"},{"cmd":"yarn add babel-literal-to-ast","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-literal-to-ast","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency, required for full Babel ecosystem integration and consistent AST handling.","package":"@babel/core","optional":false}],"imports":[{"note":"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`.","wrong":"const serialize = require('babel-literal-to-ast');","symbol":"serialize","correct":"import serialize from 'babel-literal-to-ast';"},{"note":"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.","symbol":"Node types (indirect)","correct":"import * as t from '@babel/types';"}],"quickstart":{"code":"import serialize from 'babel-literal-to-ast';\nimport generate from '@babel/generator';\n\n// Basic literal: string\nlet astString = serialize('Hello, Babel!');\nconsole.log('String AST:', generate(astString).code);\n// Expected output: String AST: \"Hello, Babel!\"\n\n// Basic literal: number\nlet astNumber = serialize(12345);\nconsole.log('Number AST:', generate(astNumber).code);\n// Expected output: Number AST: 12345\n\n// Object literal\nlet astObject = serialize({\n  message: 'Hello',\n  version: 2.1,\n  enabled: true,\n  items: [1, 'two', null]\n});\nconsole.log('Object AST:', generate(astObject).code);\n/* Expected output:\nObject AST: ({ message: 'Hello', version: 2.1, enabled: true, items: [1, 'two', null] });\n(Note: The wrapping parentheses are often added by @babel/generator for ExpressionStatement compatibility.)\n*/\n\n// Null and undefined (undefined usually becomes Identifier 'undefined')\nlet astNull = serialize(null);\nlet astUndefined = serialize(undefined);\nconsole.log('Null AST:', generate(astNull).code);\nconsole.log('Undefined AST:', generate(astUndefined).code);\n// Expected output: Null AST: null\n// Expected output: Undefined AST: undefined","lang":"javascript","description":"Demonstrates converting various JavaScript literals and objects into their Babel AST representation and then generating code from them using `@babel/generator`."},"warnings":[{"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.","message":"The 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`).","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"<=2.1.0"},{"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`.","message":"The `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'.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"Attempting to use `require` in an ESM module context or a bundling issue where `default` export is not handled correctly.","error":"TypeError: serialize is not a function"},{"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.","cause":"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.","error":"The 'type' of a node must be a string. Received 'Literal'"}],"ecosystem":"npm"}