VFile Message Statistics Utility
vfile-statistics is a lightweight utility within the unified ecosystem designed to categorize and count messages associated with `vfile` instances. It provides statistics such as the total number of messages, as well as counts for fatal errors, warnings, and informational messages. The current stable version is 3.0.0, which notably requires Node.js 16 or higher and is an ESM-only package. This library follows the release cadence of the broader unified collective, tying major version bumps to Node.js LTS updates and significant changes in its peer dependencies like `vfile`. Its key differentiator is its focused simplicity, serving as a composable building block for tools processing virtual files, rather than a standalone, opinionated linter or parser.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` vfile-statistics in a CommonJS context.fixConvert your project to ESM or switch to using dynamic `import()` if you need to load an ESM module from CommonJS. For example: `const { statistics } = await import('vfile-statistics');` -
TypeError: statistics is not a function
cause Incorrect import statement (e.g., `import statistics from 'vfile-statistics'`) or trying to use a CJS `require` in an ESM context.fixEnsure you are using named imports for ESM: `import { statistics } from 'vfile-statistics'`. Also, verify your environment supports ESM (Node.js 16+ or browser with `<script type="module">`). -
The 'value' parameter is required. Don't call when missing.
cause Calling `statistics()` without providing a valid `VFile`, `VFileMessage`, or array thereof.fixAlways pass a non-null, non-undefined `VFile`, `VFileMessage`, or an array containing these types as the argument to `statistics(file)`.
Warnings
- breaking vfile-statistics v3.0.0 changed its minimum Node.js requirement to Node.js 16 or higher.
- breaking vfile-statistics v2.0.0 transitioned to an ESM-only package, dropping CommonJS support.
- breaking In v3.0.0, the `value` parameter for `statistics` is now required. Calling it without a valid `VFile`, `VFileMessage`, or array of them will cause an error.
- breaking v3.0.0 introduced changes to use the `export` map. Direct access to private APIs or non-exported paths is no longer supported.
- gotcha The `Statistics` type is for TypeScript only and should be imported using `import type`. Importing it as a regular value will lead to build or runtime errors.
Install
-
npm install vfile-statistics -
yarn add vfile-statistics -
pnpm add vfile-statistics
Imports
- statistics
const statistics = require('vfile-statistics')import { statistics } from 'vfile-statistics' - Statistics
import { Statistics } from 'vfile-statistics'import type { Statistics } from 'vfile-statistics' - statistics
import { statistics } from 'https://esm.sh/vfile-statistics@3'
Quickstart
import { VFile } from 'vfile'
import { statistics } from 'vfile-statistics'
// Create a new VFile instance
const file = new VFile({path: '~/example.md', value: '# Hello'})
// Add various types of messages to the file
file.message('This observation could be improved', { ruleId: 'style-guide' })
file.message('Another styling suggestion', { line: 1, column: 3 })
// Simulate a fatal error that might be caught or thrown
try {
file.fail('Critical parsing error found', { ruleId: 'parser-error', fatal: true })
} catch (error) {
// In a real application, you might handle the error here
console.error('Caught a vfile.fail error:', error.message)
}
file.info('Metadata: author is John Doe', { ruleId: 'meta-data' })
// Get and log the statistics
const stats = statistics(file)
console.log('File statistics:', stats)
/* Expected Output:
File statistics: { fatal: 1, nonfatal: 3, warn: 2, info: 1, total: 4 }
*/