unified

Project: syntax-tree/esast-util-from-estree

Package: esast-util-from-estree@1.0.0

  1. Dependencies: 2·Dependents: 0
  2. esast utility to transform from estree
  1. util 130
  2. utility 126
  3. tree 44
  4. ast 32
  5. javascript 17
  6. transform 8
  7. ecmascript 7
  8. estree 4
  9. esast 3
  10. recma 3
  11. esast-util 2

esast-util-from-estree

Build Coverage Downloads Size Sponsors Backers Chat

esast utility to transform from estree.

This is often an optional transform: estrees can be used in most places where esast can be used, and vice versa. This makes sure nodes are plain JSON, adds unist positions, normalizes .bigint, and removes certain discouraged fields.

Install

npm:

npm install esast-util-from-estree

Note that this package is ESM only: it must be imported instead of required.

Use

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

// Make acorn support comments and positional info.
var comments = []
var 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

esast-util-from-estree exports the following identifiers: fromEstree. There is no default export.

fromEstree(estree, options?)

Given an estree returns an esast.

Pass options.dirty: true to leave discouraged fields in the tree.

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