JSPM

ts-simple-ast

6.10.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 22909
  • Score
    100M100P100Q27143F
  • License MIT

TypeScript compiler wrapper for AST navigation and code generation.

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

npm version Build Status Coverage Status stable

TypeScript compiler wrapper. Provides a simple way to navigate and manipulate TypeScript and JavaScript code.

Library Development - Progress Update (26 January 2018)

A report has been generated, using this library, that identifies nodes and properties from the TypeScript compiler API that haven't been wrapped. This has helped identify gaps and shortcommings of the library. The report can be viewed in the wrapped-nodes.md file.

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

  1. Installing
  2. Instantiating
  3. Adding source files
  4. Getting source files
  5. Navigating
  6. Manipulating

Example

import Ast from "ts-simple-ast";

// add source files to ast
const ast = new Ast();
const sourceFile = ast.createSourceFile("MyClass.ts", "class MyClass {}\nlet myClass: MyClass;\nexport default MyClass;");
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 classDeclaration = sourceFile.getClassOrThrow("MyClass");
classDeclaration.getName();          // returns: "MyClass"
classDeclaration.hasExportKeyword(); // returns: false
classDeclaration.isDefaultExport();  // returns: true

// manipulate ast
classDeclaration.rename("NewName");
classDeclaration.addProperty({
    name: "myProp",
    initializer: "5"
});
classDeclaration.setIsDefaultExport(false);

// result
sourceFile.getFullText(); // returns: "class NewName {\n    myProp = 5;\n}\nlet myClass: MyClass;\n"
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 ...

Resources