TSSLint Documentation

repository·master·Indexed 20 days ago

https://github.com/johnsoncodehk/tsslint

TSSLint is a type-aware TypeScript linter that runs as a tsserver plugin, reusing the existing TypeScript TypeChecker to avoid duplicate type-checking. It includes a CLI (@tsslint/cli) for CI/CD, a configuration API (@tsslint/config), and an adapter (@tsslint/compat-eslint) to run ESLint rules. It supports integration via a VS Code extension or a TypeScript plugin (@tsslint/typescript-plugin) for other editors, and provides shared types via @tsslint/types.

Tokens
11.6K
Snippets
42
Records
63
Agent score
70%

What's inside TSSLint

  1. Organize rules with nesting and scoping

    master

    Rules can be organized into namespaces by nesting them in the configuration object. The path in the object becomes the rule ID (e.g., style/no-debugger).

    Additionally, defineConfig accepts an array of configuration objects, allowing you to use include and exclude minimatch patterns to scope rules to specific files.

    defineConfig({
      rules: {
        style: {
          'no-debugger': debuggerRule,   // reported as "style/no-debugger"
        },
      },
    });
  2. Understand the role of @tsslint/core

    master

    @tsslint/core is the central linting engine for TSSLint. It is responsible for:

    • Running linting rules over TypeScript source files.
    • Managing the on-disk cache to improve performance.
    • Producing diagnostics (linting errors/warnings).

    Note: End users should not install @tsslint/core directly. It is a runtime dependency provided by the following packages:

  3. How @tsslint/typescript-plugin works with tsserver

    master
    The @tsslint/typescript-plugin acts as a tsserver language-service plugin. Instead of running a separate linting process, it integrates directly into the TypeScript language service. This means that TSSLint diagnostics and code fixes are delivered through the same mechanism as standard TypeScript errors, leveraging the Program already constructed by the editor for high performance and consistency.
  4. Understand TSSLint caching layers

    master

    TSSLint uses two layers of disk caching (stored in os.tmpdir()/tsslint-cache/) to optimize performance:

    1. Layer 1 (Syntactic): Used for rules that do not read ctx.program. These are invalidated when the linted file's modification time (mtime) changes.
    2. Layer 2 (Type-aware): Used for rules that do read ctx.program. These are invalidated using TypeScript's BuilderProgram affected-file diff, which includes transitive changes (like ambient .d.ts files).

    Tips:

    • If a rule depends on cross-file types, read ctx.program once to ensure it is classified as type-aware and correctly handled by Layer 2.
    • Use the --force CLI flag to ignore the cache.
    • Use the --list-rules CLI flag to see which rules are classified as type-aware vs syntactic.
  5. Extend TSSLint with Plugins

    master

    TSSLint supports a plugin system that allows you to rewrite rules on a per-file basis, filter diagnostics, and inject code fixes. You can use bundled plugins or build your own by implementing the Plugin type from @tsslint/types.

    Bundled plugins include:

    • createIgnorePlugin: Handles tsslint-ignore [rule-id] comments (single-line or block-style *-start/*-end).
    • createCategoryPlugin: Allows overriding the severity of rules based on pattern matching (e.g., setting all style/* rules to Warning).
    • createDiagnosticsPlugin: Forwards TypeScript's own diagnostics (like semantic errors) through the TSSLint pipeline.

    When using createDiagnosticsPlugin, it is recommended to wrap it in a check for isCLI() to avoid double-reporting errors in IDEs where ts-server already surfaces them.

    import {
      defineConfig,
      createIgnorePlugin,
      createCategoryPlugin,
      createDiagnosticsPlugin,
      isCLI,
    } from '@tsslint/config';
    import ts from 'typescript';
    
    export default defineConfig({
      rules: { /* ... */ },
      plugins: [
        // Handle tsslint-ignore [rule-id]
        createIgnorePlugin('tsslint-ignore', /* report unused */ true),
    
        // Override severity by rule-id pattern
        createCategoryPlugin({
          'style/*': ts.DiagnosticCategory.Warning,
        }),
    
        // Forward TypeScript's own diagnostics through the same pipeline.
        ...(isCLI() ? [createDiagnosticsPlugin('semantic')] : []),
      ],
    });
  6. Use @tsslint/compat-eslint to run ESLint rules in TSSLint

    master

    The @tsslint/compat-eslint package is an adapter that allows ESLint rules to run within the TSSLint environment. It is primarily consumed via the importESLintRules function from the @tsslint/config package.

    To use it, you must install the adapter and the specific ESLint plugins for the rules you wish to import.

    npm install @tsslint/compat-eslint --save-dev
    npm install @typescript-eslint/eslint-plugin --save-dev   # for @typescript-eslint/* rules
  7. Install @tsslint/types

    master

    The @tsslint/types package provides shared TypeScript types for TSSLint rules, plugins, and configuration.

    Most users will receive these types transitively by installing @tsslint/config. You should only install @tsslint/types directly if you are authoring a TSSLint plugin and do not wish to depend on @tsslint/config.

    npm install @tsslint/types --save-dev
  8. Import ESLint rules into TSSLint

    master

    You can reuse existing ESLint rules in your TSSLint configuration using @tsslint/compat-eslint.

    1. Install dependencies:
      npm install @tsslint/compat-eslint --save-dev
      npm install @typescript-eslint/eslint-plugin --save-dev
    2. Use importESLintRules in your config:
    import { defineConfig, importESLintRules } from '@tsslint/config';
    
    export default defineConfig({
      rules: {
        ...await importESLintRules({
          'no-unused-vars': true,
          '@typescript-eslint/no-explicit-any': 'warn',
        }),
      },
    });
  9. Install and configure @tsslint/typescript-plugin

    master

    To use TSSLint within your editor via the TypeScript language service, install @tsslint/typescript-plugin as a development dependency and register it in your tsconfig.json. This plugin allows diagnostics and code fixes to flow through the same path as TypeScript's own errors, utilizing the existing Program without requiring a second type-check.

    Note for VS Code users: If you use the TSSLint VS Code extension, this plugin is wired up automatically and you do not need to manually configure tsconfig.json.

    npm install @tsslint/typescript-plugin --save-dev
    {
      "compilerOptions": {
        "plugins": [{ "name": "@tsslint/typescript-plugin" }]
      }
    }
  10. Install @tsslint/cli

    master

    Install the @tsslint/cli package as a development dependency to use the TSSLint command-line runner in your terminal or CI environments. It supports linting TypeScript projects, as well as Vue, Vue Vine, MDX, Astro, and TS Macro projects via Volar language plugins.

    npm install @tsslint/cli --save-dev