Registry / serialization / micromark-util-events-to-acorn

micromark-util-events-to-acorn

JSON →
library2.0.3jsnpmunverified

micromark-util-events-to-acorn is a core utility within the micromark ecosystem, specifically designed to bridge micromark's low-level parsing events with acorn's robust JavaScript parsing capabilities. It takes an array of micromark events and attempts to construct an Abstract Syntax Tree (AST) using an acorn parser instance. This package is crucial for handling MDX expressions and similar constructs where embedded JavaScript needs to be parsed and validated. The current stable version is 2.0.3. While it doesn't have an independent, fixed release cadence, its updates are typically coordinated with major and minor releases of related micromark extensions, such as micromark-extension-mdx-expression. A key differentiator is its specialized role in the micromark architecture, providing a performant and integrated way to parse JavaScript directly from event streams, requiring the acorn parser and its options to be passed explicitly rather than being a direct dependency. It is ESM-only, requiring Node.js 16 or later.

npm install micromark-util-events-to-acorn
INSTALL
IMPORT
SIG · MICROMARK-UTIL-EVE
M
micromark-util-events-to-acorn
serializationjavascriptv2.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

eventsToAcorn
import { eventsToAcorn } from 'micromark-util-events-to-acorn'
const { eventsToAcorn } = require('micromark-util-events-to-acorn')
This package is ESM-only since v2.0.0 and requires Node.js 16+. CommonJS `require` will result in an `ERR_REQUIRE_ESM` error.
Options
import type { Options } from 'micromark-util-events-to-acorn'
TypeScript type for the configuration object passed to `eventsToAcorn`.
Result
import type { Result } from 'micromark-util-events-to-acorn'
TypeScript type for the return value of `eventsToAcorn`, which contains the parsed `estree` or an `error`.

Demonstrates how to use `eventsToAcorn` to parse a mock stream of `micromark` events into an `ESTree` using `acorn`.

import { eventsToAcorn } from 'micromark-util-events-to-acorn'; import * as acorn from 'acorn'; // acorn must be installed and provided // Mock micromark events representing a simple MDX expression `{1 + 1}`. // In a real micromark application, these events would be generated by a tokenizer // parsing Markdown/MDX content. const sampleEvents = [ { type: 'mdxExpressionText', start: { line: 1, column: 2, offset: 1 }, end: { line: 1, column: 7, offset: 6 } } ]; const mockAcornOptions = { ecmaVersion: 2024, sourceType: 'module' }; // Attempt to parse the micromark events into an ESTree AST using acorn. const result = eventsToAcorn(sampleEvents, { acorn: { parse: acorn.parse, parseExpressionAt: acorn.parseExpressionAt, }, tokenTypes: ['mdxExpressionText'], // Essential since v2.0.0: list micromark token types that represent code data acornOptions: mockAcornOptions, start: { line: 1, column: 1, offset: 0 }, // Start position for source mapping expression: true, // Indicate that we are parsing an expression allowEmpty: false, // Do not allow empty expressions prefix: '', suffix: '' }); if (result.error) { console.error('Failed to parse expression:', result.error.message); } else if (result.estree) { console.log('Successfully parsed ESTree expression:'); console.log(JSON.stringify(result.estree.body[0], null, 2)); // Expected output will be an ESTree node for '1 + 1'. } else { console.log('Parsing completed, but no ESTree generated (e.g., empty expression allowed).'); }
Debug
Known issues
breakingVersion 2.0.0 and later require Node.js 16 or higher. Older Node.js versions are no longer supported, aligning with the `unified` collective's compatibility policy.
fix
Upgrade your Node.js environment to version 16 or newer to ensure compatibility and receive security updates.
affects: >=2.0.0
breakingThe `eventsToAcorn` function now explicitly requires the `tokenTypes` option to be passed. This array of strings specifies which `micromark` token types should be concatenated to form the source text that `acorn` will parse.
fix
Ensure `options.tokenTypes` is provided as an array of `TokenType` strings, for example, `tokenTypes: ['mdxExpressionText']` for MDX expressions.
affects: >=2.0.0
gotchaThis package is ESM-only. Attempting to `require()` it in a CommonJS module will result in a runtime error.
fix
Ensure your project is configured for ESM and use `import` statements. If using Node.js, add `"type": "module"` to your `package.json` or use `.mjs` file extensions.
affects: >=2.0.0
gotchaWhile `micromark-util-events-to-acorn` itself accepts `acornOptions`, the parent `micromark-extension-mdx-expression@3.0.0` changed its default `ecmaVersion` to 2024. If using this utility in an MDX context, ensure your `acornOptions` are explicitly set if you require a different ECMAScript version.
fix
Always explicitly specify `ecmaVersion` (e.g., `ecmaVersion: 2015`) in your `acornOptions` if you need a version other than the default `2024` to maintain consistent parsing behavior.
affects: >=3.0.0 (for related extension)
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'includes')
The `tokenTypes` option was not provided to `eventsToAcorn` or was not an array of strings, which is a required parameter since v2.0.0 for processing the event stream.
fix
Pass `tokenTypes: ['someTokenType']` as an option to `eventsToAcorn`, where 'someTokenType' are the micromark token types containing the code to be parsed.
ERR_REQUIRE_ESM
You attempted to `require()` this ESM-only package (`micromark-util-events-to-acorn`) in a CommonJS environment (e.g., in a `.js` file without `"type": "module"` in `package.json`).
fix
Refactor your code to use ES Modules (e.g., `import { eventsToAcorn } from 'micromark-util-events-to-acorn'`) and ensure your project is configured for ESM.
Error: Cannot find module 'acorn'
The `acorn` parser library is a required peer/external dependency that must be installed and explicitly passed to `eventsToAcorn`, but it was not found.
fix
Install `acorn` (e.g., `npm install acorn`) and ensure you provide an `acorn` object with `parse` and `parseExpressionAt` methods in the `options` object.
Upgrade
Version history
2.0.3latest on npm
Audit
Dependencies
acornrequiredThe `acorn` parser instance is a required option for `eventsToAcorn` to parse event streams into an ESTree. It must be provided by the consuming application.
micromarkoptionalThis package is a utility for projects using `micromark` (v3+), which generate the low-level events consumed by `eventsToAcorn`.
Agent activity
4 hits · last 30 days
node
4
Resources