{"id":12293,"library":"unist-util-lsp","title":"unist-util-lsp Conversion Utility","description":"The `unist-util-lsp` library provides a focused set of utility functions to accurately convert positional information between unist (Universal Syntax Tree) nodes and the Language Server Protocol (LSP) format. This is crucial for developers building sophisticated tooling, such as IDE extensions or linters, that operate on both ASTs and interact with LSP-compliant language servers. The current stable version is 2.1.0, with an active release cadence demonstrating ongoing maintenance and feature additions. Key differentiators include its tight integration with the unist ecosystem and its precise mapping to LSP’s `Range` and `Position` types, making it an essential bridge for creating robust language-aware applications. It primarily targets environments supporting ECMAScript Modules (ESM) and requires Node.js version 16 or higher.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/unist-util-lsp","tags":["javascript","unist","util","utility","lsp","language-server-protocol","typescript"],"install":[{"cmd":"npm install unist-util-lsp","lang":"bash","label":"npm"},{"cmd":"yarn add unist-util-lsp","lang":"bash","label":"yarn"},{"cmd":"pnpm add unist-util-lsp","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is ESM-only since v2.0.0. Use `import` syntax. `fromPoint` converts a unist point to an LSP position.","wrong":"const { fromPoint } = require('unist-util-lsp')","symbol":"fromPoint","correct":"import { fromPoint } from 'unist-util-lsp'"},{"note":"There is no default export; all utilities are named exports. `toPosition` converts an LSP range to a unist position.","wrong":"import toPosition from 'unist-util-lsp'","symbol":"toPosition","correct":"import { toPosition } from 'unist-util-lsp'"},{"note":"`fromPlace` was added in v2.1.0. It converts a unist point or position to an LSP range.","wrong":"import { fromPlace } from 'unist-util-lsp/fromPlace'","symbol":"fromPlace","correct":"import { fromPlace } from 'unist-util-lsp'"}],"quickstart":{"code":"import { fromMarkdown } from 'mdast-util-from-markdown';\nimport {\n  fromPlace,\n  fromPoint,\n  fromPosition,\n  toPoint,\n  toPosition\n} from 'unist-util-lsp';\n\nconst markdown = '## Hello **World**!\\n';\nconst mdast = fromMarkdown(markdown);\n\nconsole.log('Unist Position:', mdast.position);\n\n// Convert unist position to LSP range\nconst lspRange = fromPosition(mdast.position);\nconsole.log('LSP Range from Unist Position:', lspRange);\n\n// Convert LSP range back to unist position\nconst unistPosition = toPosition(lspRange);\nconsole.log('Unist Position from LSP Range:', unistPosition);\n\n// Convert unist point (start of the AST) to LSP position\nconst lspStartPosition = fromPoint(mdast.position.start);\nconsole.log('LSP Position from Unist Start Point:', lspStartPosition);\n\n// Convert LSP position back to unist point\nconst unistStartPoint = toPoint(lspStartPosition);\nconsole.log('Unist Point from LSP Start Position:', unistStartPoint);\n\n// Convert a unist place (point or position) to an LSP range (introduced in v2.1.0)\nconst fullRangeFromPlace = fromPlace(mdast.position);\nconsole.log('LSP Range from Unist Position (via fromPlace):', fullRangeFromPlace);\n\nconst startRangeFromPlace = fromPlace(mdast.position.start);\nconsole.log('LSP Range from Unist Start Point (via fromPlace):', startRangeFromPlace);","lang":"typescript","description":"This example demonstrates how to use the core conversion utilities to map positional data between unist and LSP formats, including converting full positions and individual points, and showcasing the `fromPlace` utility."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or higher. For example, use `nvm install 16 && nvm use 16`.","message":"Version 2.0.0 of `unist-util-lsp` changed to require Node.js 16 or newer. Older Node.js versions are no longer supported.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Migrate your project to use ES Modules `import` syntax. Ensure your `package.json` has `\"type\": \"module\"` or use `.mjs` file extensions.","message":"Starting with v2.0.0, the package is ESM-only. CommonJS `require()` statements will no longer work.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Update your `@types/unist` package to the latest compatible version (e.g., `npm update @types/unist`).","message":"Version 2.0.0 updated its dependency on `@types/unist`. Users leveraging TypeScript should ensure their `@types/unist` dependency is also updated to avoid type mismatches.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure you are using `unist-util-lsp` version 2.1.0 or newer if you intend to use the `fromPlace` function.","message":"The `fromPlace` utility was added in version 2.1.0. If you are on an earlier `2.x` version, this function will not be available.","severity":"gotcha","affected_versions":"<2.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change CommonJS `require()` calls to ES Module `import` statements: `import { fromPoint } from 'unist-util-lsp'`.","cause":"Attempting to use `require()` to import an ESM-only package.","error":"ERR_REQUIRE_ESM"},{"fix":"Ensure the `place` argument passed to utilities like `fromPoint` or `fromPosition` is a valid `UnistPoint` or `UnistPosition` object, not nullish.","cause":"Attempting to access properties of a `null` or `undefined` Unist point/position.","error":"TypeError: Cannot read properties of undefined (reading 'start')"}],"ecosystem":"npm"}