MCPcopy Index your code
hub / github.com/clayrisser/react-ast

github.com/clayrisser/react-ast @0.4.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.4.1 ↗ · + Follow
188 symbols 639 edges 127 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

react-ast

npm GitHub stars

render abstract syntax trees using react

Please ★ this repo if you found it useful ★ ★ ★

Abstract syntax trees are difficult to work with by nature. This is a react renderer that makes interacting with abstract syntax trees and rendering code a breeze.

React AST is the ultimate meta programming tool that uses react to render abstract syntax trees. It can be used to build powerful unopinionated code generators and babel plugins that are easy to read and can scale without creating a rats nest of unreadable code.

You can read a post I wrote about this project at the link below.

Render Abstract Syntax Trees with React

Built by Silicon Hills LLC

Silicon Hills offers premium Node and React develpoment and support services. Get in touch at nuevesolutions.com.

Features

  • works with babel ast
  • supports typescript

Installation

npm install --save react-ast

Dependencies

Usage

Render Code

import React, { FC } from 'react';
import {
  Export,
  Expression,
  Function,
  Identifier,
  Import,
  Interface,
  JSX,
  ReactNode,
  Return,
  TypeAnnotation,
  TypeReference,
  Var,
  VarKind
} from 'react-ast';

const code = render(
  <>
    <Import default="React" imports={['FC']} from="react" />
    <Export>
      <Interface name="HelloProps" />
    </Export>
    <Var kind={VarKind.Const} typeAnnotation="FC<HelloProps>" name="Hello">
      <Function
        arrow
        params={[
          <Identifier
            typeAnnotation={
              <TypeAnnotation>
                <TypeReference name="HelloProps" />
              </TypeAnnotation>
            }
          >
            props
          </Identifier>
        ]}
      >
        <Return>
          <JSX />
        </Return>
      </Function>
    </Var>
    <Expression properties="Hello.defaultProps">{{}}</Expression>
    <Export default>
      <Identifier>Hello</Identifier>
    </Export>
  </>
);

console.log(code);

The rendered code

import React, { FC } from 'react';
export interface HelloProps {}

const Hello: FC<HelloProps> = (props: HelloProps) => {
  return <></>;
};

Hello.defaultProps = {};
export default Hello;

Render AST

Sometimes you might want to render the ast instead of rendering the code.

import React from 'react';
import { ClassDeclaration, renderAst } from 'react-ast';

const ast = renderAst(<Class name="Hello" />);

console.log(ast);

The rendered AST

{ type: 'File',
  program:
   { type: 'Program',
     body: [ [Object] ],
     directives: [],
     sourceType: 'script',
     interpreter: null },
  comments: [],
  tokens: [] }

Support

Submit an issue

Development

You can validate the AST at astexplorer.net with the following settings.

Language ParserSettings Transform
JavaScript babylon7 babelv7

Enabled the following babylon7 plugins

  • jsx
  • typescript
  • asyncGenerators
  • classProperties
  • decorators
  • doExpressions
  • dynamicImport
  • functionBind
  • functionSent
  • numericSeparator
  • objectRestSpread
  • optionalCatchBinding
  • optionalChaining

Contributing

Review the guidelines for contributing

License

MIT License

Jam Risser © 2019

Changelog

Review the changelog

Credits

Support on Liberapay

A ridiculous amount of coffee ☕ ☕ ☕ was consumed in the process of building this project.

Add some fuel if you'd like to keep me going!

Liberapay receiving Liberapay patrons

Extension points exported contracts — how you extend this code

Instance (Interface)
(no doc) [2 implementers]
src/types.ts
Context (Interface)
(no doc)
src/context.ts
FunctionalComponentProps (Interface)
(no doc)
src/components/react/FunctionalComponent/index.tsx
DeepArray (Interface)
(no doc)
src/@types/reactAst.d.ts
IElement (Interface)
(no doc)
src/elements/BaseElement.ts
HashMap (Interface)
(no doc)
src/types.ts
TypeAnnotationProps (Interface)
(no doc)
src/components/types/TypeAnnotation/index.tsx
HashMap (Interface)
(no doc)
src/@types/reactAst.d.ts

Core symbols most depended-on inside this repo

render
called by 254
src/render.ts
debugRef
called by 47
src/util.ts
renderAst
called by 4
src/render.ts
flattenPath
called by 4
src/util.ts
getContext
called by 3
src/context.ts
appendChild
called by 3
src/types.ts
commitMount
called by 2
src/types.ts
renderTypeParameter
called by 2
src/components/classes/ClassDeclaration/index.tsx

Shape

Function 88
Interface 66
Method 18
Class 12
Enum 4

Languages

TypeScript100%

Modules by API surface

src/reconciler.ts25 symbols
src/types.ts15 symbols
src/elements/BaseElement.ts13 symbols
src/components/classes/ClassDeclaration/index.tsx6 symbols
src/components/expressions/Expression/index.tsx5 symbols
src/components/classes/ClassMethod/index.tsx5 symbols
src/util.ts4 symbols
src/components/modules/ExportNamedDeclaration/index.tsx4 symbols
src/components/jsx/JSXOpeningElement/index.tsx4 symbols
src/components/jsx/JSXElement/index.tsx4 symbols
src/components/interfaces/MethodSignature/index.tsx4 symbols
src/components/interfaces/InterfaceDeclaration/index.tsx4 symbols

For agents

$ claude mcp add react-ast \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page