Skip to content

Repository files navigation

ts-morph

npm versionBuild StatusCoverage Statusstable

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

Formerly ts-simple-ast.

Library Development - Progress Update (20 April 2019)

There were a lot of breaking changes in v2.0. Please review breaking-changes.md and see the changelog for information on new features.

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";// initializeconstproject=newProject({// 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 filesproject.addExistingSourceFiles("src/**/*.ts");constmyClassFile=project.createSourceFile("src/MyClass.ts","export class MyClass {}");constmyEnumFile=project.createSourceFile("src/MyEnum.ts",{statements: [{kind: StructureKind.Enum,name: "MyEnum",isExported: true,members: [{name: "member"}]}]});// get informationconstmyClass=myClassFile.getClassOrThrow("MyClass");myClass.getName();// returns: "MyClass"myClass.hasExportKeyword();// returns: truemyClass.isDefaultExport();// returns: false// manipulateconstmyInterface=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 aboveproject.save();// get underlying compiler node from the typescript AST from any nodeconstcompilerNode=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 objectconstclassNode: ts.ClassDeclaration= ...;// create and use a wrapped nodeconstclassDec=createWrappedNode(classNode)asClassDeclaration;constfirstProperty=classDec.getProperties()[0];// ... do more stuff here ...

Resources

About

TypeScript Compiler API wrapper for static analysis and programmatic code changes.

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages