ts-simple-ast
TypeScript compiler wrapper. Provides a simple way to navigate and manipulate TypeScript and JavaScript code.
Library Development - Progress Update (04 December 2017)
Most common navigation and code manipulation/generation use cases are implemented, but there's still a lot of work to do.
Please open an issue if find a feature missing or bug that isn't in the issue tracker.
Documentation
Work in progress: https://dsherret.github.io/ts-simple-ast/
Example
import Ast from "ts-simple-ast";
const ast = new Ast();
const sourceFile = ast.addSourceFileFromText("MyFile.ts", "enum MyEnum {}\nlet myEnum: MyEnum;\nexport default MyEnum;");
ast.addSourceFiles("folder/**/*{.d.ts,.ts}");
ast.addSourceFiles("otherFolder/file.ts", "specifyAnotherFile.ts", "orAnotherGlob/**/*.ts");
ast.addSourceFileFromStructure("misc.ts", {
classes: [{
name: "SomeClass",
isExported: true
}],
enums: [{
name: "SomeEnum",
isExported: true,
members: [{ name: "member" }]
}]
});
const enumDeclaration = sourceFile.getEnumOrThrow("MyEnum");
enumDeclaration.getName();
enumDeclaration.hasExportKeyword();
enumDeclaration.isDefaultExport();
enumDeclaration.rename("NewName");
enumDeclaration.addMember({
name: "myNewMember"
});
enumDeclaration.setIsDefaultExport(false);
sourceFile.getFullText();
sourceFile.save();
const sourceFileCompilerNode = sourceFile.compilerNode;
Or navigate existing compiler nodes created outside this library:
import * as ts from "typescript";
import {createWrappedNode, ClassDeclaration} from "ts-simple-ast";
const classNode: ts.ClassDeclaration = ...;
const classDec = createWrappedNode(classNode) as ClassDeclaration;
const firstProperty = classDec.getProperties()[0];
Resources