vfile-location
vfile-location is a utility package within the unified ecosystem, currently at stable version 5.0.3, designed to convert between positional (line and column-based) and offset (character index-based) locations within a vfile instance. It provides `toOffset` and `toPoint` methods for accurate translation. The package sees active development and frequent, minor releases, with major versions typically introducing breaking changes like the move to ESM-only and Node.js 16+ requirement in v5. A key differentiator is its tight integration with `vfile`, making it ideal for tasks like generating precise linting reports or manipulating text based on raw file content where traditional AST node locations might be insufficient for location tracking.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/vfile-location/index.js from .../your-file.js not supported.
cause Attempting to use `vfile-location` (an ESM package since v5) with a CommonJS `require()` statement.fixUpdate your module to use ES module syntax (`import { location } from 'vfile-location'`) and ensure your project is configured for ESM, or use a bundler. -
TypeError: (0, _vfile_location.location) is not a function
cause Incorrect import syntax for the `location` function, often due to attempting a default import or transpilation issues with named exports.fixEnsure you are using a named import: `import { location } from 'vfile-location'`. -
TypeError: Cannot read properties of undefined (reading 'toOffset')
cause The `place` variable (returned by `location(file)`) is `undefined` because `location()` was called with an invalid `file` object, or the `location` function itself was not correctly imported.fixVerify that `VFile` is correctly imported and initialized before passing it to `location()`. Also, double-check the `location` import syntax as detailed in `imports`.
Warnings
- breaking `vfile-location` v5.0.0 and later are ESM-only. CommonJS `require()` statements will fail, leading to `ERR_REQUIRE_ESM` errors.
- breaking Version 5.0.0 requires Node.js 16 or higher. Older Node.js versions are no longer supported.
- breaking The `toOffset` method now returns `undefined` for out-of-bounds input instead of `-1`. This changes how you should check for invalid results.
- breaking The `toPoint` method now returns `undefined` for invalid points (e.g., out-of-bounds offsets) instead of its previous behavior (which might have been returning an invalid point or throwing).
- gotcha When importing, ensure you use named imports (`{ location }`) as `vfile-location` does not provide a default export. Attempting a default import (`import location from 'vfile-location'`) will result in `undefined` or a runtime error.
Install
-
npm install vfile-location -
yarn add vfile-location -
pnpm add vfile-location
Imports
- location
const { location } = require('vfile-location')import { location } from 'vfile-location' - Location
import { Location } from 'vfile-location'import type { Location } from 'vfile-location' - VFile
const { VFile } = require('vfile')import { VFile } from 'vfile'
Quickstart
import { VFile } from 'vfile';
import { location } from 'vfile-location';
// Create a VFile instance with some content
const fileContent = 'foo\nbar\nbaz';
const file = new VFile(fileContent);
// Create a location index for the file
const place = location(file);
// Convert a line/column point to an offset
const point = { line: 3, column: 3 };
const offset = place.toOffset(point); // Should be 10 (0-indexed)
console.log(`Offset for point {line: ${point.line}, column: ${point.column}} is: ${offset}`);
// Convert an offset back to a line/column point
const targetOffset = 10;
const retrievedPoint = place.toPoint(targetOffset);
console.log(`Point for offset ${targetOffset} is: {line: ${retrievedPoint?.line}, column: ${retrievedPoint?.column}, offset: ${retrievedPoint?.offset}}`);
// Demonstrate out-of-bounds handling (v5 change)
const outOfBoundsOffset = 100;
const invalidPoint = place.toPoint(outOfBoundsOffset);
console.log(`Point for out-of-bounds offset ${outOfBoundsOffset} is: ${invalidPoint}`); // Should be undefined
const outOfBoundsPoint = { line: 10, column: 1 };
const invalidOffset = place.toOffset(outOfBoundsPoint);
console.log(`Offset for out-of-bounds point {line: ${outOfBoundsPoint.line}, column: ${outOfBoundsPoint.column}} is: ${invalidOffset}`); // Should be undefined