CSpell ESLint Plugin
A spell checker plugin for ESLint based upon CSpell.
Feedback Welcome
This plugin is still in active development as part of the CSpell suite of tools and applications.
Quick Setup
-
Install @cspell/eslint-plugin
as a dev-dependency
npm install --save-dev @cspell/eslint-plugin
-
Add the plugin to the ESLint configuration (see below:)
Configuration (new: eslint.config.js
)
eslint.config.js
using recommended.
import cspellESLintPluginRecommended from '@cspell/eslint-plugin/recommended';
export default [
cspellESLintPluginRecommended
];
Or
eslint.config.js
using configs.
import cspellConfigs from '@cspell/eslint-plugin/configs';
export default [
cspellConfigs.recommended
];
Or
eslint.config.js
using plugins
import cspellPlugin from '@cspell/eslint-plugin';
export default [
{
plugins: { '@cspell': cspellPlugin },
rules: {
'@cspell/spellchecker': ['warn', {}]
}
}
];
Configuration (Legacy: .eslintrc
)
Add "plugin:@cspell/recommended"
to the extends
section of the configuration.
.eslintrc
{
"extends": ["plugin:@cspell/recommended"]
}
Options
interface Options {
autoFix: boolean;
numSuggestions: number;
generateSuggestions: boolean;
ignoreImports?: boolean;
ignoreImportProperties?: boolean;
checkIdentifiers?: boolean;
checkStrings?: boolean;
checkStringTemplates?: boolean;
checkJSXText?: boolean;
checkComments?: boolean;
configFile?: string;
cspell?: {
language?: string;
words?: string[];
ignoreWords?: string[];
flagWords?: string[];
ignoreRegExpList?: string[];
includeRegExpList?: string[];
allowCompoundWords?: boolean;
import?: string[];
dictionaries?: string[];
dictionaryDefinitions?: DictionaryDefinition[];
};
cspellOptionsRoot?: string | URL;
customWordListFile?: string | { path: string };
checkScope?: ScopeSelectorList;
debugMode?: boolean;
}
Examples:
eslint.config.js
import cspellPlugin from '@cspell/eslint-plugin';
export default [
{
plugins: { '@cspell': cspellPlugin },
rules: {
'@cspell/spellchecker': ['warn', { checkComments: false, autoFix: true }]
}
}
];
eslint.config.js
import cspellConfigs from '@cspell/eslint-plugin/configs';
export default [
cspellConfigs.recommended,
{
rules: {
'@cspell/spellchecker': ['warn', { checkComments: false, autoFix: true }]
}
}
];
.eslintrc.json
{
"plugins": ["@cspell"],
"rules": {
"@cspell/spellchecker": ["warn", { "checkComments": false, "autoFix": true }]
}
}
autoFix
When enabled, autoFix
corrects any spelling issues that have a single "preferred" suggestion. It attempts to match
case and style, but it cannot guarantee correctness of code.
Preferred Suggestions
CSpell offers the ability to flag words as incorrect and to provide suggestions.
cspell.config.yaml
words:
- allowlist
flagWords:
- blacklist->allowlist
suggestWords:
- colour->color
With this configuration, blacklist
is flagged as forbidden and allowlist
is the "preferred" suggestion. When autoFix
is enabled, all instances of blacklist
will be replaced with allowlist
.
When spell checking, if colour
is not in one of the dictionaries, then color
will be offered as the preferred suggestion. suggestWords
are used to provide preferred suggestions, but will not flag any words as incorrect.
CSpell will match case, but not word stems. blacklist
and Blacklist
will get replaced, but not blacklists
.
configFile
- Using a CSpell Configuration File
eslint.config.mjs
rules: {
'@cspell/spellchecker': [
'error',
{
configFile: new URL('./cspell.config.yaml', import.meta.url).toString(),
},
],
},
cspell
and cspellOptionsRoot
- CSpell Configuration
It is possible to send cspell
configuration to the spell checker. Where possible, use a cspell configuration file and set configFile
. But there are cases where this is not possible or desired (like fewer configuration files).
- Option
cspell
is used to pass along configuration to the spell checker. - Option
cspellOptionsRoot
is used to tell the spell checker how to find cspell.import
s.
Example: eslint.config.mjs
rules: {
'@cspell/spellchecker': [
'warn',
{
cspell: {
import: ['./cspell.config.yaml', '@cspell/dict-de-de']
},
cspellOptionsRoot: import.meta.url,
},
],
},
Assuming import.meta.url
is file:///Users/ci/project/app/eslint.config.mjs
, this tells the spell checker to import cspell.config.yaml
from file:///Users/ci/project/app/cspell.config.yaml
and to search for package @cspell/dict-de-de
starting at file:///Users/ci/project/app/
.
If cspellOptionsRoot
is not specified, the current working directory is used.
Checking Custom AST Nodes
The checkScope
setting is used to enable / disable checking AST Nodes. ESLint uses parsers to generate the AST (Abstract Syntax Tree) to evaluate a document. Each PlugIn gets access to the AST. checkScope
can be used to handle new AST nodes when a custom parser is added. Some knowledge of the AST output by the parser is necessary.
rules: {
'@cspell/spellchecker': ['warn', { checkScope: [
['JSONLiteral': true],
['JSONProperty[key] JSONLiteral', false]
['JSONProperty JSONLiteral', false]
['JSONProperty[value] JSONLiteral', true]
['YAMLPair[key] YAMLScalar', true],
['YAMLPair[value] YAMLScalar', true],
['YAMLSequence YAMLScalar', true],
] }],
},
In Combination with CSpell
Due to the nature of how files are parsed, the cspell
command line tool and this ESLint plugin will give different results.
It is recommended that either ESLint or cspell
checks a file, but not both. Use ignorePaths
setting in cspell.json
to
tell the cspell
command line tool to ignore files checked by ESLint.
Differences:
-
The CSpell parser is generic across all file types. It just breaks an entire document into words and tests them against the dictionaries. Everything is checked, comments, code, strings, etc.
-
The CSpell ESLint plugin uses the AST (a way to identify the meaning of the individual parts of your code) provided by ESLint to only check literal strings, identifiers, and comments. See Options on selecting what to check.
Example spell checked with ESLint CSpell Plugin:
Example spell checked with just cspell
:
CSpell for Enterprise
Available as part of the Tidelift Subscription.
The maintainers of cspell and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.
Brought to you by Street Side Software