Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

render abstract syntax trees with react

License

NotificationsYou must be signed in to change notification settings

clayrisser/react-ast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npmGitHub 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 rendererthat makes interacting with abstract syntax trees and rendering code a breeze.

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

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

Render Abstract Syntax Trees with React

Built by BitSpur

BitSpur offers premium Node and React development and support services. Get in touch atbitspur.com.

Features

  • works with babel ast
  • supports typescript

Installation

npm install --save react-ast

Dependencies

Usage

Render Code

importReact,{FC}from"react";import{Export,Expression,Function,Identifier,Import,Interface,JSX,ReactNode,Return,TypeAnnotation,TypeReference,Var,VarKind,}from"react-ast";constcode=awaitrender(<><Importdefault="React"imports={["FC"]}from="react"/><Export><Interfacename="HelloProps"/></Export><Varkind={VarKind.Const}typeAnnotation="FC<HelloProps>"name="Hello"><Functionarrowparams={[<IdentifiertypeAnnotation={<TypeAnnotation><TypeReferencename="HelloProps"/></TypeAnnotation>}>props</Identifier>,]}><Return><JSX/></Return></Function></Var><Expressionproperties="Hello.defaultProps">{{}}</Expression><Exportdefault><Identifier>Hello</Identifier></Export></>);console.log(code);

The rendered code

importReact,{FC}from"react";exportinterfaceHelloProps{}constHello:FC<HelloProps>=(props:HelloProps)=>{return<></>;};Hello.defaultProps={};exportdefaultHello;

Render AST

Sometimes you might want to render the ast instead ofrendering the code.

importReactfrom"react";import{ClassDeclaration,renderAst}from"react-ast";constast=renderAst(<Classname="Hello"/>);console.log(ast);

The rendered AST

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

Support

Submit anissue

Development

You can validate the AST atastexplorer.net with the following settings.

LanguageParserSettingsTransform
JavaScriptbabylon7babelv7

Enabled the followingbabylon7 plugins

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

Contributing

Review theguidelines for contributing

License

Apache-2.0 License

Clay Risser © 2019

Changelog

Review thechangelog

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 receivingLiberapay patrons


[8]ページ先頭

©2009-2025 Movatter.jp