Package Exports
- ts-simple-ast
- ts-simple-ast/dist/fileSystem
- ts-simple-ast/dist/utils
- ts-simple-ast/dist/utils/getCodeBlockWriter
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 TypeScript and JavaScript code.
Library Development - Progress Update (21 January 2018)
Navigation through statements and expressions is mostly implemented thanks to @dicarlo2! I'm going to soon use this library to identify and make a list of missing navigation features, but it shouldn't be too much at this point (see #93 and wrapped-nodes.md).
Most common 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/
Getting Started
Example
import Ast from "ts-simple-ast";
// add source files to ast
const ast = new Ast();
const sourceFile = ast.createSourceFile("MyFile.ts", "enum MyEnum {}\nlet myEnum: MyEnum;\nexport default MyEnum;");
ast.addExistingSourceFiles("**/folder/**/*.ts");
ast.createSourceFile("misc.ts", {
classes: [{
name: "SomeClass",
isExported: true
}],
enums: [{
name: "SomeEnum",
isExported: true,
members: [{ name: "member" }]
}]
});
// get information from ast
const enumDeclaration = sourceFile.getEnumOrThrow("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 asynchronously to MyFile.ts
// get underlying compiler node from the typescript AST from any node
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";
// some code that creates a class declaration (could be any kind of ts.Node)
const classNode: ts.ClassDeclaration = ...;
// create and use a wrapped node
const classDec = createWrappedNode(classNode) as ClassDeclaration;
const firstProperty = classDec.getProperties()[0];
// ... do more stuff here ...