Skip to content

Commit

Permalink
fix: eslint-plugin - fix issue with document directives
Browse files Browse the repository at this point in the history
Ignore Regexp were not being honored.
  • Loading branch information
Jason3S committed Mar 14, 2022
1 parent b16d040 commit 4389ec1
Show file tree
Hide file tree
Showing 8 changed files with 2,635 additions and 57 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.test.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @type { import("eslint").Linter.Config }
*/
const config = {
extends: ['./.eslintrc.js', 'plugin:@cspell/recommended'],
};

module.exports = config;
2,555 changes: 2,511 additions & 44 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"homepage": "https://streetsidesoftware.github.io/cspell/",
"devDependencies": {
"@cspell/eslint-plugin": "file:packages/cspell-eslint-plugin",
"@tsconfig/node12": "^1.0.9",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.21",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// cspell:disable-next-line
const message = 'Helllo world';

// cspell:disable
const messages = ['muawhahahaha', 'grrrr', 'uuug', 'aarf'];
// cspell:enable

function main() {
console.log(message);
console.log(messages);
}

main();
22 changes: 13 additions & 9 deletions packages/cspell-lib/src/textValidation/docValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ describe('docValidator', () => {
expect(dVal.ready).toBe(true);
});

// cspell:ignore Helllo
// cspell:ignore Helllo grrrr

test.each`
filename | text | expected
${__filename} | ${'__filename'} | ${[]}
${fix('sample-with-errors.ts')} | ${'Helllo'} | ${[oc({ text: 'Helllo' })]}
${fix('sample-with-errors.ts')} | ${'main'} | ${[]}
filename | text | expected
${__filename} | ${'__filename'} | ${[]}
${fix('sample-with-errors.ts')} | ${'Helllo'} | ${[oc({ text: 'Helllo' })]}
${fix('sample-with-errors.ts')} | ${'main'} | ${[]}
${fix('sample-with-cspell-directives.ts')} | ${'grrrr'} | ${[]}
`('checkText async $filename "$text"', async ({ filename, text, expected }) => {
const doc = await loadDoc(filename);
const dVal = new DocumentValidator(doc, {}, {});
Expand All @@ -46,13 +47,15 @@ describe('docValidator', () => {
assert(offset >= 0);
const range = [offset, offset + text.length] as const;
expect(dVal.checkText(range, text, [])).toEqual(expected);
expect(dVal.prepTime).toBeGreaterThan(0);
});

test.each`
filename | text | expected
${__filename} | ${'__filename'} | ${[]}
${fix('sample-with-errors.ts')} | ${'Helllo'} | ${[oc({ text: 'Helllo' })]}
${fix('sample-with-errors.ts')} | ${'main'} | ${[]}
filename | text | expected
${__filename} | ${'__filename'} | ${[]}
${fix('sample-with-errors.ts')} | ${'Helllo'} | ${[oc({ text: 'Helllo' })]}
${fix('sample-with-errors.ts')} | ${'main'} | ${[]}
${fix('sample-with-cspell-directives.ts')} | ${'grrrr'} | ${[]}
`('checkText sync $filename "$text"', async ({ filename, text, expected }) => {
const doc = await loadDoc(filename);
const dVal = new DocumentValidator(doc, {}, {});
Expand All @@ -61,6 +64,7 @@ describe('docValidator', () => {
assert(offset >= 0);
const range = [offset, offset + text.length] as const;
expect(dVal.checkText(range, text, [])).toEqual(expected);
expect(dVal.prepTime).toBeGreaterThan(0);
});
});

Expand Down
29 changes: 25 additions & 4 deletions packages/cspell-lib/src/textValidation/docValidator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { opConcatMap, pipeSync } from '@cspell/cspell-pipe';
import type { CSpellSettingsWithSourceTrace, CSpellUserSettings, PnPSettings } from '@cspell/cspell-types';
import assert from 'assert';
import { CSpellSettingsInternal } from '../Models/CSpellSettingsInternalDef';
import { TextDocument } from '../Models/TextDocument';
import { loadConfig, mergeSettings, searchForConfig } from '../Settings';
import { finalizeSettings, loadConfig, mergeSettings, searchForConfig } from '../Settings';
import { loadConfigSync, searchForConfigSync } from '../Settings/configLoader';
import { getDictionaryInternal, getDictionaryInternalSync, SpellingDictionaryCollection } from '../SpellingDictionary';
import { toError } from '../util/errors';
import { MatchRange } from '../util/TextRange';
import { createTimer } from '../util/timer';
import { clean } from '../util/util';
import { determineTextDocumentSettings } from './determineTextDocumentSettings';
import {
Expand Down Expand Up @@ -42,13 +44,15 @@ export class DocumentValidator {
readonly errors: Error[] = [];
private _prepared: Promise<void> | undefined;
private _preparations: Preparations | undefined;
private _preparationTime = -1;

/**
* @param doc - Document to validate
* @param config - configuration to use (not finalized).
*/
constructor(doc: TextDocument, readonly options: DocumentValidatorOptions, readonly settings: CSpellUserSettings) {
this._document = doc;
// console.error(`DocumentValidator: ${doc.uri}`);
}

get ready() {
Expand All @@ -62,6 +66,8 @@ export class DocumentValidator {
// Load dictionaries
if (this._ready) return;

const timer = createTimer();

const { options, settings } = this;

const useSearchForConfig =
Expand All @@ -81,7 +87,8 @@ export class DocumentValidator {

const shouldCheck = docSettings.enabled ?? true;
const validateOptions = settingsToValidateOptions(docSettings);
const includeRanges = calcTextInclusionRanges(this._document.text, docSettings);
const finalSettings = finalizeSettings(docSettings);
const includeRanges = calcTextInclusionRanges(this._document.text, finalSettings);
const segmenter = mapLineSegmentAgainstRangesFactory(includeRanges);
const lineValidator = lineValidatorFactory(dict, validateOptions);

Expand All @@ -96,6 +103,7 @@ export class DocumentValidator {
};

this._ready = true;
this._preparationTime = timer.elapsed();
}

async prepare(): Promise<void> {
Expand All @@ -108,6 +116,8 @@ export class DocumentValidator {
private async _prepareAsync(): Promise<void> {
assert(!this._ready);

const timer = createTimer();

const { options, settings } = this;

const useSearchForConfig =
Expand All @@ -127,7 +137,8 @@ export class DocumentValidator {

const shouldCheck = docSettings.enabled ?? true;
const validateOptions = settingsToValidateOptions(docSettings);
const includeRanges = calcTextInclusionRanges(this._document.text, docSettings);
const finalSettings = finalizeSettings(docSettings);
const includeRanges = calcTextInclusionRanges(this._document.text, finalSettings);
const segmenter = mapLineSegmentAgainstRangesFactory(includeRanges);
const lineValidator = lineValidatorFactory(dict, validateOptions);

Expand All @@ -142,11 +153,20 @@ export class DocumentValidator {
};

this._ready = true;
this._preparationTime = timer.elapsed();
}

/**
* The amount of time in ms to prepare for validation.
*/
get prepTime(): number {
return this._preparationTime;
}

checkText(range: SimpleRange, _text: string, _scope: string[]): ValidationIssue[] {
assert(this._ready);
assert(this._preparations);
const { segmenter, lineValidator } = this._preparations;
// Determine settings for text range
// Slice text based upon include ranges
// Check text against dictionaries.
Expand All @@ -161,7 +181,8 @@ export class DocumentValidator {
offset,
},
};
const issues = [...this._preparations.lineValidator(lineSeg)];
const aIssues = pipeSync(segmenter(lineSeg), opConcatMap(lineValidator));
const issues = [...aIssues];

if (!this.options.generateSuggestions) {
return issues;
Expand Down
24 changes: 24 additions & 0 deletions packages/cspell-lib/src/util/timer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createTimer, polyHrTime } from './timer';
import { promisify } from 'util';

const delay = promisify(setTimeout);

describe('timer', () => {
test('createTimer', async () => {
const t = createTimer();
await delay(12);
expect(t.elapsed()).toBeGreaterThan(10);
});

test('polyHrTime', async () => {
const a = createTimer();
const b = createTimer(polyHrTime);
await delay(12);
const a1 = a.elapsed();
const b1 = b.elapsed();
expect(a1).toBeGreaterThanOrEqual(10);
expect(b1).toBeGreaterThanOrEqual(10);

expect(Math.abs(b1 - a1)).toBeLessThan(2);
});
});
40 changes: 40 additions & 0 deletions packages/cspell-lib/src/util/timer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const _hrTime: HRTimeFn = process?.hrtime || polyHrTime;

export interface Timer {
/** Start / restart the timer. */
start(): void;
/**
* Calculate the amount of time in ms since the
* timer was created / started.
*/
elapsed(): number;
}

export function createTimer(hrTimeFn = _hrTime): Timer {
let start: HRTime = hrTimeFn();

return {
start() {
start = hrTimeFn();
},
elapsed() {
return toMilliseconds(hrTimeFn(start));
},
};
}

export type HRTimeFn = (time?: HRTime) => HRTime;

export type HRTime = [number, number];

export function toMilliseconds(t: HRTime): number {
return (t[0] + t[1] * 1e-9) * 1000;
}

export function polyHrTime(time?: HRTime): HRTime {
const now = Date.now() - (time ? toMilliseconds(time) : 0);
const inSeconds = now * 1.0e-3;
const s = Math.floor(inSeconds);
const n = (inSeconds - s) * 1.0e9;
return [s, n];
}

0 comments on commit 4389ec1

Please sign in to comment.