{"id":13717067,"url":"https://github.com/remcohaszing/estree-util-value-to-estree","last_synced_at":"2025-04-06T03:11:59.084Z","repository":{"id":47135107,"uuid":"345178797","full_name":"remcohaszing/estree-util-value-to-estree","owner":"remcohaszing","description":"Convert a JavaScript value to an estree expression","archived":false,"fork":false,"pushed_at":"2025-02-02T09:37:28.000Z","size":1219,"stargazers_count":25,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T02:09:46.436Z","etag":null,"topics":["esast","estree","estree-util","javascript","language","typescript","unist"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/remcohaszing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"remcohaszing"}},"created_at":"2021-03-06T19:34:09.000Z","updated_at":"2025-03-29T05:09:24.000Z","dependencies_parsed_at":"2024-04-02T15:30:55.512Z","dependency_job_id":"6f281a5e-3c95-44a4-b4be-a5855e942dfb","html_url":"https://github.com/remcohaszing/estree-util-value-to-estree","commit_stats":{"total_commits":118,"total_committers":2,"mean_commits":59.0,"dds":0.07627118644067798,"last_synced_commit":"02df96d4381525a8093ebc6f10a21b647d959cad"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcohaszing%2Festree-util-value-to-estree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcohaszing%2Festree-util-value-to-estree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcohaszing%2Festree-util-value-to-estree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcohaszing%2Festree-util-value-to-estree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/remcohaszing","download_url":"https://codeload.github.com/remcohaszing/estree-util-value-to-estree/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427012,"owners_count":20937214,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["esast","estree","estree-util","javascript","language","typescript","unist"],"created_at":"2024-08-03T00:01:17.589Z","updated_at":"2025-04-06T03:11:59.067Z","avatar_url":"https://github.com/remcohaszing.png","language":"TypeScript","funding_links":["https://github.com/sponsors/remcohaszing"],"categories":["esast utilities","编辑器相关"],"sub_categories":[],"readme":"# estree-util-value-to-estree\n\n[![github actions](https://github.com/remcohaszing/estree-util-value-to-estree/actions/workflows/ci.yaml/badge.svg)](https://github.com/remcohaszing/estree-util-value-to-estree/actions/workflows/ci.yaml)\n[![codecov](https://codecov.io/gh/remcohaszing/estree-util-value-to-estree/branch/main/graph/badge.svg)](https://codecov.io/gh/remcohaszing/estree-util-value-to-estree)\n[![npm version](https://img.shields.io/npm/v/estree-util-value-to-estree)](https://www.npmjs.com/package/estree-util-value-to-estree)\n[![npm downloads](https://img.shields.io/npm/dm/estree-util-value-to-estree)](https://www.npmjs.com/package/estree-util-value-to-estree)\n\nConvert a JavaScript value to an [ESTree](https://github.com/estree/estree) expression.\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [API](#api)\n  - [`valueToEstree(value, options?)`](#valuetoestreevalue-options)\n- [Compatibility](#compatibility)\n- [License](#license)\n\n## Installation\n\n```sh\nnpm install estree-util-value-to-estree\n```\n\n## Usage\n\nThis package converts a JavaScript value to an [ESTree](https://github.com/estree/estree) expression\nfor values that can be constructed without the need for a context.\n\nCurrently the following types are supported:\n\n- [`bigint`](https://developer.mozilla.org/docs/Glossary/BigInt)\n- [`boolean`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)\n- [`null`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/null)\n- [`number`](https://developer.mozilla.org/docs/Glossary/Number) (Including\n  [Infinity](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Infinity)\n  and [NaN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/NaN))\n- [`string`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)\n- [`symbol`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol)\n  ([shared](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol#shared_symbols_in_the_global_symbol_registry)\n  and\n  [well-known](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol#well-known_symbols))\n- [`object`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)\n- [null-prototype `Object`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#null-prototype_objects)\n- [`undefined`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Undefined)\n- [`Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\n- [`BigInt64Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array)\n- [`BigUint64Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array)\n- [`Boolean`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)\n- [`Buffer`](https://nodejs.org/api/buffer.html)\n- [`Date`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date)\n- [`Float32Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Float32Array)\n- [`Float64Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Float64Array)\n- [`Int16Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int16Array)\n- [`Int32Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int32Array)\n- [`Int8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int8Array)\n- [`Map`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)\n- [`Number`](https://developer.mozilla.org/docs/Glossary/Number)\n- [`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)\n- [`Set`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)\n- [`String`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)\n- [`Temporal.Duration`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration)\n- [`Temporal.Instant`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant)\n- [`Temporal.PlainDate`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDate)\n- [`Temporal.PlainDateTime`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime)\n- [`Temporal.PlainYearMonth`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth)\n- [`Temporal.PlainMonthDay`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainMonthDay)\n- [`Temporal.PlainTime`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainTime)\n- [`Temporal.ZonedDateTime`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime)\n- [`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)\n- [`Uint8ClampedArray`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray)\n- [`Uint16Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array)\n- [`Uint32Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array)\n- [`URL`](https://developer.mozilla.org/docs/Web/API/URL)\n- [`URLSearchParams`](https://developer.mozilla.org/docs/Web/API/URLSearchParams)\n\nIf `options.instanceAsObject` is set to `true`, other objects are turned into plain object.\n\n```ts\nimport assert from 'node:assert/strict'\n\nimport { valueToEstree } from 'estree-util-value-to-estree'\n\nconst result = valueToEstree({\n  null: null,\n  undefined,\n  string: 'Hello world!',\n  number: 42,\n  negativeNumber: -1337,\n  infinity: Number.POSITIVE_INFINITY,\n  notANumber: Number.NaN,\n  regex: /\\w+/i,\n  date: new Date('1970-01-01'),\n  array: ['I’m an array item!'],\n  object: { nested: 'value' }\n})\n\nassert.deepEqual(result, {\n  type: 'ObjectExpression',\n  properties: [\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'null' },\n      value: { type: 'Literal', value: null }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'undefined' },\n      value: { type: 'Identifier', name: 'undefined' }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'string' },\n      value: { type: 'Literal', value: 'Hello world!' }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'number' },\n      value: { type: 'Literal', value: 42 }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'negativeNumber' },\n      value: {\n        type: 'UnaryExpression',\n        operator: '-',\n        prefix: true,\n        argument: { type: 'Literal', value: 1337 }\n      }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'infinity' },\n      value: { type: 'Identifier', name: 'Infinity' }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'notANumber' },\n      value: { type: 'Identifier', name: 'NaN' }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'regex' },\n      value: {\n        type: 'Literal',\n        value: /\\w+/i,\n        regex: { pattern: '\\\\w+', flags: 'i' }\n      }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'date' },\n      value: {\n        type: 'NewExpression',\n        callee: { type: 'Identifier', name: 'Date' },\n        arguments: [{ type: 'Literal', value: 0 }]\n      }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'array' },\n      value: {\n        type: 'ArrayExpression',\n        elements: [{ type: 'Literal', value: 'I’m an array item!' }]\n      }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'object' },\n      value: {\n        type: 'ObjectExpression',\n        properties: [\n          {\n            type: 'Property',\n            method: false,\n            shorthand: false,\n            computed: false,\n            kind: 'init',\n            key: { type: 'Literal', value: 'nested' },\n            value: { type: 'Literal', value: 'value' }\n          }\n        ]\n      }\n    }\n  ]\n})\n\nclass Point {\n  line: number\n\n  column: number\n\n  constructor(line: number, column: number) {\n    this.line = line\n    this.column = column\n  }\n}\n\n// Normally complex objects throw.\nassert.throws(() =\u003e valueToEstree(new Point(2, 3)))\n\n// `instanceAsObject: true` treats them as plain objects.\nassert.deepEqual(valueToEstree(new Point(2, 3), { instanceAsObject: true }), {\n  type: 'ObjectExpression',\n  properties: [\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'line' },\n      value: { type: 'Literal', value: 2 }\n    },\n    {\n      type: 'Property',\n      method: false,\n      shorthand: false,\n      computed: false,\n      kind: 'init',\n      key: { type: 'Literal', value: 'column' },\n      value: { type: 'Literal', value: 3 }\n    }\n  ]\n})\n```\n\n## API\n\nThis API exports the function `valueToEstree`.\n\n### `valueToEstree(value, options?)`\n\nConvert a value to an [ESTree](https://github.com/estree/estree) node.\n\n#### options\n\n- `instanceAsObject` (boolean, default: `false`) — If true, treat objects that have a prototype as\n  plain objects.\n- `preserveReferences` (boolean, default: `false`) — If true, preserve references to the same object\n  found within the input. This also allows to serialize recursive structures. If needed, the\n  resulting expression will be an iife.\n\n## Compatibility\n\nThis project is compatible with Node.js 16 or greater.\n\n## License\n\n[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremcohaszing%2Festree-util-value-to-estree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fremcohaszing%2Festree-util-value-to-estree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremcohaszing%2Festree-util-value-to-estree/lists"}