Package Exports
- ts-simple-ast
- ts-simple-ast/dist/utils
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ts-simple-ast) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ts-simple-ast
TypeScript compiler wrapper. Provides a simple way to navigate and manipulate the TypeScript AST.
Library Development - Progress Update (03 June 2017)
- Most AST navigation is implemented, but still some missing. This library can be used to help you easily navigate the TypeScript compiler's AST.
- Code manipulation/generation is making steady progress, but it's not very usable at this point. Most common manipulation tasks should be done within a month.
Simple Layer
ts-simple-ast adds a layer over the compiler while still providing access to the underlying TypeScript compiler AST.
- Simple Layer - Provides a simple way for navigating and manipulating the AST.
- Compiler Layer - TypeScript compiler objects.
Changes made in the simple layer will be made to the underlying compiler layer.
Documentation
Work in progress: https://dsherret.github.io/ts-simple-ast/
Example
import Ast from "ts-simple-ast";
// add source files to 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.addSourceFile("misc.ts", {
classes: [{
name: "MyClass",
isExported: true
}],
enums: [{
name: "MyEnum",
isExported: true,
members: [{
name: "member"
}]
}]
});
// get information from ast
const enumDeclaration = sourceFile.getEnum("MyEnum")!;
enumDeclaration.getName(); // returns: "MyEnum"
enumDeclaration.hasExportKeyword(); // returns: false
enumDeclaration.isDefaultExport(); // returns: true
// manipulate ast
enumDeclaration.rename("NewName");
enumDeclaration.addMember({
name: "myNewMember"
});
enumDeclaration.setIsDefaultExport(false);
// result
sourceFile.getFullText(); // returns: "enum NewName {\n myNewMember\n}\nlet myEnum: NewName;"
sourceFile.save(); // save it to MyFile.ts
// get underlying compiler node from the typescript AST from any node
const sourceFileCompilerNode = sourceFile.node;