@plumjs/reflect
v1.4.1
Published
TypeScript reflection and type inspector library
Downloads
9
Readme
tinspector
TypeScript type inspector library
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' }]
}