Skip to content

Improve pre-commit hook to remove unused custom dictionary entries and keep the dictionary sorted #686

@ssbarnea

Description

@ssbarnea

After years of successfully using cspell as a pre-commit hook, two common annoyances seem to be present.

  • custom dictionary keep growing and unnecessary items are not removed. They can be not needed due to update of enabled bundled dictionaries or because the occurence was removed
  • adding terms to the custom dictionary and keeping it sorted is prone to human errors.

Due to this, recently I found an alternative way to run cspell hook which I want to share with you, hoping that this will become an official hook.

  - repo: https://github.com/streetsidesoftware/cspell-cli
    rev: v9.0.1
    hooks:
      - id: cspell
        entry: sh
        pass_filenames: false
        args:
          [
            "-c",
            "cspell-cli . --relative --no-progress --no-summary && cspell . --disable-dictionary=words --words-only --quiet --unique | sort --ignore-case > .config/dictionary.txt",
          ]
        name: Spell check with cspell and remove unused dictionary entries

As seen above this will:

  • run spell once on entire codebase (as opposed to only modified files). This address bug where cspell will not run if its own configuration is changed or if the custom dictionary is modified, failing to spot issues.
  • if the run is successful, it will reorder the custom dictionary.
  • this keeps pre-commit hook cspell behavior in sync with external cspell calls (vscode extension for example).

Downsides:

  • Current implementation relies on knowing the name and location of the in-repo custom dictionary. Maybe some modifications to the tool itself might remove the need for these and solely rely on its config file:
dictionaryDefinitions:
  - name: words
    path: .config/dictionary.txt
    addWords: true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions