-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cspell-eslint-plugin): Experimental Add word to dictionary (#3247)
## Experimental Feature Add the ability to add words to a custom dictionary. Mostly fixes: #3233, but is flaky because it is using an unsupported technique to detect when a fix has been applied. - Option: `customWordListFile` **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.
- Loading branch information
Showing
8 changed files
with
151 additions
and
14 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
packages/cspell-eslint-plugin/fixtures/with-errors/creepyData.dict.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
muawhahaha | ||
grrrrr | ||
uuuug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
const sortFn = new Intl.Collator().compare; | ||
|
||
export function addWordToCustomWordList(customWordListPath: string, word: string) { | ||
const content = readFile(customWordListPath) || '\n'; | ||
const lineEndingMatch = content.match(/\r?\n/); | ||
const lineEnding = lineEndingMatch?.[0] || '\n'; | ||
const words = new Set( | ||
content | ||
.split(/\n/g) | ||
.map((a) => a.trim()) | ||
.filter((a) => !!a) | ||
); | ||
words.add(word); | ||
|
||
const lines = [...words]; | ||
lines.sort(sortFn); | ||
writeFile(customWordListPath, lines.join(lineEnding) + lineEnding); | ||
} | ||
|
||
function readFile(file: string): string | undefined { | ||
try { | ||
return fs.readFileSync(file, 'utf-8'); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
|
||
function writeFile(file: string, content: string) { | ||
makeDir(path.dirname(file)); | ||
fs.writeFileSync(file, content); | ||
} | ||
|
||
function makeDir(dir: string) { | ||
try { | ||
fs.mkdirSync(dir, { recursive: true }); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.