Skip to content

Latest commit

 

History

History
176 lines (123 loc) · 5.67 KB

README.md

File metadata and controls

176 lines (123 loc) · 5.67 KB

esast-util-builder

github release npm codecov module type: esm license conventional commits typescript vitest yarn

esast utility to build trees

Contents

What is this?

This is a tiny but useful utility for building esast nodes.

When should I use this?

Use this package when you need to create esast nodes.

If you need to build nodes for other ASTs, use unist-util-builder instead.

Install

This package is ESM only.

In Node.js (version 18+) with yarn:

yarn add @flex-development/esast-util-builder
See Git - Protocols | Yarn  for details regarding installing from Git.

In Deno with esm.sh:

import { u } from 'https://esm.sh/@flex-development/esast-util-builder'

In browsers with esm.sh:

<script type="module">
  import { u } from 'https://esm.sh/@flex-development/esast-util-builder'
</script>

Use

TODO: use

API

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

u(type[, builder])

Build an esast node using a child node array, properties object, or value.

If builder is omitted, a void node (a node with only a type field) will be created.

👉 Undefined literals must be created using a properties object, rather than a value. Passing undefined will create a void node.

Type Parameters

  • T (Type<AnyNode>) - esast node type
Parameters
  • type (T) - esast node type
  • builder (Builder<T>, optional) - node children, properties, or value
Returns

Match<AnyNode, T> new esast node.

AnyNode

Union of nodes that can occur in esast (TypeScript type).

👉 This type is exported from @flex-development/esast.
See AnyNode for more details.

Builder<[T]>

Construct a union of esast node builders (TypeScript type).

See also: ub.AnyBuilder, ub.Builder

type Builder<T extends Type = Type<AnyNode>> = T extends Type<AnyNode>
  ? ub.Builder<Match<AnyNode, T>> extends infer B extends ub.AnyBuilder
    ? B extends ub.BuilderValue
      ? B
      : B extends readonly Node[]
        ? B[number][]
        : {
            [K in keyof B]: K extends 'children'
              ? B[K] extends infer U extends readonly Node[]
                ? U[number][]
                : never
              : B[K]
          }
    : never
  : never

Match<N, Check>

Check if node N passes a test. (TypeScript type).

👉 This type is exported from @flex-development/unist-util-types.
See Match<N, Check> for more details.

Type<[T]>

Extract type from node T (TypeScript type).

👉 This type is exported from @flex-development/unist-util-types.
See Type<[T]> for more details.

Types

This package is fully typed with TypeScript.

Related

  • esast — ecmascript abstract syntax tree format

Contribute

See CONTRIBUTING.md.

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