CSpell

repository·main·Indexed 23 days ago

https://github.com/streetsidesoftware/cspell

A powerful spell checker specifically designed for code, provided as a mono-repo. It includes a CLI, ESLint integration via @cspell/eslint-plugin, and various libraries for dictionary management, such as cspell-config, cspell-dictionary, and @cspell/cspell-bundled-dicts. It supports programmatic invocation via the lint() function and provides a dictionary-bundler-plugin for build tools including Vite, Rollup, esbuild, Webpack, Rspack, Rolldown, and tsdown.

Tokens
113.6K
Snippets
269
Records
778
Agent score
81%

What's inside CSpell

  1. Overview of CSpell packages

    main

    CSpell is organized as a monorepo containing several specialized packages. Depending on your use case, you may need different components:

    • CLI Application: Use cspell for the standard command-line spelling checker.
    • ESLint Integration: Use @cspell/eslint-plugin to integrate spell checking into your ESLint workflow.
    • Programmatic Usage: Use cspell-lib if you want to perform code-driven spelling checks within your own applications.
    • Type Safety: Use cspell-types to access TypeScript definitions and JSON schemas for CSpell configuration files.
    • Dictionaries: cspell-bundled-dicts provides the standard collection of dictionaries used by the tool.
    • Low-level Utilities: Other packages like cspell-glob, cspell-io, cspell-trie-lib, and hunspell-reader provide specialized I/O, globbing, and data structure support used by the core engine.
  2. Use @cspell/cspell-worker for off-thread spelling checks

    main

    The @cspell/cspell-worker package is designed to enable spelling checking of documents on a NodeJS worker thread. This allows you to offload the CPU-intensive spelling check process from the main thread, preventing UI or main-loop blocking in NodeJS applications.

    CAUTION

    This package is currently experimental. Its exports and APIs are subject to change.

  3. What is Simple RPC and how does it communicate?

    main

    Simple RPC is a basic client/server Remote Procedure Call implementation designed for sending requests over a MessagePort. It is compatible with environments providing a MessagePortLike interface, such as Node.js Worker Threads or Web Workers.

    Communication is handled via RPCMessage objects sent over the MessagePort. Each message includes a signature (sig: 'RPC0'), a unique identifier (id), and a type indicating the message's purpose.

    export interface RPCMessage {
      sig: 'RPC0';
      /**
       * A Unique identifier for the request/response.
       * Ideally this is a randomUUID.
       */
      id: RequestID;
      /**
       * The type of message being sent.
       */
      type: 'request' | 'response' | 'cancel' | 'canceled' | 'ok' | 'ready';
    }
    
    export type RPCRequestType = 'request' | 'cancel' | 'ok' | 'ready';
    
    export type RPCResponseType = 'response' | 'canceled' | 'ok' | 'ready';
  4. Understand File Type Ids (languageId)

    main
    In CSpell configuration, the field languageId refers to file types (e.g., javascript, python, markdown) rather than spoken languages (e.g., English, German). This terminology is used for historical reasons to maintain compatibility with VSCode conventions. The languageId determines which dictionaries are enabled, which rules apply, and whether a file is subject to spell-checking.
  5. Use inline document settings in source code

    main

    You can add spell check settings directly into your source code using comments. This is useful for file-specific configurations that shouldn't apply to the entire project. All settings must be prefixed with cSpell:, spell-checker:, or spellchecker:.

    Supported settings include:

    • disable / enable: Turn the spell checker on or off.
    • ignore: Specify a list of words to ignore for the entire file.
    • words: Specify a list of words to be considered correct (added to suggestions) for the entire file.
    • ignoreRegExp: Any text matching this regular expression will NOT be checked.
    • includeRegExp: Only text matching this regular expression will be checked.
    • enableCompoundWords / disableCompoundWords: Allow or disallow joined words (e.g., "stringlength"). Note: The last setting in the file determines the value for the entire file.
    • dictionaries: Specify a list of dictionary names to use.
    // cSpell:words woorxs sweeetbeat
    // cSpell:ignore zaallano, wooorrdd
    // cSpell:enableCompoundWords
    // cSpell:ignoreRegExp 0x[0-9a-f]+
  6. How CSpell processes words

    main

    CSpell uses a simple strategy of splitting camelCase and snake_case words before checking them against known dictionaries.

    Examples of splitting:

    • camelCase $\rightarrow$ camel case
    • HTMLInput $\rightarrow$ html input
    • srcCode $\rightarrow$ src code
    • snake_case_words $\rightarrow$ snake case words
    • camel2snake $\rightarrow$ camel snake (the 2 is ignored)
    • function parseJson(text: string) $\rightarrow$ function parse json text string

    Special Cases & Limitations:

    • Escape Characters: Characters like \n or \t are removed if the resulting word doesn't match. For example, \narrow becomes narrow (a match), but \ncode becomes ncode (not a match).
    • Case Sensitivity: The spellchecker is case-insensitive and will not catch errors like english instead of English.
    • Word Length: Only words longer than 3 characters are checked (e.g., jsj is ignored, but jsja is checked).
    • Symbols: All symbols and punctuation are ignored.
  7. Understand case sensitivity in spelling dictionaries

    main

    CSpell dictionaries can operate in two modes: case-insensitive (default) or case-sensitive.

    • Case-Insensitive (Default): Words are treated as lowercase with accents removed. This allows for broad matching but may lose specific casing information.
    • Case-Sensitive: Allows for exact matches of casing and accents. To support both exact matches and broad case-insensitive lookups, dictionaries use a prefixing convention.

    To enable fast lookups while maintaining flexibility, case-sensitive dictionaries store words using the ~ prefix to indicate that case or accents have been normalized.