Skip to content

Commit

Permalink
feat: Add configFile option to CSpell ESLint Plugin (#5545)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed May 2, 2024
1 parent 59d47d0 commit 1ebc8b8
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 7 deletions.
22 changes: 22 additions & 0 deletions packages/cspell-eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ interface Options {
* @default true
*/
checkComments?: boolean;
/**
* Path to the cspell configuration file.
* Relative paths, will be relative to the current working directory.
* @since 8.8.0
*/
configFile?: string;
/**
* Some CSpell Settings
*/
Expand Down Expand Up @@ -246,6 +252,22 @@ export default [
}
```

## `configFile` - Using a CSpell Configuration File

**`eslint.config.mjs`**

```js
rules: {
'@cspell/spellchecker': [
'error',
{
//
configFile: new URL('./cspell.config.yaml', import.meta.url).toString(),
},
],
},
```
## `autoFix`
When enabled, `autoFix` corrects any spelling issues that have a single "preferred" suggestion. It attempts to match
Expand Down
4 changes: 4 additions & 0 deletions packages/cspell-eslint-plugin/assets/options.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"description": "Spell check strings",
"type": "boolean"
},
"configFile": {
"description": "Path to the cspell configuration file. Relative paths, will be relative to the current working directory.",
"type": "string"
},
"cspell": {
"additionalProperties": false,
"description": "CSpell options to pass to the spell checker.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ignorePaths:
- cspell*.{yaml,json}
ignoreWords:
- todos
- blacklist
- whitelist
- café
- bluelist
- Bluelist
6 changes: 6 additions & 0 deletions packages/cspell-eslint-plugin/src/common/options.cts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ export interface Check {
* @default true
*/
checkComments?: boolean;
/**
* Path to the cspell configuration file.
* Relative paths, will be relative to the current working directory.
* @since 8.8.0
*/
configFile?: string;
/**
* CSpell options to pass to the spell checker.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const defaultCheckOptions: Required<Check> = {
checkJSXText: true,
checkStrings: true,
checkStringTemplates: true,
configFile: '',
cspell: {
words: [],
flagWords: [],
Expand Down
6 changes: 6 additions & 0 deletions packages/cspell-eslint-plugin/src/test/index.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ ruleTester.run('cspell', Rule.rules.spellchecker, {
ignoreWords: ['Guuide', 'Gallaxy', 'BADD', 'functionn', 'coool'],
},
}),
readFix('with-errors/auto-fix.ts', {
ignoreImports: false,
customWordListFile: resolveFix('with-errors/creepyData.dict.txt'),
// Load a configuration to ignore the forbidden words.
configFile: resolveFix('cspell.test.config.yaml'),
}),
readFix('issue-4870/sample.js', {
cspell: {
dictionaries: ['business-terms'],
Expand Down
12 changes: 12 additions & 0 deletions packages/cspell-eslint-plugin/src/worker/spellCheck.mts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,18 @@ function calcInitialSettings(options: WorkerOptions): CSpellSettings {
flagWords: cspell?.flagWords || [],
};

if (options.configFile) {
const optionCspellImport = options.cspell?.import;
const importConfig =
typeof optionCspellImport === 'string'
? [optionCspellImport]
: Array.isArray(optionCspellImport)
? optionCspellImport
: [];
importConfig.push(options.configFile);
settings.import = importConfig;
}

if (customWordListFile) {
const filePath = isCustomWordListFile(customWordListFile) ? customWordListFile.path : customWordListFile;
const { dictionaries = [], dictionaryDefinitions = [] } = settings;
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-tools/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export interface Target extends CompileTargetOptions {
* Words from the sources that are found in `excludeWordsFrom` files
* will not be added to the dictionary.
*
* @version 8.3.2
* @since 8.3.2
*/
excludeWordsFrom?: FilePath[] | undefined;

Expand Down
8 changes: 4 additions & 4 deletions packages/cspell-types/src/CSpellSettingsDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export interface FileSettings extends ExtendableSettings, CommandLineSettings {
/**
* Configure CSpell features.
*
* @version 5.16.0
* @since 5.16.0
*/
features?: Features;
}
Expand Down Expand Up @@ -806,7 +806,7 @@ export interface ExperimentalFileSettings {
/**
* Future Plugin support
* @experimental
* @version 6.2.0
* @since 6.2.0
*/
plugins?: Plugin[];
}
Expand All @@ -829,15 +829,15 @@ export interface ExperimentalBaseSettings {
/**
* Parser to use for the file content
* @experimental
* @version 6.2.0
* @since 6.2.0
*/
parser?: ParserName;
}

/**
* Plugin API
* @experimental
* @version 6.2.0
* @since 6.2.0
*/
export interface Plugin {
parsers?: Parser[];
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-types/src/DictionaryDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export interface DictionaryDefinitionInlineIgnoreWords

/**
* Inline Dictionary Definitions
* @version 6.23.0
* @since 6.23.0
*/
export type DictionaryDefinitionInline =
| DictionaryDefinitionInlineWords
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const config = [
...tsESLint.configs.recommended,
// cspellRecommended or cspellConfigs.recommended can be used interchangeably.
cspellConfigs.recommended,

{
files: ['**/*.ts', '**/*.tsx'],
ignores: ['**/*.d.ts', '**/*.map', '**/coverage/**', '**/dist/**', '**/node_modules/**'],
Expand All @@ -25,6 +24,17 @@ const config = [
'@cspell/spellchecker': ['warn', { customWordListFile: 'words.txt', autoFix: true }],
},
},
{
files: ['**/importAlias.ts'],
rules: {
'@cspell/spellchecker': [
'error',
{
configFile: `${new URL('fixtures/cspell.test.config.yaml', import.meta.url)}`,
},
],
},
},
{
files: ['**/*.js'],
plugins: cspellRecommended.plugins,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
# yaml-language-server: $schema=https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json
$schema: https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json
noConfigSearch: true
ignorePaths:
- "*.{ts,js}"
- "cspell*.{json,yaml}"
ignoreWords:
- quic
- tsbuildinfo
- uuug
- grrr
- Expresssions # Note this is a deliberate misspelling
words:
- pnpm
flagWords:
- blacklist->denylist
- whitelist->allowlist
- café->cafe
- bluelist->greenList
- Bluelist->GreenList
dictionaryDefinitions:
- name: words
path: ../words.txt
dictionaries:
- words

0 comments on commit 1ebc8b8

Please sign in to comment.