All Projects → dsherret → Ts Type Info

dsherret / Ts Type Info

Licence: mit
TypeScript AST and code generator [Deprecated]

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ts Type Info

Cgen
C/C++ source generation from an AST
Stars: ✭ 107 (+18.89%)
Mutual labels:  ast, code-generation
toast
Plugin-driven CLI utility for code generation using Go source as IDL
Stars: ✭ 52 (-42.22%)
Mutual labels:  ast, code-generation
Ts Morph
TypeScript Compiler API wrapper for static analysis and programmatic code changes.
Stars: ✭ 2,384 (+2548.89%)
Mutual labels:  ast, code-generation
Javaparser
Java 1-15 Parser and Abstract Syntax Tree for Java, including preview features to Java 13
Stars: ✭ 3,972 (+4313.33%)
Mutual labels:  ast, code-generation
Python3Generator
A toolkit to generate Python 3 source code from Pharo.
Stars: ✭ 25 (-72.22%)
Mutual labels:  ast, code-generation
Spoon
Spoon is a metaprogramming library to analyze and transform Java source code (up to Java 15). 🥄 is made with ❤️, 🍻 and ✨. It parses source files to build a well-designed AST with powerful analysis and transformation API.
Stars: ✭ 1,078 (+1097.78%)
Mutual labels:  ast, code-generation
Metadsl
Domain Specific Languages in Python
Stars: ✭ 71 (-21.11%)
Mutual labels:  code-generation
Gen
Type-driven code generation for Go
Stars: ✭ 1,246 (+1284.44%)
Mutual labels:  code-generation
Snowflaqe
A dotnet CLI tool to work with GraphQL queries: static query verification, type checking and code generating type-safe clients for F# and Fable.
Stars: ✭ 69 (-23.33%)
Mutual labels:  code-generation
Mid
mid is a generic domain-specific language for generating code and documentation
Stars: ✭ 68 (-24.44%)
Mutual labels:  code-generation
Astq
Abstract Syntax Tree (AST) Query Engine
Stars: ✭ 89 (-1.11%)
Mutual labels:  ast
Uaiso
A multi-language parsing infrastructure with an unified AST
Stars: ✭ 86 (-4.44%)
Mutual labels:  ast
Lang C
Lightweight C parser for Rust
Stars: ✭ 77 (-14.44%)
Mutual labels:  ast
Xcode Ast Dump
Dump the AST of your Swift Xcode project
Stars: ✭ 71 (-21.11%)
Mutual labels:  ast
Apollo Prophecy
🔮 GraphQL error management made Easy, generate custom machine-readable errors for Apollo Client/Server from the CLI
Stars: ✭ 83 (-7.78%)
Mutual labels:  code-generation
Gowrtr
gowrtr is a library that supports golang code generation
Stars: ✭ 70 (-22.22%)
Mutual labels:  code-generation
Diffsitter
A tree-sitter based AST difftool to get meaningful semantic diffs
Stars: ✭ 89 (-1.11%)
Mutual labels:  ast
Json2java4idea
A JSON to Java conversion plugin for Intellij IDEA and AndroidStudio.
Stars: ✭ 69 (-23.33%)
Mutual labels:  code-generation
Show ast
An IPython notebook plugin for visualizing ASTs.
Stars: ✭ 76 (-15.56%)
Mutual labels:  ast
I Pascal
A free Object Pascal language plugin for IntelliJ IDEA
Stars: ✭ 85 (-5.56%)
Mutual labels:  ast

TSTypeInfo

npm version Build Status Coverage Status

TypeScript AST and code generator.

Uses the TypeScript Compiler API to get information about TypeScript code in an easy to use format.

npm install ts-type-info --save-dev

NOTICE - LIBRARY IS DEPRECATED

This library has been deprecated by ts-simple-ast:

https://github.com/dsherret/ts-simple-ast

Version 7.0 is the last final major release of ts-type-info.

There's certain issues with this library and to address them I had to do a complete redesign. ts-simple-ast wraps the typescript compiler rather than creating separate standalone objects. It's much more powerful and supports a lot more use cases.

AST

// V:\TestFile.ts

export class MyClass {
    myStringProperty: string;
    readonly myNumberProperty = 253;

    myMethod(myParameter: string) {
        return `Test: ${myParameter}`;
    }
}

Get the file info:

import * as TsTypeInfo from "ts-type-info";

const result = TsTypeInfo.getInfoFromFiles(["V:\\TestFile.ts"]);
const property = result.getFile("TestFile.ts")
    .getClass("MyClass")                            // get first by name
    .getProperty(p => p.defaultExpression != null); // or first by what matches

console.log(property.name);                   // myNumberProperty
console.log(property.type.text);              // number
console.log(property.defaultExpression.text); // 253
console.log(property.isReadonly);             // true

// or access the arrays directly
const myMethod = result.files[0].classes[0].methods[0];

console.log(myMethod.name); // myMethod

Code Generation

You can work with objects retrieved from the AST or start with your own new file definition:

import * as TsTypeInfo from "ts-type-info";

// create whatever you like at the start
const file = TsTypeInfo.createFile({
    classes: [{
        name: "MyClass",
        methods: [{
            name: "myMethod",
            parameters: [{ name: "myParam", type: "string" }],
            onBeforeWrite: writer => writer.write("// myMethod is here"),
            onWriteFunctionBody: writer => {
                writer.write(`if (myParam != null && myParam.length > 40)`).block(() => {
                    writer.write("alert(myParam)");
                });
                writer.newLine().write("return myParam;");
            }
        }]
    }]
});

// add to it later
const myClass = file.getClass("MyClass");
myClass.isAbstract = true;
myClass.addDecorator({
    name: "MyDecorator"
});

myClass.addProperty({
    name: "myProperty1",
    type: "string"
});
myClass.addProperty({
    name: "myProperty2",
    type: "number",
    defaultExpression: "4"
});

// write it out
console.log(file.write());

Outputs:

@MyDecorator
abstract class MyClass {
    myProperty1: string;
    myProperty2 = 4;

    // myMethod is here
    myMethod(myParam: string) {
        if (myParam != null && myParam.length > 40) {
            alert(myParam);
        }

        return myParam;
    }
}

Simple Examples

  • Strict Interfaces - Make all interface properties required and append "Strict" to the end of the interface name.

Full Examples

  • TsStateTestGenerator - Code generates functions for testing the state of objects. I used this in this ts-type-info and was able to discover a bunch of unreported bugs. I also no longer have to maintain a large portion of the project because the code is automatically generated for me. I highly recommend this.
  • TsCloneableGenerator - Code generates functions for cloning and filling objects.
  • TsObjectCreate - Code generates functions for creating objects with their types.
  • Server Bridge - Automatically generates client side code to communicate with the server from the server side code.

Include tsNodes

In case there's something you need from the compiler that's not implemented in this library, set the includeTsNodes option to true. This will include the TypeScript compiler nodes in the tsNode property of most objects.

import * as ts from "typescript";
import * as TsTypeInfo from "ts-type-info";

const result = TsTypeInfo.getInfoFromFiles(["V:\\TestFile.ts"], { includeTsNodes: true });
const typeChecker = result.getTypeChecker(); // ts.TypeChecker in case you need it
const myMethod = result.getFile("TestFile.ts").getClass("MyClass").getMethod("myMethod");
const myMethodNode = myMethod.tsNode as ts.MethodDeclaration;

console.log(myMethodNode.body.statements[0].getText()); // "return `Test: ${myParameter}`;"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].