Registry / serialization / estree-util-value-to-estree

estree-util-value-to-estree

JSON →
library3.5.0jsnpmunverified

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-estree
INSTALL
IMPORT
SIG · ESTREE-UTIL-VALUE-
E
estree-util-value-to-estree
serializationjavascriptv3.5.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.

valueToEstree
import { valueToEstree } from 'estree-util-value-to-estree'
const valueToEstree = require('estree-util-value-to-estree')
This package is designed for ESM consumption. While CommonJS might work in some transpiled environments, native ESM import is the intended and recommended approach.
Options
import type { Options } from 'estree-util-value-to-estree'
Type import for the configuration options of the valueToEstree function, useful for TypeScript projects.

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.

import { valueToEstree } from 'estree-util-value-to-estree'; import { generate } from 'astring'; // Commonly used to convert ESTree to code const exampleValue = { id: 123, name: 'Example Item', enabled: true, price: 99.95, details: ['alpha', 'beta', null], config: new Map([['key1', 'value1'], ['key2', 42n]]), created: new Date('2024-01-01T10:00:00Z'), pattern: /test/g, complex: { nested: Symbol.for('unique-id'), buffer: Buffer.from('hello'), // Unsupported types will require a custom serializer customClass: new class MyCustom { constructor() { this.prop = 'value'; } }() } }; // Convert a standard JavaScript value to an ESTree expression const estreeExpression = valueToEstree(exampleValue); console.log('Generated ESTree (partial):', estreeExpression.type); console.log('Generated Code for standard value:\n', generate(estreeExpression)); // Example with a custom serializer for the custom class const estreeWithCustom = valueToEstree(exampleValue, { serialize(value) { if (value instanceof Buffer) { // Convert Buffer to a Buffer.from() call return { type: 'CallExpression', callee: { type: 'MemberExpression', object: { type: 'Identifier', name: 'Buffer' }, property: { type: 'Identifier', name: 'from' }, computed: false, optional: false }, arguments: [{ type: 'Literal', value: value.toString('hex') }, { type: 'Literal', value: 'hex' }] }; } if (value && typeof value === 'object' && value.constructor.name === 'MyCustom') { // Convert MyCustom instance to a new MyCustom() expression return { type: 'NewExpression', callee: { type: 'Identifier', name: 'MyCustom' }, arguments: [] }; } // Let the default logic handle other types return undefined; } }); console.log('\nGenerated Code with custom serializer:\n', generate(estreeWithCustom));
Debug
Known issues
breakingA critical vulnerability (CVE-2025-32014) was identified where converting an object with a `__proto__` property could lead to prototype pollution in the generated ESTree. This allows an attacker to inject arbitrary properties into `Object.prototype`, affecting all objects in the application.
fix
Upgrade to version `3.3.3` or newer. This version fixes the `__proto__` property emit to prevent prototype pollution.
affects: <3.3.3
gotchaWhen working with `Temporal` types (e.g., `Temporal.Instant`, `Temporal.PlainDate`), ensure that the `Temporal` global object is available or polyfilled in the environment where the generated code will run. While the library itself was updated in v3.3.1 to not crash if `Temporal` is undefined, the resulting AST for `Temporal` objects will rely on the `Temporal` global being present.
fix
If supporting `Temporal` types, include `@js-temporal/polyfill` or ensure a native `Temporal` implementation is available in your target environment.
affects: >=3.3.0
gotchaFor unsupported JavaScript values or custom class instances, the default behavior of `valueToEstree` might not produce the desired AST representation. For example, a custom class instance might be serialized as a plain object or might lead to an error if not handled.
fix
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.
affects: >=3.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require()` to import `estree-util-value-to-estree` in a pure ESM context (e.g., in a Node.js module with `"type": "module"` in `package.json` or a `.mjs` file).
fix
Change your import statement to use ES module syntax: `import { valueToEstree } from 'estree-util-value-to-estree';`
Error: Unknown value type for serialization.
You are attempting to serialize a value type that is not natively supported by `estree-util-value-to-estree`, and no custom `serialize` option has been provided to handle it.
fix
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 })`.
Upgrade
Version history
3.5.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
Resources
estree-util-value-to-estree — npm install estree-util-value-to-estree · libregistry