estree-util-value-to-estree is a utility library for converting diverse JavaScript values into their corresponding ESTree Abstract Syntax Tree (AST) expressions. This package is particularly useful for tools that manipulate or generate JavaScript code, enabling programmatic construction of AST nodes from native JavaScript types. The current stable version is 3.5.0, with a release cadence that includes regular minor and patch updates to support new JavaScript features and fix bugs. Key differentiators include its broad support for various primitive types (e.g., bigint, symbol, undefined, null, boolean, number, string) and object types (e.g., Array, Object, Map, Set, Date, RegExp, Buffer, various TypedArrays, and Temporal types). It also provides a `serialize` option for handling custom or unsupported values, allowing users to define how complex or application-specific data structures should be represented in the AST. It is designed for values that can be constructed without needing a runtime context.
npm install estree-util-value-to-estreeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates converting a complex JavaScript object into an ESTree expression, then generating JavaScript code from that ESTree. It includes an example of using the `serialize` option to handle custom class instances and `Buffer` objects, which are not natively supported as simple literals.
Upgrade to version `3.3.3` or newer. This version fixes the `__proto__` property emit to prevent prototype pollution.
If supporting `Temporal` types, include `@js-temporal/polyfill` or ensure a native `Temporal` implementation is available in your target environment.
Utilize the `options.serialize` function to provide custom logic for converting specific types or instances into appropriate ESTree nodes. This gives you full control over how non-standard values are represented.
Change your import statement to use ES module syntax: `import { valueToEstree } from 'estree-util-value-to-estree';`Implement a `serialize` function in the options object passed to `valueToEstree` to explicitly define how to convert the unsupported value into an ESTree node. For example, `valueToEstree(myCustomValue, { serialize: (value) => value instanceof MyClass ? { type: 'NewExpression', callee: { type: 'Identifier', name: 'MyClass' }, arguments: [] } : undefined })`.No dependency data recorded yet.