Skip to content

Commit

Permalink
Honor the limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed May 29, 2022
1 parent b52d458 commit 780bb7b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
@@ -0,0 +1,29 @@
/**
* This is a sample file with many duplicate spelling errors.
*/

/**
* Creates a reciever withe a naame
* @param naame - the naame of the reciever.
*/
export function createReciever(naame: string): Reciever {
return {
naame: naame,
kount: 0,
};
}

interface Reciever {
naame: string;
kount: number;
}

export function colectorFactory(): (naame: string) => number {
const recievers = new Map<string, Reciever>();

return (naame: string) => {
const n = (recievers.get(naame) || 0) + 1;
recievers.set(naame, n);
return n;
};
}
14 changes: 14 additions & 0 deletions packages/cspell-lib/src/textValidation/docValidator.test.ts
Expand Up @@ -85,6 +85,20 @@ describe('docValidator', () => {
}
expect(dVal.prepTime).toBeGreaterThan(0);
});

// cspell:ignore kount naame colector Reciever reciever recievers
test.each`
filename | maxDuplicateProblems | expectedIssues
${fix('sample-with-errors.ts')} | ${undefined} | ${['Helllo']}
${fix('sample-with-many-errors.ts')} | ${undefined} | ${['reciever', 'naame', 'naame', 'naame', 'reciever', 'Reciever', 'naame', 'Reciever', 'naame', 'kount', 'Reciever', 'kount', 'colector', 'recievers', 'Reciever', 'recievers', 'recievers']}
${fix('sample-with-many-errors.ts')} | ${1} | ${['reciever', 'naame', 'Reciever', 'kount', 'colector', 'recievers']}
`('checkDocument $filename $maxDuplicateProblems', async ({ filename, maxDuplicateProblems, expectedIssues }) => {
const doc = await loadDoc(filename);
const dVal = new DocumentValidator(doc, { generateSuggestions: false }, { maxDuplicateProblems });
await dVal.prepare();
const r = dVal.checkDocument();
expect(r.map((issue) => issue.text)).toEqual(expectedIssues);
});
});

function td(uri: string, content: string, languageId?: string, locale?: string, version = 1): TextDocument {
Expand Down
18 changes: 17 additions & 1 deletion packages/cspell-lib/src/textValidation/docValidator.ts
Expand Up @@ -17,6 +17,8 @@ import { determineTextDocumentSettings } from './determineTextDocumentSettings';
import { ParsedText, SimpleRange } from './parsedText';
import {
calcTextInclusionRanges,
defaultMaxDuplicateProblems,
defaultMaxNumberOfProblems,
LineValidator,
lineValidatorFactory,
mapLineSegmentAgainstRangesFactory,
Expand Down Expand Up @@ -265,10 +267,24 @@ export class DocumentValidator {
}

private *checkDocumentLines() {
assert(this._preparations, ERROR_NOT_PREPARED);
const { maxNumberOfProblems = defaultMaxNumberOfProblems, maxDuplicateProblems = defaultMaxDuplicateProblems } =
this._preparations.validateOptions;

let numProblems = 0;
const mapOfProblems = new Map<string, number>();

for (const line of this.document.getLines()) {
const { text, offset } = line;
const range = [offset, offset + text.length] as const;
yield* this.check({ text, range });
for (const issue of this.check({ text, range })) {
const { text } = issue;
const n = (mapOfProblems.get(text) || 0) + 1;
mapOfProblems.set(text, n);
if (n > maxDuplicateProblems) continue;
yield issue;
if (++numProblems >= maxNumberOfProblems) return;
}
}
}

Expand Down

0 comments on commit 780bb7b

Please sign in to comment.