npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@b08/type-parser

v3.0.1

Published

typescript file parser, produces type information in convenient form

Downloads

78

Readme

@b08/type-parser, seeded from @b08/library-seed, library type: feature

Library is made to be used for code generation. Before this, every generator I made, had its own parser, parsing its own things, intersecting on some of the functionality. From now on, parser is extracted into its own library and every new generator will be using it.

Usage

Easiest way to use this is from gulp task, with @b08/gulp-transform module

import { transform } from "@b08/gulp-transform";
import { parseTypes } from "@b98/type-parser";

gulp.src(files)
  .pipe(transform(file => generateSomething(parseTypes(file)); )
  .pipe(gulp.dest())

This way, generator input parameter should be of TypesModel type.
If the file is read with another tool, to parse the file, method parseTypes should be provided with object containing file contents, file name and folder.
There is also second "Options" parameter, containing one field(so far):

  1. aliasMap: replacement aliases, example { "app": "./src/app" } - this will transform absolute reference in parsed file "app/folder/file" to "c:/{project folder}/src/app"

Types Model

Model will contain the following parsed entities:

  1. types
  2. constants
  3. enums
  4. interfaces
  5. classes
  6. functions

All types should be in the root of the file to be parsed. I.e. types inside any sort of brackets will not be included. Also, Types not prefixed with export keyword will not be included.

Type id

Each parsed entity contains definition, or id.
Definition has name and folder/file where type is defined

Types

Parsed type contains only type id.
Type should look like this to be properly parsed:

export type MyType = string;

Constants

Same as types, constant contains only type id.

const a: number = 2;

Enums

Parsed enum contains a list of value names. Values ("b" and 5) are not included.
Enum should look like this to be properly parsed:

export enum MyEnum {
  a,
  b = "b",
  c = 5
}

Functions

Function contains list of parameters and return type. Parameter contains parameter name, type name and definitions of imported types included in type name in case of a composed type.
For example, parameter a: MyType<string, MySecondType> has 2 definitions, for MyType and for MySecondType. If those 2 are defined in the same file, their definitions will have the same file and folder as currently parsed file. If type is imported, then imports will be used to calculate absolute path where this imported type is defined.

Function should look like this to be parsed:

export function a(b: string, c: MyType<string>): MyOtherType<MyThirdType> {
  console.log("hello world");
  return null;
}

Interfaces

Interface contains list of defined fields and functions. Field contains field name, field type name and definitions of imported types included in type name.
Functions are the same as separately defined functions.
Also interface will contain a list of other types it extends, if any.

Interface should be defined following way to be parsed:

import { MyOtherType } from "../myOther.type";

export interface MyType extends MySecondType {
   // simple field
   a: string;

   // field with composed imported type
   b: MyOtherType<string>

   // function
   c: (f1: string) => number;
}

Classes

Class, same as interface, contains a list of fields, functions, and types it extends. In addition class has interfaces it implements and a constructor. Fields and functions of the class should be explicitly defined as public to be included. Also, if constructor fields are defined public, they will be included as fields.

Class should be defined like this to be parsed:

export class MyClass implements MyInterface {
  public b: number;

  constructor(public a: string) {
  }

  public c(a: string): void {
    console.log(a);
  }
}

Extra function to use on the model

exportedTypes - returns a list of exported type names
isMonotype - returns true if type is not generic
getRelativePath - path to import one type from the other file's folder