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

a-javascript-and-typescript-documentation-generator-based-on-typescript-compiler

v0.0.1-2

Published

A documentation generator that understands ES modules and infers types. It is powered by the TypeScript compiler, lol.

Downloads

5

Readme

a-javascript-and-typescript-documentation-generator-based-on-typescript-compiler

A documentation generator that understands ES modules and infers types. It is powered by the TypeScript compiler, which also powers the JavaScript and TypeScript IntelliSense.

This means your documented code will be picked up by IDEs (such as Visual Studio, VS Code, or Atom-IDE) to provide auto-completions the same way!

Instead of “looking for functions/variables/constants/classes declared in a file,” it looks at each public module and walks through each exported symbol. This means it understands modules that re-exports things like this:

// index.ts
import * as doc from './doc'

export { doc }
export { default as generateDocs, GenerateDocsResult } from './generateDocs'

And generates a documentation like this:

Generated docs

It also works with JavaScript file. In this case, TypeScript compiler reads the type annotation from JSDog tags.

/**
 * Calculates the accuracy.
 * @param {number[]} count The 3-element array containing METICULOUS judgment,
 * PRECISE judgment, and GOOD judgment, respectively.
 * @param {number} total The total amount of possible judgments that may be given.
 * @returns the accuracy number, from 0 to 1
 */
export function calculateAccuracy (count, total) {
  return (count[0] + count[1] * 0.8 + count[2] * 0.5) / total
}

Which generates a documentation like this (note: the return type is inferred by the compiler):

Generated docs

Inspiration

  • Visual Studio’s JavaScript IntelliSense (based on TypeScript) is very smart. It is based on TypeScript’s Language Service. However, most current documentation tools don’t benefit from that smart-ness.

  • Most tools don’t infer type from source code and only reads the JSDoc tags.

  • Some tools requires you to specify types and documentation in a proprietary format, which is not compatible with the IntelliSense.

  • The thing I found closest to what I want is TypeDoc. It does use TypeScript compiler, but processes modules by walking through the AST, instead of looking at all exported symbols of a module (e.g. using TypeScript’s getExportsOfModule()).

  • Many projects has an index.js file which only re-exports things from other modules (e.g. export { createStore } from './createStore'). However, TypeDoc does not support it yet.

  • I don’t want to document a file; I want to document my module’s API surface area.

    Given that I import a module, what is there for me to use?

  • VS Code’s IntelliSense tries its best to understand your code and infer stuff, instead of forcing you to document everything in its own syntax. Documentation generator tools should do the same!

  • The plan: Learn how TypeScript Language Service achieves that smartness, then create a documentation generator (loosely) based on that knowledge.

Process

                `ts.createProgram()`
                        |
+-------------------+   |   +--------------------+
|    Input files    | ----> | TypeScript program |
| (.js, .ts, .d.ts) |       |   (`ts.Program`)   |
+-------------------+       +--------------------+
                                | walk the modules and symbols
                                V
     +-----------------------------------------+
     | Documentation model (JSON-serializable) |
     +-----------------------------------------+
         | generate             | export
         V                      V
     +-------------------+   +---------------+
     | Web pages (.html) |   | Model (.json) |
     +-------------------+   +---------------+
  • Input files are fed into TypeScript compilers, which will resolve all modules, infer types, and lots of super-cool stuff. It results in a ts.Program object.

  • a-javascript-and-typescript-documentation-generator-based-on-typescript-compiler goes through the modules, and collect documentation data, into a JSON-serializable model.

  • Then we can generate a web page / readme file / whatever out of it!

Usage

Warning: This is still in development, and it might not work well. Please be prepared to dive into the source code.

Installation:

yarn global add a-javascript-and-typescript-documentation-generator-based-on-typescript-compiler

Generate HTML documentation:

a-javascript-and-typescript-documentation-generator-based-on-typescript-compiler src/index.js --html docs/api

Generate JSON to stdout:

a-javascript-and-typescript-documentation-generator-based-on-typescript-compiler src/index.js --json docs/api.json

JSON model is defined at src/generator/doc.ts.

development

  1. Clone this project.

  2. Install the dependencies:

    yarn
  3. To generate a documentation JSON:

    yarn dev:generate <inputfile.ts ...>
  4. To view and develop the documentation web page:

    ./node_modules/.bin/ts-node --doc.input=<inputfile.ts>