Skip to content

Commit

Permalink
dev: Catch forbidden whole words (#2000)
Browse files Browse the repository at this point in the history
Fix #1953
  • Loading branch information
Jason3S committed Nov 16, 2021
1 parent 7c360c6 commit b76fd12
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 26 deletions.
12 changes: 12 additions & 0 deletions packages/cspell-lib/samples/forbid-words/README.md
@@ -0,0 +1,12 @@
# Test Flagged Words

[`flagWords` does not mark all flagged words. · Issue #1953 · streetsidesoftware/cspell](https://github.com/streetsidesoftware/cspell/issues/1953)

Here is a list of words that should be flagged:

- therefor
- they'd
- they'll
- they're
- they've
- therefor
13 changes: 13 additions & 0 deletions packages/cspell-lib/samples/forbid-words/cspell.json
@@ -0,0 +1,13 @@
{
"version": "0.2",
"language": "en",
"words": [],
"flagWords": [
"therefor",
"they'd",
"they'll",
"they're",
"they've",
"therefor"
]
}
62 changes: 38 additions & 24 deletions packages/cspell-lib/src/textValidator.test.ts
Expand Up @@ -209,15 +209,20 @@ describe('Validate textValidator functions', () => {
});

test.each`
text | ignoreWords | flagWords | expected
${'red'} | ${[]} | ${undefined} | ${[]}
${'color'} | ${[]} | ${undefined} | ${[ov({ text: 'color', isFound: false })]}
${'colour'} | ${[]} | ${undefined} | ${[ov({ text: 'colour', isFlagged: true })]}
${'colour'} | ${['colour']} | ${undefined} | ${[]}
${'The ant ate the antelope.'} | ${[]} | ${['fbd']} | ${[]}
${'The ant ate the antelope.'} | ${[]} | ${['ate']} | ${[ov({ text: 'ate', isFlagged: true })]}
${'theANT_ateThe_antelope.'} | ${[]} | ${['ate']} | ${[ov({ text: 'ate', isFlagged: true })]}
${'The ant ate the antelope.'} | ${[]} | ${['antelope']} | ${[ov({ text: 'antelope', isFlagged: true })]}
text | ignoreWords | flagWords | expected
${'red'} | ${[]} | ${undefined} | ${[]}
${'color'} | ${[]} | ${undefined} | ${[ov({ text: 'color', isFound: false })]}
${'colour'} | ${[]} | ${undefined} | ${[ov({ text: 'colour', isFlagged: true })]}
${'colour'} | ${['colour']} | ${undefined} | ${[]}
${'The ant ate the antelope.'} | ${[]} | ${['fbd']} | ${[]}
${'The ant ate the antelope.'} | ${[]} | ${['ate']} | ${[ov({ text: 'ate', isFlagged: true })]}
${'theANT_ateThe_antelope.'} | ${[]} | ${['ate']} | ${[ov({ text: 'ate', isFlagged: true })]}
${'The ant ate the antelope.'} | ${[]} | ${['antelope']} | ${[ov({ text: 'antelope', isFlagged: true })]}
${'This should be ok'} | ${[]} | ${[]} | ${[]}
${"This should've been ok"} | ${[]} | ${[]} | ${[]}
${"This should've not been ok"} | ${[]} | ${["should've"]} | ${[ov({ text: "should've", isFlagged: true })]}
${"They'll be allowed"} | ${[]} | ${[]} | ${[]}
${"They'll not be allowed"} | ${[]} | ${["they'll"]} | ${[ov({ text: "They'll", isFlagged: true })]}
`('Validate forbidden words', ({ text, ignoreWords, expected, flagWords }) => {
const dict = getSpellingDictionaryCollectionSync({ ignoreWords });
const result = [...validateText(text, dict, { ignoreCase: false, flagWords })];
Expand Down Expand Up @@ -265,29 +270,38 @@ const fruit = [
const animals = ['ape', 'lion', 'tiger', 'Elephant', 'monkey', 'gazelle', 'antelope', 'aardvark', 'hyena'];
const insects = ['ant', 'snail', 'beetle', 'worm', 'stink bug', 'centipede', 'millipede', 'flea', 'fly'];
const words = [
'the',
'allowed',
'and',
'is',
'has',
'ate',
'light',
'dark',
'little',
'be',
'been',
'better',
'big',
'we',
'dark',
'done',
'fixes',
'has',
'have',
'published',
'is',
'known',
'light',
'little',
'multiple',
'fixes',
'to',
'the',
'not',
'problems',
'better',
'done',
'known',
"shouldn't",
'published',
'should',
'the',
'they',
'this',
'to',
'we',
"'ll",
"couldn't",
"should've",
"shouldn't",
"they'll",
"they've",
];

const forbiddenWords = ['!colour', '!favour'];
Expand Down
17 changes: 15 additions & 2 deletions packages/cspell-lib/src/textValidator.ts
Expand Up @@ -124,10 +124,14 @@ function lineValidator(dict: SpellingDictionary, options: ValidationOptions): Li
return dict.isNoSuggestWord(word, options);
}

function checkFlagWords(word: ValidationResult): ValidationResult {
function isWordFlagged(word: TextOffset): boolean {
const isIgnored = isWordIgnored(word.text);
const isFlagged = !isIgnored && testForFlaggedWord(word);
word.isFlagged = isFlagged;
return isFlagged;
}

function checkFlagWords(word: ValidationResult): ValidationResult {
word.isFlagged = isWordFlagged(word);
return word;
}

Expand Down Expand Up @@ -175,6 +179,15 @@ function lineValidator(dict: SpellingDictionary, options: ValidationOptions): Li
}

function checkPossibleWords(possibleWord: TextOffset) {
if (isWordFlagged(possibleWord)) {
const vr: ValidationResult = {
...possibleWord,
line: lineSegment,
isFlagged: true,
};
return [vr];
}

const mismatches: ValidationResult[] = Text.extractWordsFromTextOffset(possibleWord)
.filter(filterAlreadyChecked)
.map((wo) => ({ ...wo, line: lineSegment }))
Expand Down

0 comments on commit b76fd12

Please sign in to comment.