Line

You are reading docs for v0.8. See latest (v0.9)

packages/ast/jsxTree

AST package: the JSX element tree with stable node ids.

AST, v0.8. Source: packages/ast/jsxTree.mjs.

Functions#

findParentRecord()#

findParentRecord(byId, nodeId): any

Defined in: packages/ast/jsxTree.mjs:117

The direct parent record of nodeId in a parsed tree's byId, or null for a root.

Parameters#

byId

Map<string, any>

The byId map from parseJsxTree.

nodeId

string

The child's id.

Returns#

any

The parent record, or null for a root.


jsxAttributes()#

jsxAttributes(openingElement, source): object[]

Defined in: packages/ast/jsxTree.mjs:31

An opening tag's attributes as {kind, name, value, index} records (index = 0-based position in the attribute list; a spread has name: null). kinds: 'string' | 'number' | 'boolean' | 'identifier' | 'expression' (value = raw source text) | 'spread' (value = the spread argument's source text).

Parameters#

openingElement

any

A JSX opening element node.

source

string

The full source text (for expression values).

Returns#

object[]

One record per attribute, in order.


jsxNameToString()#

jsxNameToString(nameNode): string

Defined in: packages/ast/jsxTree.mjs:15

Foo, ui.Item or ns:tag for a JSX name node ('?' for anything else).

Parameters#

nameNode

any

A JSX name node.

Returns#

string

The name as written; '?' for an unknown node type.


parseJsxTree()#

parseJsxTree(source): object

Defined in: packages/ast/jsxTree.mjs:69

Parse source into { roots, byId, ast }. Each record is {id, tag, isFragment, isCustomComponent, props, start, end, line, children, openingElementNode} (openingElementNode is the raw estree opening element, null for a fragment -- internal, for offset-exact attribute edits). Nesting is derived from source ranges, so elements found anywhere in a parent's subtree (inside {cond && <X/>} or .map(...) callbacks) nest correctly. Throws on a syntax error.

Parameters#

source

string

JSX or TSX module text.

Returns#

object

The element tree.

ast

ast: any

byId

byId: Map<string, any>

roots

roots: any[]

Throws#

On a syntax error.

Since#

0.8

Example#

const { roots, byId } = parseJsxTree('export const A = () => <div><b /></div>;');
roots[0].tag; // => 'div'