24.0.0 • Published 3 months ago

ts-morph v24.0.0

Weekly downloads
344,725
License
MIT
Repository
github
Last release
3 months ago

ts-morph

npm version CI stable

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

Formerly ts-simple-ast.

Overview

Main Features

  1. Wraps the compiler API objects to provide helper methods for getting information and programmatically changing files.
  2. Allows falling back to the compiler API objects if necessary (ex. classDeclaration.compilerNode or typeChecker.compilerObject).
  3. All changes are kept in memory (including file and directory moves) until specifying to save to the underlying file system.
  4. Changes are made to the text and wrapped nodes can be held on to between manipulations.

Getting Started

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

Library Progress

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.

Example

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

// initialize
const project = new Project({
    // Optionally specify compiler options, tsconfig.json, in-memory 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://ts-morph.com/setup/
});

// add source files
project.addSourceFilesAtPaths("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
await 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 ...
@martin-yin/tool-cabinet-cli@neuvernetzung/cms@neuvernetzung/cms-cli@triplex/server@fernapi/fern-typescriptbetter-vercel@devopstation/graph@gmnx/gmnx-util@hailstonelabs/configx-wombat@fern-typescript/type-generator@fern-typescript/abstract-error-class-generator@fern-typescript/abstract-schema-generator@fern-typescript/sdk-generator@fern-typescript/sdk-inlined-request-schema-generator@fern-typescript/sdk-service-declaration-handler@fern-typescript/service-generator@fern-typescript/union-generator@fern-typescript/union-schema-generator@fern-typescript/type-reference-converters@fern-typescript/type-reference-example-generator@fern-typescript/type-schema-generator@fern-typescript/types-v2@fern-typescript/request-wrapper-generator@fern-typescript/sdk-client-class-generator@fern-typescript/sdk-declaration-handler@fern-typescript/sdk-endpoint-type-schemas-generator@fern-typescript/sdk-error-generator@fern-typescript/sdk-error-schema-generator@fern-typescript/client-v2@fern-typescript/errors-v2@fern-typescript/express-endpoint-type-schemas-generator@fern-typescript/express-error-generator@fern-typescript/express-error-schema-generator@fern-typescript/express-generator@fern-typescript/express-inlined-request-schema-generator@fern-typescript/commons-v2@fern-typescript/declaration-handler@fern-typescript/encoders@fern-typescript/endpoint-error-union-generator@fern-typescript/endpoint-type-schemas-generator@fern-typescript/generic-sdk-error-generators@fern-typescript/helper-utils@fern-typescript/inlined-request-schema-generator@fern-typescript/generic-error-generators@fern-typescript/generic-express-error-generators@fern-typescript/express-register-generator@fern-typescript/express-service-generator@fern-typescript/endpoint-types-generator@fern-typescript/environments-generator@fern-typescript/error-generator@fern-typescript/error-schema-generatorsan-ssr-target-jsnetless-dts-bundlegenerate-types@bumble/plop-mdtyoag@tomfrenken/generator@af_selim/spotexportmon@typelib/eslint-plugin-auto-import@nowtech/npm-prisma-tgql-nestgenerated-plugin-taro-modal-servicegenerated-plugin-taro-router-servicegenerator-express-microservice@nowtech/nestjs-prisma-generatorngx-usagehzero-schematicsempty-package-abcempty-package-abcdglue-js-documentation-builder@desmondrg/browsifier@desmondrg/common-cli@desmondrg/graphqlizer@desmondrg/nestifier@urbanshona/browsifier@fundora/model-libpareto-ts-validaterpareto-ts-validatortsg-tgjzanalysenode@ovrsea/graphql-schema-checkerpublishable-techwink-creater@golee/authlibv2scan-contextmap@fortezhuo/zod-prisma@infinitebrahmanuniverse/nolb-ts-m@startcore/startcore@cobraz-dev/wsdl-tsclient@unrevealed/cli@triplex/ts-morphsmb-nest-cliserviceclub_sharedmichaelangrivera-ts-restgews@everything-registry/sub-chunk-2978wzm@ryanke/openapi-typescriptsnbc-paas-schematicscpaas-schematics
24.0.0

3 months ago

23.0.0

6 months ago

22.0.0

10 months ago

21.0.1

1 year ago

21.0.0

1 year ago

19.0.0

2 years ago

20.0.0

1 year ago

18.0.0

2 years ago

17.0.1

2 years ago

17.0.0

2 years ago

16.0.0

2 years ago

15.1.0

3 years ago

15.0.0

3 years ago

14.0.0

3 years ago

13.0.3

3 years ago

13.0.2

3 years ago

13.0.0

3 years ago

13.0.1

3 years ago

12.2.0

3 years ago

12.1.0

3 years ago

12.0.0

3 years ago

11.0.3

3 years ago

11.0.2

3 years ago

11.0.0

4 years ago

10.1.0

4 years ago

10.0.2

4 years ago

10.0.1

4 years ago

10.0.0

4 years ago

9.1.0

4 years ago

9.0.0

4 years ago

8.2.0

4 years ago

8.1.2

4 years ago

8.1.1

4 years ago

8.1.0

4 years ago

8.0.0

4 years ago

7.3.0

4 years ago

7.2.0

4 years ago

7.1.3

4 years ago

7.1.2

5 years ago

7.1.1

5 years ago

7.1.0

5 years ago

7.0.3

5 years ago

7.0.2

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

6.0.3

5 years ago

1.3.5

5 years ago

6.0.1

5 years ago

6.0.0

5 years ago

6.0.2

5 years ago

5.0.0

5 years ago

4.3.3

5 years ago

4.3.2

5 years ago

4.3.1

5 years ago

4.3.0

5 years ago

4.2.0

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.3.0

6 years ago

1.3.4

6 years ago

2.2.0

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.4-rc

6 years ago

2.0.3-rc

6 years ago

2.0.2-rc

6 years ago

2.0.1-rc

6 years ago

2.0.0-rc

6 years ago

2.0.0

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago