Skip to content

Commit

Permalink
fix: Mark adding words to custom dictionary experimental (#3266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jul 19, 2022
1 parent 0db77a0 commit ec0dcd8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
@@ -1,7 +1,30 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"definitions": {},
"definitions": {
"CustomWordListFile": {
"additionalProperties": false,
"properties": {
"addWords": {
"description": "**Experimental**: Provide a fix option to add words to the file.\n\nNote: this does not yet work perfectly.",
"type": "boolean"
},
"path": {
"$ref": "#/definitions/CustomWordListFilePath",
"description": "Path to word list file. File format: 1 word per line"
}
},
"required": [
"path",
"addWords"
],
"type": "object"
},
"CustomWordListFilePath": {
"description": "Specify a path to a custom word list file",
"type": "string"
}
},
"properties": {
"checkComments": {
"default": true,
Expand All @@ -24,8 +47,15 @@
"type": "boolean"
},
"customWordListFile": {
"description": "**Experimental**: Specify a path to a custom word list file. A utf-8 text file with one word per line. This file is used to present the option to add words.",
"type": "string"
"anyOf": [
{
"$ref": "#/definitions/CustomWordListFilePath"
},
{
"$ref": "#/definitions/CustomWordListFile"
}
],
"description": "Specify a path to a custom word list file"
},
"debugMode": {
"default": false,
Expand Down
18 changes: 13 additions & 5 deletions packages/cspell-eslint-plugin/src/cspell-eslint-plugin.ts
Expand Up @@ -15,7 +15,7 @@ import type { Comment, Identifier, ImportSpecifier, Literal, Node, TemplateEleme
import * as path from 'path';
import { format } from 'util';
import { addWordToCustomWordList } from './customWordList';
import { normalizeOptions, Options } from './options';
import { CustomWordListFile, normalizeOptions, Options } from './options';
import optionsSchema from './_auto_generated_/options.schema.json';

const schema = optionsSchema as unknown as Rule.RuleMetaData['schema'];
Expand Down Expand Up @@ -210,9 +210,11 @@ function create(context: Rule.RuleContext): Rule.RuleListener {
}

function createAddWordToDictionaryFix(word: string): Rule.SuggestionReportDescriptor | undefined {
if (!options.customWordListFile) return undefined;
if (!isCustomWordListFile(options.customWordListFile) || !options.customWordListFile.addWords) {
return undefined;
}

const dictFile = path.resolve(context.getCwd(), options.customWordListFile);
const dictFile = path.resolve(context.getCwd(), options.customWordListFile.path);

const data = { word, dictionary: path.basename(dictFile) };
const messageId: MessageIds = 'addWordToDictionary';
Expand Down Expand Up @@ -434,9 +436,11 @@ function getDocValidator(context: Rule.RuleContext): DocumentValidator {
}

function calcInitialSettings(options: Options, cwd: string): CSpellSettings {
if (!options.customWordListFile) return defaultSettings;
const { customWordListFile } = options;
if (!customWordListFile) return defaultSettings;

const dictFile = path.resolve(cwd, options.customWordListFile);
const filePath = isCustomWordListFile(customWordListFile) ? customWordListFile.path : customWordListFile;
const dictFile = path.resolve(cwd, filePath);

const settings: CSpellSettings = {
...defaultSettings,
Expand Down Expand Up @@ -481,3 +485,7 @@ class WrapFix implements Rule.Fix {
return this.fix.text;
}
}

function isCustomWordListFile(value: string | CustomWordListFile | undefined): value is CustomWordListFile {
return !!value && typeof value === 'object';
}
24 changes: 21 additions & 3 deletions packages/cspell-eslint-plugin/src/options.ts
Expand Up @@ -58,10 +58,28 @@ export interface Check {
*/
checkComments?: boolean;
/**
* **Experimental**: Specify a path to a custom word list file. A utf-8 text file with one word per line.
* This file is used to present the option to add words.
* Specify a path to a custom word list file
*/
customWordListFile?: string | undefined;
customWordListFile?: CustomWordListFilePath | CustomWordListFile | undefined;
}

/**
* Specify a path to a custom word list file
*/
export type CustomWordListFilePath = string;

export interface CustomWordListFile {
/**
* Path to word list file.
* File format: 1 word per line
*/
path: CustomWordListFilePath;
/**
* **Experimental**: Provide a fix option to add words to the file.
*
* Note: this does not yet work perfectly.
*/
addWords: boolean;
}

export const defaultCheckOptions: Required<Check> = {
Expand Down

0 comments on commit ec0dcd8

Please sign in to comment.