Skip to content

Quramy/astsx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

astsx

Write TypeScript AST with TSX

import ts from 'typescript';
import Astsx from 'astsx';

const Id = () => <identifier text="hoge" />;

const Func = () => (
  <functionDeclaration
    name="foo"
    parameters={[]}
    body={
      <block
        statements={[
          <return expression={<Id />} />
        ]}
        multiLine
      />
    }
  />
);

const printer = ts.createPrinter();
const src = ts.createSourceFile('main.ts', '', ts.ScriptTarget.Latest);

console.log(printer.printNode(ts.EmitHint.Unspecified, Func(), src));

Getting started

$ npm i astsx -D
/* tsconfig.json */
{
  "compilerOptions": {
    "jsx": "react",
    "jsxFactory": "Astsx.$"
  }
}

Disclaimer

For now, return type of TSX expression is inferred to not subtype of ts.Node but any type. It's TypeScript JSX.Element type inference mechanism issue. See microsoft/TypeScript#21699, if you want detail.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published