JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2986658
  • Score
    100M100P100Q237538F
  • License MIT

micromark utility to try and parse events w/ acorn

Package Exports

  • micromark-util-events-to-acorn

Readme

micromark-util-events-to-acorn

Build Coverage Downloads Size Sponsors Backers Chat

micromark utility to try and parse events w/ acorn.

Contents

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with npm:

npm install micromark-util-events-to-acorn

In Deno with esm.sh:

import {mdxExpression} from 'https://esm.sh/micromark-util-events-to-acorn@1'

In browsers with esm.sh:

<script type="module">
  import {mdxExpression} from 'https://esm.sh/micromark-util-events-to-acorn@1?bundle'
</script>

Use

import {eventsToAcorn} from 'micromark-util-events-to-acorn'

// A factory that uses the utility:
/** @type {Tokenizer} */
function factoryMdxExpression(effects, ok, nok) {
  return start

  // …

  /** @type {State} */
  atClosingBrace(code) {
    // …

    // Gnostic mode: parse w/ acorn.
    const result = eventsToAcorn(
      self.events.slice(eventStart),
      acorn,
      acornOptions,
      {
        start: startPosition,
        expression: true,
        allowEmpty,
        prefix: spread ? '({' : '',
        suffix: spread ? '})' : ''
      }
    )

    // …
  }

  // …
}

API

This module exports the identifier eventsToAcorn. There is no default export.

The export map supports the endorsed development condition. Run node --conditions development module.js to get instrumented dev code. Without this condition, production code is loaded.

eventsToAcorn(events, options)

Parameters
  • events (Array<Event>) — events
  • options.acorn (Acorn, required) — object with acorn.parse and acorn.parseExpressionAt
  • options.acornOptions (AcornOptions) — configuration for acorn
  • options.start (Point, optional) — place where events start
  • options.prefix (string, default: '') — text to place before events
  • options.suffix (string, default: '') — text to place after events
  • options.expression (boolean, default: false) — whether this is a program or expression
  • options.allowEmpty (boolean, default: false) — whether an empty expression is allowed (programs are always allowed to be empty)
Returns
  • estree (Program?) — estree node
  • error (Error?) — error if unparseable
  • swallow (boolean) — whether the error, if there is one, can be swallowed and more JavaScript could be valid

Types

This package is fully typed with TypeScript. It exports the additional types Acorn, AcornOptions, Options, Point, and Program.

Security

See security.md in micromark/.github for how to submit a security report.

Contribute

See contributing.md in micromark/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Titus Wormer