ts-morph
TypeScript compiler wrapper for static analysis and code manipulation.
Last updated 8 years ago by dsherret .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install ts-morph 
SYNC missed versions from official npm registry.

ts-morph

npm version Build Status Coverage Status stable

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

Formerly ts-simple-ast.

Library Development - Progress Update (02 February 2019)

The library has been renamed from ts-simple-ast to ts-morph. The version was also reset back to 1.0.0 with this change.

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-morph/

Getting Started

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

Example

import { Project, StructureKind } from "ts-morph";

// initialize
const project = new Project({
    // Optionally specify compiler options, tsconfig.json, virtual file system, and more here.
    // If you initialize with a tsconfig.json, then it will automatically populate the project
    // with the associated source files.
    // Read more: https://dsherret.github.io/ts-morph/setup/
});

// add source files
project.addExistingSourceFiles("src/**/*.ts");
const myClassFile = project.createSourceFile("src/MyClass.ts", "export class MyClass {}");
const myEnumFile = project.createSourceFile("src/MyEnum.ts", {
    statements: [{
        kind: StructureKind.Enum,
        name: "MyEnum",
        isExported: true,
        members: [{ name: "member" }]
    }]
});

// get information from ast
const myClass = myClassFile.getClassOrThrow("MyClass");
myClass.getName();          // returns: "MyClass"
myClass.hasExportKeyword(); // returns: true
myClass.isDefaultExport();  // returns: false

// manipulate ast
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();

// asynchronously save all the changes above
project.save();

// get underlying compiler node from the typescript AST from any node
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-morph";

// some code that creates a class declaration using the ts object
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

Current Tags

  • 28.0.0                                ...           latest (8 months ago)
  • 2.0.4-rc                                ...           rc (8 years ago)

89 Versions

  • 28.0.0                                ...           8 months ago
  • 27.0.2                                ...           a year ago
  • 27.0.0                                ...           a year ago
  • 26.0.0                                ...           2 years ago
  • 25.0.1                                ...           2 years ago
  • 25.0.0                                ...           2 years ago
  • 24.0.0                                ...           2 years ago
  • 23.0.0                                ...           2 years ago
  • 22.0.0                                ...           3 years ago
  • 21.0.1                                ...           3 years ago
  • 21.0.0                                ...           3 years ago
  • 20.0.0                                ...           3 years ago
  • 19.0.0                                ...           3 years ago
  • 18.0.0                                ...           4 years ago
  • 17.0.1                                ...           4 years ago
  • 17.0.0                                ...           4 years ago
  • 16.0.0                                ...           4 years ago
  • 15.1.0                                ...           4 years ago
  • 15.0.0                                ...           5 years ago
  • 14.0.0                                ...           5 years ago
  • 13.0.3                                ...           5 years ago
  • 13.0.2                                ...           5 years ago
  • 13.0.1                                ...           5 years ago
  • 13.0.0                                ...           5 years ago
  • 12.2.0                                ...           5 years ago
  • 12.1.0                                ...           5 years ago
  • 12.0.0                                ...           5 years ago
  • 11.0.3                                ...           5 years ago
  • 11.0.2                                ...           5 years ago
  • 11.0.0                                ...           6 years ago
  • 10.1.0                                ...           6 years ago
  • 10.0.2                                ...           6 years ago
  • 10.0.1                                ...           6 years ago
  • 10.0.0                                ...           6 years ago
  • 9.1.0                                ...           6 years ago
  • 9.0.0                                ...           6 years ago
  • 8.2.0                                ...           6 years ago
  • 8.1.2                                ...           6 years ago
  • 8.1.1                                ...           6 years ago
  • 8.1.0                                ...           6 years ago
  • 8.0.0                                ...           6 years ago
  • 7.3.0                                ...           6 years ago
  • 7.2.0                                ...           6 years ago
  • 7.1.3                                ...           6 years ago
  • 7.1.2                                ...           6 years ago
  • 7.1.1                                ...           6 years ago
  • 7.1.0                                ...           7 years ago
  • 7.0.3                                ...           7 years ago
  • 7.0.2                                ...           7 years ago
  • 7.0.1                                ...           7 years ago
  • 7.0.0                                ...           7 years ago
  • 6.0.3                                ...           7 years ago
  • 1.3.5                                ...           7 years ago
  • 6.0.2                                ...           7 years ago
  • 6.0.1                                ...           7 years ago
  • 5.0.0                                ...           7 years ago
  • 4.3.3                                ...           7 years ago
  • 4.3.2                                ...           7 years ago
  • 4.3.1                                ...           7 years ago
  • 4.3.0                                ...           7 years ago
  • 4.2.0                                ...           7 years ago
  • 4.1.1                                ...           7 years ago
  • 4.1.0                                ...           7 years ago
  • 4.0.1                                ...           7 years ago
  • 4.0.0                                ...           7 years ago
  • 3.1.3                                ...           7 years ago
  • 3.1.2                                ...           7 years ago
  • 3.1.1                                ...           7 years ago
  • 3.1.0                                ...           7 years ago
  • 3.0.0                                ...           7 years ago
  • 2.3.0                                ...           7 years ago
  • 1.3.4                                ...           8 years ago
  • 2.2.0                                ...           8 years ago
  • 2.1.2                                ...           8 years ago
  • 2.1.1                                ...           8 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.1                                ...           8 years ago
  • 2.0.4-rc                                ...           8 years ago
  • 2.0.3-rc                                ...           8 years ago
  • 2.0.2-rc                                ...           8 years ago
  • 2.0.1-rc                                ...           8 years ago
  • 2.0.0-rc                                ...           8 years ago
  • 1.3.3                                ...           8 years ago
  • 1.3.2                                ...           8 years ago
  • 1.3.1                                ...           8 years ago
  • 1.3.0                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 124
Today 0
This Week 0
This Month 100
Last Day 0
Last Week 11
Last Month 23
Dependencies (8)
Dev Dependencies (35)

© 2010 - cnpmjs.org x YWFE | Home | YWFE