esast-util-from-estree

esast utility to transform from estree

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
esast-util-from-estree
102.0.08 months ago3 years agoMinified + gzip package size for esast-util-from-estree in KB

Readme

esast-util-from-estree
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
esast utility to transform from estree.

Contents

*   [`fromEstree(estree[, options])`](#fromestreeestree-options)
*   [`Options`](#options)

What is this?

This package applies some transforms to a cloned, 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 onlyesm. In Node.js (version 16+), install with npm:
npm install esast-util-from-estree

In Deno with esm.shesmsh:
import {fromEstree} from 'https://esm.sh/esast-util-from-estree@2'

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

Use

import {parse} from 'acorn'
import {fromEstree} from './index.js'

// Make acorn support comments and positional info.
/** @type {Array<import('acorn').Comment>} */
const comments = []
/** @type {import('estree').Program} */
// @ts-expect-error: acorn looks like estree.
const estree = parse(
  'export function x() { /* Something senseless */ console.log(/(?:)/ + 1n) }',
  {
    sourceType: 'module',
    ecmaVersion: 'latest',
    locations: true,
    onComment: comments
  }
)
estree.comments = comments

const esast = fromEstree(estree)

console.log(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 fromEstreeapi-from-estree. There is no default export.

fromEstree(estree[, options])

Turn an estree into an esast.
Parameters
— estree
— configuration
Returns
Clean clone of estree (UnistNodeesast).

Options

Configuration (TypeScript Type).
Fields
  • dirty (boolean, default: false)
— leave discouraged fields in the tree

Types

This package is fully typed with TypeScript. It exports the additional type Optionsapi-options.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, esast-util-from-estree@^2, compatible with Node.js 16.

Contribute

See contributing.mdcontributing in syntax-tree/.githubhealth for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MITlicense © Titus Wormerauthor