JSPM

esast-util-from-estree

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

esast utility to transform from estree

Package Exports

  • esast-util-from-estree
  • esast-util-from-estree/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (esast-util-from-estree) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

esast-util-from-estree

Build Coverage Downloads Size Sponsors Backers Chat

esast utility to transform from estree.

Contents

What is this?

This package applies some transforms to a given estree to make it compatible with unist. It:

  • makes sure nodes are plain JSON
  • adds unist positions
  • normalizes .bigint
  • remove attributes, selfClosing from JSXOpeningFragment
  • removes certain discouraged fields

When should I use this?

The transform applied by this utility is often optional: estrees can be used in most places where esast can be used, and vice versa. But, if you come from a unist background and want to deal with JavaScript, or want to use unist utilities with JavaScript, this helps a lot.

Install

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

npm install esast-util-from-estree

In Deno with esm.sh:

import {fromEstree} from 'https://esm.sh/esast-util-from-estree@1'

In browsers with esm.sh:

<script type="module">
  import {fromEstree} from 'https://esm.sh/esast-util-from-estree@1?bundle'
</script>

Use

import {parse} from 'acorn'
import {fromEstree} from 'esast-util-from-estree'

// Make acorn support comments and positional info.
const comments = []
const esast = parse(
  'export function x() { /* Something senseless */ console.log(/(?:)/ + 1n) }',
  {sourceType: 'module', locations: true, onComment: comments}
)
esast.comments = comments

console.log(fromEstree(esast))

Yields:

{
  type: 'Program',
  body: [
    {
      type: 'ExportNamedDeclaration',
      declaration: [Object],
      specifiers: [],
      source: null,
      position: [Object]
    }
  ],
  sourceType: 'module',
  comments: [
    {
      type: 'Block',
      value: ' Something senseless ',
      position: [Object]
    }
  ],
  position: {
    start: {line: 1, column: 1, offset: 0},
    end: {line: 1, column: 75, offset: 74}
  }
}

API

This package exports the identifier fromEstree. There is no default export.

fromEstree(estree, options?)

Given an estree returns an esast.

options

Configuration (optional).

options.dirty

Leave discouraged fields in the tree (boolean, default: false).

Types

This package is fully typed with TypeScript. It exports the additional type Options.

Compatibility

Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. Our projects sometimes work with older versions, but this is not guaranteed.

Contribute

See contributing.md in syntax-tree/.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, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer