Skip to content
TypeScript Compiler API wrapper for static analysis and programmatic code changes.
Branch: master
Clone or download
Latest commit 07ee7bd Jul 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE chore: Rename library. Feb 2, 2019
.vscode chore: Add vscode launch scripts. Jun 29, 2019
docs docs: Fix documentation compile errors. Jun 29, 2019
lib feat: #657 - Ability to specify the script kind when creating a sourc… Jul 3, 2019
scripts chore: Fix build error. Jul 13, 2019
src fix: #665 - Getting descendants where jsx was used in a non-jsx file … Jul 17, 2019
.gitignore chore: Add vscode launch scripts. Jun 29, 2019
.npmignore chore: Update internal scripts to use ts-morph 2.0. Apr 12, 2019
.travis.yml chore: Attempt to fix ci again. Jun 29, 2019
CHANGELOG.md chore(release): 3.1.3 Jul 18, 2019
CODE_OF_CONDUCT.md chore: Add code of conduct. Oct 28, 2018
CONTRIBUTING.md chore: Rename library. Feb 2, 2019
LICENSE chore: Update license year. Jan 6, 2019
breaking-changes.md refactor: Remove `renameNode` and `renameLocations` from the language… May 31, 2019
mocha.opts chore: Update performance tests to add remove (#610) Apr 13, 2019
package.json chore(release): 3.1.3 Jul 18, 2019
readme.md chore: Update readme.md for new release. Jun 29, 2019
tsconfig.common.json reactor: #574 - Target ES2015. Mar 22, 2019
tsconfig.declarations.json feat: #456 - Relax ts dependency to 3.0.1 and 3.0.3. Oct 6, 2018
tsconfig.json feat: #456 - Relax ts dependency to 3.0.1 and 3.0.3. Oct 6, 2018
tsconfig.scripts.json feat: #383 - Update to TS 3.0 Aug 6, 2018
tslint.json feat: Replace `StatementedNode` with new `.statements` property / Add… Mar 18, 2019
wrapped-nodes.md docs: Fix documentation compile errors. Jun 29, 2019
yarn.lock chore: Update to ts-morph 3.1.2 (internally) Jul 16, 2019

readme.md

ts-morph

npm version Build Status Coverage Status stable

TypeScript Compiler API wrapper. Provides an easier way to navigate and manipulate TypeScript and JavaScript code.

Formerly ts-simple-ast.

Library Development - Progress Update (29 June 2019)

v3.0 is now released! Always review breaking-changes.md before upgrading and see the changelog for information on new features.

This library is still under 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, bug, or question 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
const myClass = myClassFile.getClassOrThrow("MyClass");
myClass.getName();          // returns: "MyClass"
myClass.hasExportKeyword(); // returns: true
myClass.isDefaultExport();  // returns: false

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

You can’t perform that action at this time.