typos

repository·master·Indexed 26 days ago

https://github.com/crate-ci/typos

A fast, low-false-positive spell checker designed for source code and monorepos, intended for use in CI/CD pipelines and PRs. It provides a CLI tool, typos-cli, that supports multiple output formats (long, brief, silent, JSON, SARIF), automatic corrections via --write-changes, and extensive configuration through _typos.toml, Cargo.toml, or pyproject.toml to handle false positives and file exclusions.

Tokens
12K
Snippets
24
Records
75
Agent score
87%

What's inside typos

  1. Use the `write_changes` input to modify local files

    master

    Setting write_changes: true instructs the action to write spelling corrections directly to the disk in the Action's local checkout.

    Important: This does not commit or push changes to your branch. It is intended for use in workflows where subsequent steps handle the local diff, such as actions that submit code suggestions based on local changes.

  2. Configure typos configuration precedence and file locations

    master

    The typos tool reads configuration in the following order of precedence:

    1. Command line arguments
    2. File specified via --config PATH
    3. Automatic discovery of configuration files in the current or parent directories. Supported filenames include:
      • typos.toml
      • _typos.toml
      • .typos.toml
      • Cargo.toml (must be under [workspace.metadata.typos] or [package.metadata.typos])
      • pyproject.toml (must be under [tool.typos])

    If a pyproject.toml or Cargo.toml is found but the required [typos] section is missing, the configuration file will be skipped.

  3. Run typos to find or fix spelling mistakes

    master
    Use the typos command to scan your project for spelling errors. By default, it reports typos found in your source code. You can also use the --write-changes (or -w) flag to automatically apply corrections.
  4. Integrate typos with pre-commit

    master

    To use typos as a pre-commit hook, add the following configuration to your .pre-commit-config.yaml file. By default, the typos hook installs a prebuilt executable from GitHub releases. Ensure you update the rev field to the specific typos git tag or revision you wish to use.

    repos:
      - repo: https://github.com/crate-ci/typos
        rev: v1.48.0
        hooks:
          - id: typos
  5. Install typos-cli

    master

    You can install the typos-cli tool using several package managers depending on your environment:

    • Cargo (Rust): cargo install typos-cli --locked
    • Homebrew (macOS/Linux): brew install typos-cli
    • Conda: conda install typos
    • Pacman (Arch Linux): sudo pacman -S typos
    • Pre-built binaries: Download from the GitHub releases page or use gh-install.
    $ cargo install typos-cli --locked
  6. Configure typos pre-commit hook execution modes

    master

    The default typos hook configuration is set to write fixes automatically. This will cause a pre-commit failure if any files are modified by the tool.

    If you want the hook to only report findings without modifying files, override the args property with an empty list [] (which passes no options) to prevent the use of -w or --write-changes.

    repos:
      - repo: https://github.com/crate-ci/typos
        rev: v1.48.0
        hooks:
          - id: typos
            args: []
  7. Set up Typos as a GitHub Action

    master

    You can use the Typos GitHub Action to automatically test your repository's spelling (or a specific subset of files) on pull requests.

    Requirements:

    • The runner must have wget installed.

    Note: While the v1 tag is available, using it may cause CI to fail if new releases introduce new typos. It is recommended to use a specific version tag like v1.48.0.

    name: Spelling
    
    permissions:
      contents: read
    
    on: [pull_request]
    
    env:
      CLICOLOR: 1
    
    jobs:
      spelling:
        name: Spell Check with Typos
        runs-on: ubuntu-latest
        steps:
        - name: Checkout Actions Repository
          uses: actions/checkout@v5
        - name: Spell Check Repo
          uses: crate-ci/typos@v1.48.0
  8. Configure file exclusion and ignore settings

    master

    Use the [files] section to control which files and directories typos processes.

    • extend-exclude: A list of gitignore-style globs to exclude specific files. Note: CLI arguments override this by default unless --force-exclude is used.
    • ignore-hidden: (bool, default: true) Skip hidden files and directories. CLI: --hidden.
    • ignore-files: (bool, default: true) Respect ignore files. CLI: --ignore.
    • ignore-dot: (bool, default: true) Respect .ignore files. CLI: --ignore-dot.
    • ignore-vcs: (bool, default: true) Respect ignore files in VCS directories. CLI: --ignore-vcs.
    • ignore-global: (bool, default: true) Respect global ignore files. CLI: --ignore-global.
    • ignore-parent: (bool, default: true) Respect ignore files in parent directories. CLI: --ignore-parent.
    [files]
    extend-exclude = [
      "*",
      "!something",
    ]
  9. Map typos to corrections using extend-words and extend-identifiers

    master

    You can manually define correct spellings for words and identifiers using tables in the [default] section.

    Words

    Use [default.extend-words] to map word typos to corrections.

    • If the correction is blank, the word is never valid.
    • If the correction is the same as the key, the word is always valid.

    Identifiers

    Use [default.extend-identifiers] to map identifier typos to corrections.

    • If the correction is blank, the identifier is never valid.
    • If the correction is the same as the key, the identifier is always valid.
    [default.extend-words]
    ## Project-specific acronym
    taits = "taits"
    tais = "taits"
    
    [default.extend-identifiers]
    ## Names
    Hte = "Hte"
    ## External
    ERROR_FILENAME_EXCED_RANGE = "ERROR_FILENAME_EXCED_RANGE"
    ERROR_FILENAME_EXCEDE_RANGE = "ERROR_FILENAME_EXCED_RANGE"
  10. Define custom file type configurations

    master

    When defining new file types, use the [type.NAME] section. You must provide extend-glob (a list of strings) to specify which files match this type. If multiple globs match a file, the most specific glob is used.

    Use typos --type-list to see the available NAMEs you can configure.

  11. Ignore patterns using regex in typos

    master

    To prevent typos from flagging specific patterns, use regex-based ignore keys in the [default] section:

    • extend-ignore-re: Matches uncorrectable sections (like specific comment patterns or code blocks).
    • extend-ignore-identifiers-re: Matches patterns for always-valid identifiers.
    • extend-ignore-words-re: Matches patterns for always-valid words. Note: You must handle case insensitivity yourself in the regex.

    Run with --type-list to see available file type names for custom configuration.

    [default]
    extend-ignore-re = [
        # Ignore lines that end with `# spellchecker:disable-line`
        "(?Rm)^.*(#|//)\s*spellchecker:disable-line$",
        # Ignore the line after `# spellchecker:ignore-next-line`:
        "(#|//)\s*spellchecker:ignore-next-line\n.*",
        # Ignore blocks between `# spellchecker:off` and `# spellchecker:on`
        "(?s)(#|//)\s*spellchecker:off.*?\n\s*(#|//)\s*spellchecker:on",
    ]
    extend-ignore-identifiers-re = [
        # Ignore identifiers that look like SSL cipher suites:
        "\\bTLS_[A-Z0-9_]+(_anon_[A-Z0-9_]+)?\\b",
    ]
    extend-ignore-words-re = [
        # words with length <= 4 chars is likely noise
        "^[a-zA-Z]{1,4}$",
    ]
  12. Handle false positives in _typos.toml

    master

    To prevent typos from flagging intentional words, acronyms, or names, you can configure a _typos.toml file.

    Common configuration tasks include:

    • Ignoring identifiers via regex: Use extend-ignore-identifiers-re under the [default] table.
    • Extending identifiers: Use [default.extend-identifiers] to map specific identifiers to their correct spelling.
    • Extending words: Use [default.extend-words] to add valid words to the dictionary.
    • Disabling content checking for specific file types: Use check-file = false for a specific type (e.g., [type.po]).
    • Excluding files: Use extend-exclude under the [files] table to completely skip certain files or patterns.
    [default]
    extend-ignore-identifiers-re = [
        # *sigh* this just isn't worth the cost of fixing
        "AttributeID.*Supress.*",
    ]
    
    [default.extend-identifiers]
    # *sigh* this just isn't worth the cost of fixing
    AttributeIDSupressMenu = "AttributeIDSupressMenu"
    
    [default.extend-words]
    # Don't correct the surname "Teh"
    teh = "teh"
    
    [type.po]
    extend-glob = ["*.po"]
    check-file = false
    
    [files]
    extend-exclude = ["localized/*.po"]