A comprehensive spell checking tool specifically designed for source code with CLI interface and programmatic API
npx @tessl/cli install tessl/npm-cspell@9.2.0CSpell is a comprehensive spell checking tool specifically designed for source code. It provides fast and accurate spell checking across multiple programming languages and file types, with intelligent parsing that handles CamelCase, snake_case, and compound word naming conventions.
npm install -g cspell (CLI) or npm install cspell (library)ESM:
import { lint, trace, checkText, suggestions, createInit } from "cspell";
import { run } from "cspell/app";CommonJS:
const { lint, trace, checkText, suggestions, createInit } = require("cspell");
const { run } = require("cspell/app");CLI usage:
cspell "**/*.js"
npx cspell lint "src/**/*.ts"CLI spell checking:
# Check all JavaScript files recursively
cspell "src/**/*.js"
# Check specific files with custom config
cspell lint "*.md" --config ./custom-cspell.json
# Interactive spell checking with color highlighting
cspell check README.md
# Get suggestions for misspelled words
cspell suggestions "teh" "recieve"Programmatic usage:
import { lint, createInit } from "cspell";
// Initialize a configuration file first
await createInit({
output: "./cspell.json",
format: "json",
locale: ["en-US"],
dictionary: ["typescript", "node"]
});
// Spell check files programmatically
const result = await lint(
["src/**/*.js", "docs/**/*.md"],
{
verbose: true,
showSuggestions: true,
config: "./cspell.json"
}
);
console.log(`Found ${result.issues} spelling issues`);
console.log(`Checked ${result.files} files`);CSpell is built around several key components:
Complete command-line interface with comprehensive spell checking commands including lint, check, trace, suggestions, init, config, link, and dictionaries management.
# Primary commands
cspell [options] [globs...] # Default lint command
cspell lint [options] [globs...] # Explicit lint command
cspell check [files...] # Interactive checking with colors
cspell trace [words...] # Debug word lookup
cspell suggestions [words...] # Get spelling suggestions
cspell init [options] # Initialize configuration
cspell config [options] # Update configuration
cspell link <subcommand> # Manage global links
cspell dictionaries [options] # List dictionariesCore spell checking functions for integration into Node.js applications, providing lint, trace, check, and suggestion capabilities with full TypeScript support.
function lint(
fileGlobs: string[],
options: LinterCliOptions,
reporter?: CSpellReporter
): Promise<RunResult>;
function trace(words: string[], options: TraceOptions): AsyncIterableIterator<TraceWordResult>;
function checkText(filename: string, options: BaseOptions & LegacyOptions): Promise<CheckTextResult>;
function suggestions(words: string[], options: SuggestionOptions): AsyncIterable<TimedSuggestionsForWordResult>;
function createInit(options: InitOptions): Promise<void>;Configuration initialization and management system for creating and updating CSpell configuration files with support for multiple formats and import/export capabilities.
function createInit(options: InitOptions): Promise<void>;
interface InitOptions {
config?: string;
output?: string;
format?: string;
import?: string[];
locale?: string[];
dictionary?: string[];
comments?: boolean;
schema?: boolean;
stdout?: boolean;
}Extensible reporting system with built-in reporters and support for custom reporter modules, template-based output formatting, and color styling.
function getDefaultReporter(options: ReporterOptions, config?: CSpellReporterConfiguration): CSpellReporter;
interface CSpellReporterConfiguration extends ReporterConfiguration, LinterCliOptions {
readonly console?: ReporterConsole;
}
interface CSpellReporterModule {
getReporter: <T>(settings: T, config: CSpellReporterConfiguration) => CSpellReporter;
}interface RunResult {
files: number;
filesWithIssues: Set<string>;
issues: number;
errors: number;
cachedFiles: number;
}
interface TraceWordResult {
word: string;
found: boolean;
foundWord?: string;
forbidden?: boolean;
noSuggest?: boolean;
dictionaries: DictionaryTraceResult[];
}
interface CheckTextResult {
document: TextDocument;
options: CSpellSettings;
issues: TextIssue[];
}
interface TimedSuggestionsForWordResult extends SuggestionsForWordResult {
elapsedTimeMs?: number;
}
interface LinterCliOptions extends BaseOptions, CacheOptions, ReporterConfiguration {
verbose?: boolean;
debug?: boolean;
exclude?: string[] | string;
wordsOnly?: boolean;
unique?: boolean;
root?: string;
dot?: boolean;
showContext?: boolean | number;
showSuggestions?: boolean;
cache?: boolean;
cacheLocation?: string;
gitignore?: boolean;
reporter?: string[];
issueTemplate?: string;
}
interface BaseOptions {
config?: string;
locale?: string;
languageId?: string;
defaultConfiguration?: boolean;
}
interface TraceOptions extends BaseOptions {
allowCompoundWords?: boolean;
ignoreCase?: boolean;
dictionary?: string[];
stdin?: boolean;
}
interface SuggestionOptions extends BaseOptions {
numChanges?: number;
numSuggestions?: number;
includeTies?: boolean;
repl?: boolean;
useStdin?: boolean;
ignoreCase?: boolean;
}CSpell uses custom error classes for different failure scenarios:
class ApplicationError extends Error {
readonly exitCode: number;
readonly cause?: Error | undefined;
constructor(message: string, exitCode?: number, cause?: Error | undefined);
}
class CheckFailed extends Error {
readonly exitCode: number;
constructor(message: string, exitCode?: number);
}Common error scenarios:
ApplicationError for unsupported Node.js versions