New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tinspector

Package Overview
Dependencies
Maintainers
1
Versions
194
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tinspector

TypeScript type inspector library

  • 1.4.0-alpha.caa0578b
  • npm
  • Socket score

Version published
Weekly downloads
495
increased by4.43%
Maintainers
1
Weekly downloads
 
Created
Source

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' }]
}

Keywords

FAQs

Package last updated on 15 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc