ts-simple-ast
TypeScript Compiler API wrapper. Provides a simple way to navigate and manipulate TypeScript and JavaScript code.
NOTICE - LIBRARY RENAMED!
ts-simple-ast has been renamed to ts-morph.
Library Development - Progress Update (06 January 2019)
View information on breaking changes in breaking-changes.md.
This library is still under early active development. Most common code manipulation/generation use cases are implemented, but there's still a lot of work to do.
Please open an issue if you find a feature missing or bug that isn't in the issue tracker.
Report
View a generated report on what nodes have been wrapped in the wrapped-nodes.md file.
Documentation
Work in progress: https://dsherret.github.io/ts-simple-ast/
Getting Started
- Installing
- Instantiating
- Adding source files
- Getting source files
- Navigating
- Manipulating
Example
import { Project } from "ts-simple-ast";
const project = new Project({
});
project.addExistingSourceFiles("src/**/*.ts");
const myClassFile = project.createSourceFile("src/MyClass.ts", "export class MyClass {}");
const myEnumFile = project.createSourceFile("src/MyEnum.ts", {
enums: [{
name: "MyEnum",
isExported: true,
members: [{ name: "member" }]
}]
});
const myClass = myClassFile.getClassOrThrow("MyClass");
myClass.getName();
myClass.hasExportKeyword();
myClass.isDefaultExport();
const myInterface = myClassFile.addInterface({
name: "IMyInterface",
isExported: true,
properties: [{
name: "myProp",
type: "number"
}]
});
myClass.rename("NewName");
myClass.addImplements(myInterface.getName());
myClass.addProperty({
name: "myProp",
initializer: "5"
});
project.getSourceFileOrThrow("src/ExistingFile.ts").delete();
project.save();
const compilerNode = myClassFile.compilerNode;
Or navigate existing compiler nodes created with the TypeScript compiler (the ts
named export is the TypeScript compiler):
import { createWrappedNode, ClassDeclaration, ts } from "ts-simple-ast";
const classNode: ts.ClassDeclaration = ...;
const classDec = createWrappedNode(classNode) as ClassDeclaration;
const firstProperty = classDec.getProperties()[0];
Resources