vfile-sort
vfile-sort is a utility package within the unified/vfile ecosystem designed for ordering `VFile` objects and `VFileMessage` objects. Its current stable version is 4.0.0. The package primarily exposes comparator functions, `compareFile` and `compareMessage`, which can be used with array sorting methods (e.g., `Array.prototype.sort` or `toSorted`) to arrange files or messages by properties like line, column, severity, and other metadata. It follows the Node.js LTS release cycle for compatibility and ships with TypeScript types, making it suitable for modern JavaScript and TypeScript projects. Key differentiators include its tight integration with the `vfile` specification and its focus on providing precise, consistent sorting logic for diagnostics and file-related metadata, ensuring human-readable reports.
Common errors
-
TypeError: (0 , _vfile_sort__WEBPACK_IMPORTED_MODULE_2__.sort) is not a function
cause Attempting to call the `sort` function after upgrading to v4.0.0, where it was removed.fixInstead of `sort(file)`, use `file.messages.sort(compareMessage)` or `myArray.toSorted(compareMessage/compareFile)`. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to `import` vfile-sort in a CommonJS context (e.g., in a `.js` file without `"type": "module"` in `package.json` or in an older Node.js version).fixEnsure your project is configured for ESM (add `"type": "module"` to your `package.json` or use `.mjs` file extensions) and upgrade Node.js to v16+. -
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/vfile-sort/index.js from ... not supported.
cause Attempting to `require()` vfile-sort, which is an ESM-only package since v3.fixUse `import` statements instead of `require()`. If in a CommonJS context, consider upgrading to a newer Node.js version and migrating to ESM for your project.
Warnings
- breaking The primary `sort(file)` function that directly mutated a `VFile` object was removed in v4.0.0. The package now exclusively exports comparison functions (`compareFile` and `compareMessage`).
- breaking vfile-sort is now an ESM-only package. CommonJS `require()` is no longer supported for importing this library.
- breaking Node.js 16 or newer is now required to use vfile-sort.
- gotcha Using private module APIs, such as directly importing from deep paths (e.g., `vfile-sort/lib/some-internal-module`), is not supported and may break in future versions due to the package's use of `export` maps.
- breaking vfile-sort v4.0.0 updates its internal `vfile` dependency/compatibility to `vfile@6.0.0`. This might introduce further breaking changes if your project uses an older `vfile` version.
Install
-
npm install vfile-sort -
yarn add vfile-sort -
pnpm add vfile-sort
Imports
- compareFile
const { compareFile } = require('vfile-sort')import { compareFile } from 'vfile-sort' - compareMessage
const { compareMessage } = require('vfile-sort')import { compareMessage } from 'vfile-sort' - VFile
const { VFile } = require('vfile')import { VFile } from 'vfile' - VFileMessage
const { VFileMessage } = require('vfile-message')import { VFileMessage } from 'vfile-message'
Quickstart
import { VFile } from 'vfile';
import { VFileMessage } from 'vfile-message';
import { compareFile, compareMessage } from 'vfile-sort';
// Sort VFileMessages
const messages = [
new VFileMessage('Error: Missing semicolon.', { place: { line: 3, column: 1 } }),
new VFileMessage('Warning: Unused variable.', { place: { line: 2, column: 5 } }),
new VFileMessage('Info: Deprecated API.', { place: { line: 3, column: 10 } })
];
const sortedMessages = messages.toSorted(compareMessage).map(String);
console.log('Sorted Messages:', sortedMessages);
// Expected: ['2:5: Warning: Unused variable.', '3:1: Error: Missing semicolon.', '3:10: Info: Deprecated API.']
// Sort VFiles (example using dummy paths for demonstration)
const fileA = new VFile({ path: '/project/src/components/button.ts' });
const fileB = new VFile({ path: '/project/src/utils/helpers.ts' });
const fileC = new VFile({ path: '/project/index.ts' });
const files = [fileA, fileB, fileC];
const sortedFiles = files.toSorted(compareFile).map((d) => d.path);
console.log('Sorted Files:', sortedFiles);
// Expected: ['/project/index.ts', '/project/src/components/button.ts', '/project/src/utils/helpers.ts']