# ts-simple-ast

> TypeScript compiler wrapper for static analysis and code manipulation.

Latest version **21.0.4** (published 2019-02-02) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install ts-simple-ast
pnpm add ts-simple-ast
yarn add ts-simple-ast
bun add ts-simple-ast
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 21.0.4 |
| Published | 2019-02-02 |
| First published | 2017-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 1.9 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6179 |
| Author | David Sherret |
| Maintainers | dsherret |
| Keywords | typescript, ast, static analysis, code generation, code refactor |

## Links

- npm: https://www.npmjs.com/package/ts-simple-ast
- Repository: https://github.com/dsherret/ts-simple-ast
- Homepage: https://github.com/dsherret/ts-simple-ast#readme
- Issues: https://github.com/dsherret/ts-simple-ast/issues
- npm.io page: https://npm.io/package/ts-simple-ast

## Dependencies (9)

- [tslib](https://npm.io/package/tslib.md) ^1.9.0
- [globby](https://npm.io/package/globby.md) ^8.0.1
- [fs-extra](https://npm.io/package/fs-extra.md) ^7.0.0
- [multimatch](https://npm.io/package/multimatch.md) ^2.1.0
- [typescript](https://npm.io/package/typescript.md) ^3.0.1
- [glob-parent](https://npm.io/package/glob-parent.md) ^3.1.0
- [is-negated-glob](https://npm.io/package/is-negated-glob.md) ^1.0.0
- [code-block-writer](https://npm.io/package/code-block-writer.md) ^7.2.2
- [@dsherret/to-absolute-glob](https://npm.io/package/@dsherret/to-absolute-glob.md) ^2.0.2

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 21.0.4 (latest) — 2019-02-02
- 21.0.3 — 2019-01-31
- 21.0.2 — 2019-01-28
- 21.0.1 — 2019-01-28
- 21.0.0 — 2019-01-12
- 20.0.0 — 2018-12-08
- 19.1.0 — 2018-12-04
- 19.0.0 — 2018-11-14
- 18.1.0 — 2018-11-11
- 18.0.1 — 2018-11-10
- 18.0.0 — 2018-10-28
- 17.1.1 — 2018-10-20
- 17.1.0 — 2018-10-14
- 17.0.1 — 2018-10-13
- 17.0.0 — 2018-10-12
- … 231 more at https://npm.io/package/ts-simple-ast/versions

## README

ts-simple-ast
=============

[![npm version](https://badge.fury.io/js/ts-simple-ast.svg)](https://badge.fury.io/js/ts-simple-ast)
[![Build Status](https://travis-ci.org/dsherret/ts-simple-ast.svg?branch=master)](https://travis-ci.org/dsherret/ts-simple-ast)
[![Coverage Status](https://coveralls.io/repos/dsherret/ts-simple-ast/badge.svg?branch=master&service=github)](https://coveralls.io/github/dsherret/ts-simple-ast?branch=master)
[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)

[TypeScript](https://github.com/Microsoft/TypeScript) Compiler API wrapper. Provides a simple way to navigate and manipulate TypeScript and JavaScript code.

## NOTICE - LIBRARY RENAMED!

ts-simple-ast has been renamed to [ts-morph](https://www.npmjs.com/package/ts-morph).

## Library Development - Progress Update (06 January 2019)

View information on breaking changes in [breaking-changes.md](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](wrapped-nodes.md) file.

## Documentation

Work in progress: https://dsherret.github.io/ts-simple-ast/

## Getting Started

1. [Installing](https://dsherret.github.io/ts-simple-ast/)
2. [Instantiating](https://dsherret.github.io/ts-simple-ast/setup/)
3. [Adding source files](https://dsherret.github.io/ts-simple-ast/setup/adding-source-files)
4. [Getting source files](https://dsherret.github.io/ts-simple-ast/navigation/getting-source-files)
5. [Navigating](https://dsherret.github.io/ts-simple-ast/navigation/example)
6. [Manipulating](https://dsherret.github.io/ts-simple-ast/manipulation/)

## Example

```ts
import { Project } from "ts-simple-ast";

// 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-simple-ast/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", {
    enums: [{
        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):

```ts ignore-error: 1109
import { createWrappedNode, ClassDeclaration, ts } from "ts-simple-ast";

// 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

* [AST Viewers](https://dsherret.github.io/ts-simple-ast/setup/ast-viewers)

---
_Source: https://npm.io/package/ts-simple-ast · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
