• Stars
    star
    316
  • Rank 132,587 (Top 3 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

render abstract syntax trees with react

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

index

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

More Repositories

1

linux-factory

a framework used to create custom debian linux operating systems
Python
287
star
2

sphinx-markdown-builder

sphinx builder that outputs markdown files.
Python
159
star
3

react-gantt

A gantt chart for react
JavaScript
149
star
4

dotstow

Mirror of https://gitlab.com/risserlabs/community/dotstow
Shell
95
star
5

idempotent-babel-polyfill

import babel-polyfill multiple times
JavaScript
69
star
6

breeze-hacked-cursor-theme

Official cursor theme for Jam OS
Shell
62
star
7

nestjs-crud-prisma

crud for restful apis built with nestjs and prisma
TypeScript
53
star
8

create-react-renderer

learn to build a custom react renderer
TypeScript
25
star
9

reactant

write once . . . render everywhere
TypeScript
25
star
10

easyappointments-docker

A docker image for Easy!Appointments
PHP
25
star
11

keycloak-sso-configs

common keycloak single sign on configurations
22
star
12

blogdown

A back-end agnostic, zero compilation, markdown blogging platform
HTML
22
star
13

wp-plugin-devkit

A modern approach to wordpress plugin development
PHP
21
star
14

nestjs-keycloak

nestjs module for authenticating keycloak
TypeScript
21
star
15

sphinx-jekyll-builder

sphinx builder that outputs jekyll compatible markdown files with frontmatter
Python
20
star
16

sphinx-markdown-parser

write markdown inside of docutils & sphinx projects
Python
19
star
17

gtk-node

Node bindings for Gtk3
C
19
star
18

generator-helm-chart

yeoman generator rancher 2 helm charts
JavaScript
18
star
19

deskterm

Turn your Linux desktop into a terminal
Shell
15
star
20

i3lock-color-ubuntu

i3lock-color build for ubuntu deb
Makefile
13
star
21

charts

rock8s community helm charts
Go
13
star
22

open-terminal

cross platform open terminal and run command
TypeScript
11
star
23

generator-jekyll-plugin

πŸ’Ž Yeoman generator for jekyll plugins
JavaScript
11
star
24

docker-gtk

Run GTK GUI apps with docker
C
11
star
25

react-native-ignore-warnings

βš›οΈπŸ€·βš οΈ Ignore react native warnings and logs
Makefile
11
star
26

make4docker

Use GNU Make to simplify your Docker builds
Makefile
11
star
27

green-docker

Make the most efficient docker images using best practices
Makefile
11
star
28

docker-prisma-studio

prisma studio
Makefile
9
star
29

react-native-exposure-notification

access the covid-19 ios and android exposure notification api from react native
Java
9
star
30

ts-gir

generate typescript from gir
TypeScript
8
star
31

gnome-gtk

typescript bindings for gnome gtk
JavaScript
7
star
32

craco

high level api for interacting with webpack config
TypeScript
7
star
33

react-noscript

Nest html tags under the <noscript> tag in react
JavaScript
7
star
34

generator-trailblazer

A pragmatic implementation of TrailsJS for rapid development
JavaScript
7
star
35

gjs-gtk-typescript

gtk typescript app proof of concept
TypeScript
7
star
36

cross-domain-cookies

Set cookies on other domains
Go
6
star
37

forkbuntu

Easily create your own ubuntu distribution and install cd
Python
6
star
38

lb4-middleware

run express middleware in loopback 4
TypeScript
6
star
39

babel-plugin-gjs

babel plugin for gjs
JavaScript
6
star
40

mklink

Make symbolic links, hard links, and directory junctions from the context menu
C#
6
star
41

lb4-authorization

authorization adapter for loopback 4
TypeScript
5
star
42

ory-stack

Ory hydra oathkeeper and keto
5
star
43

xmpp-react-hooks

react hooks for xmpp
JavaScript
5
star
44

ory-keto-client

Ory Keto access control client for JavaScript and TypeScript
TypeScript
5
star
45

dotfiles

My dotfiles - enjoy!
Emacs Lisp
5
star
46

beegfs-installer

Easy installer for BeeGFS
Python
5
star
47

identity-api

An identity api built with loopback 4
TypeScript
5
star
48

generator-node-module-typescript

yeoman generator for typescript node modules
Makefile
4
star
49

eternal-utils

Eternally persist exported environment variables, aliases and sources
C
4
star
50

kube-api-proxy

proxy kubernetes api
Smarty
4
star
51

readme

An opinionated README file
4
star
52

trailduck

a topological cyclic sorting algorithm based on depth-first search (dfs)
JavaScript
4
star
53

binary-fetch

Fetch binary data with incremental progress from a browser
JavaScript
4
star
54

hmr-client

A client for communicating with hot module replacement (HMR)
JavaScript
4
star
55

ts-ignore

ignore typescript errors
TypeScript
4
star
56

redux-context-provider

πŸ’‰ Inject redux into react context
JavaScript
4
star
57

js-info

Get system information about your JavaScript runtime engine
JavaScript
4
star
58

python-env

Cross platform isolated embedded python environment
JavaScript
4
star
59

google-chart-react

Implement Google Charts in react
JavaScript
4
star
60

generator-github-project

🎩 :octocat: Yeoman generator for GitHub projects
JavaScript
4
star
61

nestjs-example

example nestjs project integrated with keycloak
TypeScript
4
star
62

oh-my-zsh-docker

run oh-my-zsh from a docker container
Dockerfile
3
star
63

use-constructor

react hook that behaves like a class constructor for functional components
Makefile
3
star
64

awesome-ideas

a curated list of awesome ideas
3
star
65

terraform-rancher-2

Initialize rancher 2 with terraform
HCL
3
star
66

node-sphinxdoc

document node project with sphinx
TypeScript
3
star
67

instant-wordpress

Skip the installation process of WordPress by using environment variables
Python
3
star
68

drone-postman

Test postman collection with Drone
Makefile
3
star
69

oooc

Pretty print structured data found in stdout
Perl
3
star
70

terraform

personal terraform scripts
HCL
3
star
71

rc-config

Load runtime configuration
JavaScript
3
star
72

windows-wifi-cracker

Show the passwords of all wifi accounts saved on a Windows machine
C#
3
star
73

spotawesome

Open source spot instance orchestrator
JavaScript
3
star
74

volback

backup and restore docker volumes
Go
3
star
75

drone-init

Batch initialize drone projects
Python
3
star
76

role-based-access-control

unopinionated role based access control
TypeScript
3
star
77

keycloak-theme

boilerplate keycloak theme
FreeMarker
3
star
78

generator-nestjs-prisma

yeoman generator for nestjs prisma
TypeScript
3
star
79

generator-yo-es6

A generator for ES6 yeoman generators
JavaScript
3
star
80

gnome-cairo

typescript bindings for gnome cairo
TypeScript
3
star
81

loopback4-hydra

loopback 4 integration with ory hydra
TypeScript
3
star
82

google-closure-library

google closure library for node and typescript
JavaScript
3
star
83

php-nginx-alpine-docker

Lightweight php nginx server on an alpine image
Nginx
3
star
84

crypto-review

cryptos from a developers point of view
3
star
85

node-gnumake

cross platform gnu make for nodejs
TypeScript
3
star
86

login-ui

front-end for ory hydra loopback 4 identity api
JavaScript
3
star
87

ghost-proxy

A rest API proxy for the Ghost blogging platform.
JavaScript
2
star
88

link-type-definitions

typescript definition package manager
TypeScript
2
star
89

kiss-date

keep it simple stupid date library
Makefile
2
star
90

velero-schedule-replicator

operator to replicate velero schedules across namespaces
Makefile
2
star
91

xmpp-ts

cross platform typescript library for xmpp built on xmpp.js
Makefile
2
star
92

dns-register

Register and unregister server with DNS providers
Go
2
star
93

generator-sphinx

Yeoman generator for sphinx
JavaScript
2
star
94

keycloak-gatekeeper-demo

secure an application with keycloak gatekeeper demo
2
star
95

event-ears

manage even listeners
Makefile
2
star
96

dockerinfo

Gives detailed information about a docker container's host.
JavaScript
2
star
97

docker-do-spaces

proxy digital ocean spaces
HTML
2
star
98

node-sigar

node bindings to sigar
C++
2
star
99

stripe-exporter

Stripe exporter for Prometheus
Go
2
star
100

bullean

boolean expression interpreter
Makefile
2
star