Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not return forbidden words in suggestions #1519

Merged
merged 1 commit into from Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,7 +22,8 @@ describe('Verify using multiple dictionaries', () => {
];
const wordsB = ['ape', 'lion', 'tiger', 'elephant', 'monkey', 'gazelle', 'antelope', 'aardvark', 'hyena'];
const wordsC = ['ant', 'snail', 'beetle', 'worm', 'stink bug', 'centipede', 'millipede', 'flea', 'fly'];
const wordsD = ['red*', 'green*', 'blue*', 'pink*', 'black*', '*berry', '+-fruit'];
const wordsD = ['red*', 'green*', 'blue*', 'pink*', 'black*', '*berry', '+-fruit', '*bug', 'pinkie'];
const wordsF = ['!pink*', '+berry', '+bug', '!stinkbug'];
test('checks for existence', async () => {
const dicts = await Promise.all([
createSpellingDictionary(wordsA, 'wordsA', 'test'),
Expand Down Expand Up @@ -113,6 +114,73 @@ describe('Verify using multiple dictionaries', () => {
expect(sugs).toContain('apple mango');
});

test.each`
word | expected
${'redberry'} | ${true}
${'pink'} | ${true}
${'bug'} | ${true}
${'blackberry'} | ${true}
${'pinkbug'} | ${true}
`('checks has word: "$word"', ({ word, expected }) => {
const dicts = [
createSpellingDictionary(wordsA, 'wordsA', 'test'),
createSpellingDictionary(wordsB, 'wordsB', 'test'),
createSpellingDictionary(wordsC, 'wordsC', 'test'),
createSpellingDictionary(wordsD, 'wordsD', 'test'),
createSpellingDictionary(wordsF, 'wordsF', 'test'),
];

const dictCollection = createCollection(dicts, 'test', ['Avocado']);
expect(dictCollection.has(word)).toEqual(expected);
});

// cspell:ignore pinkbug redberry
// Note: `pinkbug` is not forbidden because compound forbidden words is not yet supported.
test.each`
word | expected
${'redberry'} | ${false}
${'pink'} | ${true}
${'bug'} | ${false}
${'blackberry'} | ${false}
${'stinkbug'} | ${true}
${'pinkbug'} | ${false}
`('checks forbid word: "$word"', ({ word, expected }) => {
const dicts = [
createSpellingDictionary(wordsA, 'wordsA', 'test'),
createSpellingDictionary(wordsB, 'wordsB', 'test'),
createSpellingDictionary(wordsC, 'wordsC', 'test'),
createSpellingDictionary(wordsD, 'wordsD', 'test'),
createSpellingDictionary(wordsF, 'wordsF', 'test'),
];

const dictCollection = createCollection(dicts, 'test', ['Avocado']);
expect(dictCollection.isForbidden(word)).toEqual(expected);
});

function sr(word: string, cost: number) {
return { word, cost };
}

test.each`
word | expected
${'redberry'} | ${[sr('redberry', 0), sr('red berry', 105)]}
${'pink'} | ${[sr('pinkie', 189)]}
${'bug'} | ${[sr('bug', 5)]}
${'blackberry'} | ${[sr('blackberry', 0), sr('black berry', 98)]}
${'stinkbug'} | ${[sr('stink bug', 103), sr('pinkbug', 198)]}
`('checks suggestions word: "$word"', ({ word, expected }) => {
const dicts = [
createSpellingDictionary(wordsA, 'wordsA', 'test'),
createSpellingDictionary(wordsB, 'wordsB', 'test'),
createSpellingDictionary(wordsC, 'wordsC', 'test'),
createSpellingDictionary(wordsD, 'wordsD', 'test'),
createSpellingDictionary(wordsF, 'wordsF', 'test'),
];

const dictCollection = createCollection(dicts, 'test', ['Avocado']);
expect(dictCollection.suggest(word, 2)).toEqual(expected);
});

test('checks for suggestions with flagged words', async () => {
const dicts = await Promise.all([
createSpellingDictionary(wordsA, 'wordsA', 'test'),
Expand Down
Expand Up @@ -73,8 +73,12 @@ export class SpellingDictionaryCollection implements SpellingDictionary {
} = suggestOptions;
_suggestOptions.compoundMethod = this.options.useCompounds ? CompoundWordsMethod.JOIN_WORDS : compoundMethod;
const prefixNoCase = CASE_INSENSITIVE_PREFIX;
const filter = (word: string) => {
return !this.wordsToFlag.has(word.toLowerCase()) && (ignoreCase || word[0] !== prefixNoCase);
const filter = (word: string, _cost: number) => {
return (
!this.wordsToFlag.has(word.toLowerCase()) &&
(ignoreCase || word[0] !== prefixNoCase) &&
!this.isForbidden(word)
);
};
const collector = suggestionCollector(word, {
numSuggestions,
Expand Down