TSDoc Documentation

repository·main·Indexed 26 days ago

https://github.com/microsoft/tsdoc

TSDoc is a standardized documentation format for TypeScript code. This repository provides the core parser library (@microsoft/tsdoc), a configuration loader for tsdoc.json (@microsoft/tsdoc-config), and an ESLint plugin (eslint-plugin-tsdoc) to ensure documentation is structured, machine-readable, and consistent.

Tokens
7.9K
Snippets
14
Records
58
Agent score
89%

What's inside TSDoc

  1. Overview of TSDoc

    main

    TSDoc is a documentation standard for TypeScript. It provides a structured way to write comments that can be parsed by tools to generate documentation or perform static analysis.

    Key resources for users:

  2. Overview of tsdoc-build-rig

    main

    The tsdoc-build-rig package is a specialized build rig designed for projects within the TSDoc repository that utilize Heft for building. It extends @rushstack/heft-node-rig and @rushstack/heft-web-rig by adding configuration options specific to the TSDoc repository environment.

    Note: This package is not published to the NPM registry and is intended for internal use within the TSDoc repository.

  3. Available TSDoc Packages

    main

    This monorepo contains several packages for working with TSDoc:

    • @microsoft/tsdoc: The core parser library.
    • @microsoft/tsdoc-config: A loader for tsdoc.json configuration files.
    • eslint-plugin-tsdoc: An ESLint plugin to enforce TSDoc standards.
    • api-demo: Code samples demonstrating how to use the @microsoft/tsdoc parser.
  4. Understand TSDoc syntax and purpose

    main

    TSDoc is a standardized proposal for doc comments in TypeScript source files. It enables different tools to reliably extract documentation content from comments using a consistent syntax. A typical TSDoc comment uses tags like @remarks, @param, @returns, and @beta within a standard JSDoc-style block.

    export class Statistics {
      /**
       * Returns the average of two numbers.
       *
       * @remarks
       * This method is part of the {@link core-library#Statistics | Statistics subsystem}.
       *
       * @param x - The first input number
       * @param y - The second input number
       * @returns The arithmetic mean of `x` and `y`
       *
       * @beta
       */
      public static getAverage(x: number, y: number): number {
        return (x + y) / 2.0;
      }
    }
  5. Install and configure eslint-plugin-tsdoc

    main

    Use eslint-plugin-tsdoc to validate that TypeScript doc comments conform to the TSDoc specification.

    Prerequisites

    Ensure your TypeScript project is configured for ESLint using @typescript-eslint/parser and @typescript-eslint/eslint-plugin.

    Installation

    Install the plugin as a development dependency:

    npm install --save-dev eslint-plugin-tsdoc

    Configuration

    Add eslint-plugin-tsdoc to your ESLint plugins array and enable the tsdoc/syntax rule in your .eslintrc.js file.

    module.exports =  {
      plugins: [
        "@typescript-eslint/eslint-plugin",
        "eslint-plugin-tsdoc"
      ],
      extends:  [
        'plugin:@typescript-eslint/recommended'
      ],
      parser:  '@typescript-eslint/parser',
      parserOptions: {
        project: "./tsconfig.json",
        tsconfigRootDir: __dirname,
        ecmaVersion: 2018,
        sourceType: "module"
      },
      rules: {
        "tsdoc/syntax": "warn"
      }
    };
  6. Create a tsdoc.json configuration file

    main

    The tsdoc.json file is an optional configuration file used to define custom TSDoc tags. It should be placed in the same folder as your tsconfig.json. The loader searches upwards from the source file directory until it finds a folder containing tsconfig.json or package.json to locate the tsdoc.json file.

    To define custom tags, use the tagDefinitions field. Each definition requires a tagName and a syntaxKind (matching the TSDocTagDefinition API).

    You can also share tag definitions across multiple projects using the extends field. This field accepts a list of paths that will be merged into the current configuration. Note that extends paths use NodeJS module resolution: local paths must start with ./ to prevent them from being treated as NPM packages.

    {
      "$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
      "tagDefinitions": [
        {
          "tagName": "@myTag",
          "syntaxKind": "modifier"
        }
      ]
    }
  7. Run the @microsoft/tsdoc api-demo using NPM

    main

    Use this option to quickly try out the @microsoft/tsdoc API using the latest official release. This method does not symlink to local builds of the library.

    Note: If you are contributing a fix to the TSDoc repository, use the Rush method instead.

    $ cd ./api-demo
    # Use --no-package-lock to avoid conflicts with Rush
    $ npm install --no-package-lock
    $ npm run build
    
    # To run the simple demo (direct source parsing):
    $ npm run simple
    
    # To run the advanced demo (TypeScript AST extraction):
    $ npm run advanced
  8. Run the @microsoft/tsdoc api-demo using Rush

    main

    Use this option if you are a contributor submitting a pull request. This method links api-demo to your local build of the @microsoft/tsdoc library for testing and validation. This requires the Rush monorepo manager.

    Warning: After running rush install, the repository is in a "Rush-linked" state. Do not run npm install in this state. To return to standalone mode, run rush unlink && rush purge.

    # 1. Install Rush globally
    $ npm install -g @microsoft/rush
    
    # 2. Install dependencies for the entire monorepo (run from the repo root)
    $ rush install
    
    # 3. Build all projects
    $ rush build
    
    # 4. Build and run the demo
    $ cd ./api-demo
    $ npm run build
    $ npm run simple
    $ npm run advanced
  9. Use @microsoft/tsdoc for parsing TSDoc

    main
    The @microsoft/tsdoc library is the reference implementation of the TSDoc parser. Use it to ensure your tools are 100% compatible with the TSDoc standard. For implementation details on how to invoke the parser, refer to the api-demo directory in the repository.
  10. Configure eslint-plugin-tsdoc with Flat Config

    main

    To use eslint-plugin-tsdoc in a project using ESLint's Flat Config format, you can compose a configuration using profiles and mixins provided by tsdoc-build-rig.

    This configuration applies TSDoc linting rules to all .ts and .tsx files. It requires setting tsconfigRootDir in parserOptions to ensure the TypeScript parser correctly resolves the project's type information.

    const nodeTrustedToolProfile = require('tsdoc-build-rig/includes/eslint/flat/profile/node-trusted-tool');
    const friendlyLocalsMixin = require('tsdoc-build-rig/includes/eslint/flat/mixins/friendly-locals');
    
    module.exports = [
      ...nodeTrustedToolProfile,
      ...friendlyLocalsMixin,
      {
        files: ['**/*.ts', '**/*.tsx'],
        languageOptions: {
          parserOptions: {
            tsconfigRootDir: __dirname
          }
        }
      }
    ];
  11. Configure ESLint using tsdoc-build-rig profiles

    main

    You can use tsdoc-build-rig to provide pre-configured ESLint flat configuration profiles and mixins. This allows you to quickly adopt recommended settings for Node.js environments and local development patterns.

    Available components for configuration include:

    • tsdoc-build-rig/includes/eslint/flat/profile/node-trusted-tool: A profile for trusted Node.js tooling.
    • tsdoc-build-rig/includes/eslint/flat/mixins/friendly-locals: A mixin for handling local development patterns.
    const nodeTrustedToolProfile = require('tsdoc-build-rig/includes/eslint/flat/profile/node-trusted-tool');
    const friendlyLocalsMixin = require('tsdoc-build-rig/includes/eslint/flat/mixins/friendly-locals');
    
    module.exports = [
      ...nodeTrustedToolProfile,
      ...friendlyLocalsMixin,
      {
        files: ['**/*.ts', '**/*.tsx'],
        languageOptions: {
          parserOptions: {
            tsconfigRootDir: __dirname
          }
        },
        rules: {
          'no-console': 'off'
        }
      }
    ];