{"id":10848,"library":"estree-util-value-to-estree","title":"Convert JavaScript values to ESTree expressions","description":"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.","status":"active","version":"3.5.0","language":"javascript","source_language":"en","source_url":"https://github.com/remcohaszing/estree-util-value-to-estree","tags":["javascript","esast","estree","estree-util","language","unist","typescript"],"install":[{"cmd":"npm install estree-util-value-to-estree","lang":"bash","label":"npm"},{"cmd":"yarn add estree-util-value-to-estree","lang":"bash","label":"yarn"},{"cmd":"pnpm add estree-util-value-to-estree","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is designed for ESM consumption. While CommonJS might work in some transpiled environments, native ESM import is the intended and recommended approach.","wrong":"const valueToEstree = require('estree-util-value-to-estree')","symbol":"valueToEstree","correct":"import { valueToEstree } from 'estree-util-value-to-estree'"},{"note":"Type import for the configuration options of the valueToEstree function, useful for TypeScript projects.","symbol":"Options","correct":"import type { Options } from 'estree-util-value-to-estree'"}],"quickstart":{"code":"import { valueToEstree } from 'estree-util-value-to-estree';\nimport { generate } from 'astring'; // Commonly used to convert ESTree to code\n\nconst exampleValue = {\n  id: 123,\n  name: 'Example Item',\n  enabled: true,\n  price: 99.95,\n  details: ['alpha', 'beta', null],\n  config: new Map([['key1', 'value1'], ['key2', 42n]]),\n  created: new Date('2024-01-01T10:00:00Z'),\n  pattern: /test/g,\n  complex: {\n    nested: Symbol.for('unique-id'),\n    buffer: Buffer.from('hello'),\n    // Unsupported types will require a custom serializer\n    customClass: new class MyCustom { constructor() { this.prop = 'value'; } }()\n  }\n};\n\n// Convert a standard JavaScript value to an ESTree expression\nconst estreeExpression = valueToEstree(exampleValue);\nconsole.log('Generated ESTree (partial):', estreeExpression.type);\nconsole.log('Generated Code for standard value:\\n', generate(estreeExpression));\n\n// Example with a custom serializer for the custom class\nconst estreeWithCustom = valueToEstree(exampleValue, {\n  serialize(value) {\n    if (value instanceof Buffer) {\n      // Convert Buffer to a Buffer.from() call\n      return {\n        type: 'CallExpression',\n        callee: { type: 'MemberExpression', object: { type: 'Identifier', name: 'Buffer' }, property: { type: 'Identifier', name: 'from' }, computed: false, optional: false },\n        arguments: [{ type: 'Literal', value: value.toString('hex') }, { type: 'Literal', value: 'hex' }]\n      };\n    }\n    if (value && typeof value === 'object' && value.constructor.name === 'MyCustom') {\n      // Convert MyCustom instance to a new MyCustom() expression\n      return {\n        type: 'NewExpression',\n        callee: { type: 'Identifier', name: 'MyCustom' },\n        arguments: []\n      };\n    }\n    // Let the default logic handle other types\n    return undefined;\n  }\n});\nconsole.log('\\nGenerated Code with custom serializer:\\n', generate(estreeWithCustom));","lang":"typescript","description":"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."},"warnings":[{"fix":"Upgrade to version `3.3.3` or newer. This version fixes the `__proto__` property emit to prevent prototype pollution.","message":"A 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.","severity":"breaking","affected_versions":"<3.3.3"},{"fix":"If supporting `Temporal` types, include `@js-temporal/polyfill` or ensure a native `Temporal` implementation is available in your target environment.","message":"When 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.","severity":"gotcha","affected_versions":">=3.3.0"},{"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.","message":"For 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.","severity":"gotcha","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to use ES module syntax: `import { valueToEstree } from 'estree-util-value-to-estree';`","cause":"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).","error":"ReferenceError: require is not defined"},{"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 })`.","cause":"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.","error":"Error: Unknown value type for serialization."}],"ecosystem":"npm"}