3.2.2 • Published 3 years ago

tinspector v3.2.2

Weekly downloads
342
License
MIT
Repository
-
Last release
3 years ago

tinspector

TypeScript type inspector library

Build Status Coverage Status

Motivation

tinspector is a type inspector library, it can parse all exported functions and classes inside a module into Abstract Syntax Tree.

How to Use It

Example you have a file with classes like below:

//filename: src/mock.ts
export function myFun(firstPar: string, secondPar: string) { }
export function myOtherFun() { }

export class MyClass {
    myMethod(firstPar: string, secondPar: string) { }
    myOtherMethod(){}
}

You can retrieve AST from above module using:

//filename: src/index.ts
import {reflect} from "./reflect"

const result = reflect("./mock")

The result will be like below:

{
    type: 'Object',
    name: 'module',
    members: [{
        type: 'Function',
        name: 'myFun',
        parameters:
            [{ type: 'Parameter', name: 'firstPar' },
            { type: 'Parameter', name: 'secondPar' }]
    },
    { type: 'Function', name: 'myOtherFun', parameters: [] },
    {
        type: 'Class',
        name: 'MyClass',
        methods:
            [{
                type: 'Function',
                name: 'myMethod',
                parameters:
                    [{ type: 'Parameter', name: 'firstPar' },
                    { type: 'Parameter', name: 'secondPar' }]
            },
            { type: 'Function', name: 'myOtherMethod', parameters: [] }]
    }]
}

The parameter of the reflect method is the path of the module that will be parsed, it is respect the JavaScript import naming such as absolute "./module", "/path/of/module", relative "../../module" or global "module"

tinspector can handle TypeScript style decorator properly

import {decorateClass, decorateMethod, decorateParameter} from "./reflect"

@decorateClass({ url: "/animal" })
export class AnimalClass {
    @decorateMethod({ url: "/get" })
    myMethod(@decorateParameter({ required: true }) firstPar: string, @decorateParameter({ required: false }) secondPar: string) { }
    myOtherMethod(@decorateParameter({ required: true }) par1: string, par2: string) { }
}

The reflection result is like below:

{
    type: 'Class',
    name: 'AnimalClass',
    methods:
        [{
            type: 'Function',
            name: 'myMethod',
            parameters:
                [{
                    type: 'Parameter',
                    name: 'firstPar',
                    decorators:
                        [{ required: true }]
                },
                {
                    type: 'Parameter',
                    name: 'secondPar',
                    decorators:
                        [{ required: false }]
                }],
            decorators:
                [{ url: '/get' }]
        },
        {
            type: 'Function',
            name: 'myOtherMethod',
            parameters:
                [{
                    type: 'Parameter',
                    name: 'par1',
                    decorators:
                        [{ required: true }]
                },
                { type: 'Parameter', name: 'par2', decorators: [] }],
            decorators: []
        }],
    decorators:
        [{ url: '/animal' }]
}
3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.8

4 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.10

4 years ago

2.2.9

4 years ago

2.2.8

4 years ago

2.2.7

5 years ago

2.2.6

5 years ago

2.2.5

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

2.0.0-3

5 years ago

2.0.0-2

5 years ago

2.0.0-1

5 years ago

2.0.0-0

5 years ago