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: Support passing CSpellConfigFile to endpoints #6651

Merged
merged 3 commits into from
Dec 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/cspell-config-lib/src/CSpellConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ function addUniqueWordsToListAndSort(list: string[], toAdd: string[]): void {
}
}

export function satisfiesCSpellConfigFile(obj: unknown): obj is ICSpellConfigFile {
const r: boolean =
obj instanceof CSpellConfigFile ||
(!!obj &&
typeof obj === 'object' &&
'url' in obj &&
obj.url instanceof URL &&
'settings' in obj &&
!!obj.settings &&
typeof obj.settings === 'object');
return r;
}

export const __testing__ = {
addUniqueWordsToListAndSort,
};
2 changes: 1 addition & 1 deletion packages/cspell-config-lib/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { createReaderWriter } from './createReaderWriter.js';
export type { ICSpellConfigFile } from './CSpellConfigFile.js';
export { CSpellConfigFile } from './CSpellConfigFile.js';
export { CSpellConfigFile, satisfiesCSpellConfigFile } from './CSpellConfigFile.js';
export {
CSpellConfigFileInMemory,
CSpellConfigFileJavaScript,
Expand Down
11 changes: 6 additions & 5 deletions packages/cspell-lib/api/api.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 41 additions & 18 deletions packages/cspell-lib/src/lib/spellCheckFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'node:fs/promises';
import * as Path from 'node:path';
import { posix } from 'node:path';
import { fileURLToPath } from 'node:url';
import { fileURLToPath, pathToFileURL } from 'node:url';

import type { CSpellSettingsWithSourceTrace, CSpellUserSettings } from '@cspell/cspell-types';
import { ICSpellConfigFile } from 'cspell-config-lib';
import { describe, expect, test } from 'vitest';

import { pathPackageSamples, pathRepoTestFixtures } from '../test-util/index.mjs';
Expand Down Expand Up @@ -35,20 +37,21 @@ describe('Validate Spell Checking Files', () => {
}

test.each`
filename | settings | options | expected
${'src/not_found.c'} | ${{}} | ${{}} | ${{ checked: false, errors: [errNoEnt('src/not_found.c')] }}
${'src/sample.c'} | ${{}} | ${{}} | ${{ checked: true, issues: [], localConfigFilepath: es('.cspell.json'), errors: undefined }}
${'src/sample.c'} | ${{}} | ${{ noConfigSearch: true }} | ${{ checked: true, localConfigFilepath: undefined, errors: undefined }}
${'src/README.md'} | ${{}} | ${{}} | ${{ checked: true, issues: [], localConfigFilepath: es('.cspell.json'), errors: undefined }}
${__filename} | ${{}} | ${{ noConfigSearch: true }} | ${{ checked: true, localConfigFilepath: undefined, errors: undefined }}
${'src/sample.c'} | ${{ noConfigSearch: true }} | ${{}} | ${{ checked: true, localConfigFilepath: undefined, errors: undefined }}
${__filename} | ${{}} | ${{ configFile: rpS('../cspell.config.json') }} | ${{ checked: true, localConfigFilepath: es('../cspell.config.json'), errors: undefined }}
${__filename} | ${{ noConfigSearch: true }} | ${{ configFile: rpS('../cspell.config.json') }} | ${{ checked: true, localConfigFilepath: es('../cspell.config.json'), errors: undefined }}
${'src/sample.c'} | ${{ noConfigSearch: true }} | ${{ noConfigSearch: false }} | ${{ checked: true, localConfigFilepath: es('.cspell.json'), errors: undefined }}
${'src/sample.c'} | ${{}} | ${{}} | ${{ document: expect.anything(), errors: undefined }}
${'src/sample.c'} | ${{}} | ${{ configFile: rpS('../cSpell.json') }} | ${{ checked: false, localConfigFilepath: es('../cSpell.json'), errors: [eFailed(rpS('../cSpell.json'))] }}
${'src/not_found.c'} | ${{}} | ${{}} | ${{ checked: false, errors: [errNoEnt('src/not_found.c')] }}
${__filename} | ${{}} | ${{}} | ${{ checked: true, localConfigFilepath: es('../cspell.config.json'), errors: undefined }}
filename | settings | options | expected
${'src/not_found.c'} | ${{}} | ${{}} | ${{ checked: false, errors: [errNoEnt('src/not_found.c')] }}
${'src/sample.c'} | ${{}} | ${{}} | ${{ checked: true, issues: [], localConfigFilepath: es('.cspell.json'), errors: undefined }}
${'src/sample.c'} | ${{}} | ${{ noConfigSearch: true }} | ${{ checked: true, localConfigFilepath: undefined, errors: undefined }}
${'src/README.md'} | ${{}} | ${{}} | ${{ checked: true, issues: [], localConfigFilepath: es('.cspell.json'), errors: undefined }}
${__filename} | ${{}} | ${{ noConfigSearch: true }} | ${{ checked: true, localConfigFilepath: undefined, errors: undefined }}
${__filename} | ${toConfigFile({ noConfigSearch: true })} | ${{}} | ${{ checked: true, localConfigFilepath: undefined, errors: undefined }}
${'src/sample.c'} | ${{ noConfigSearch: true }} | ${{}} | ${{ checked: true, localConfigFilepath: undefined, errors: undefined }}
${__filename} | ${{}} | ${{ configFile: rpS('../cspell.config.json') }} | ${{ checked: true, localConfigFilepath: es('../cspell.config.json'), errors: undefined }}
${__filename} | ${{ noConfigSearch: true }} | ${{ configFile: rpS('../cspell.config.json') }} | ${{ checked: true, localConfigFilepath: es('../cspell.config.json'), errors: undefined }}
${'src/sample.c'} | ${{ noConfigSearch: true }} | ${{ noConfigSearch: false }} | ${{ checked: true, localConfigFilepath: es('.cspell.json'), errors: undefined }}
${'src/sample.c'} | ${{}} | ${{}} | ${{ document: expect.anything(), errors: undefined }}
${'src/sample.c'} | ${{}} | ${{ configFile: rpS('../cSpell.json') }} | ${{ checked: false, localConfigFilepath: es('../cSpell.json'), errors: [eFailed(rpS('../cSpell.json'))] }}
${'src/not_found.c'} | ${{}} | ${{}} | ${{ checked: false, errors: [errNoEnt('src/not_found.c')] }}
${__filename} | ${{}} | ${{}} | ${{ checked: true, localConfigFilepath: es('../cspell.config.json'), errors: undefined }}
`(
'spellCheckFile $filename $settings $options',
async ({ filename, settings, options, expected }: TestSpellCheckFile) => {
Expand Down Expand Up @@ -89,7 +92,7 @@ describe('Validate Determine settings', () => {
);
});

describe('Validate Spell Checking Documents', () => {
describe('Validate Spell Checking Documents', async () => {
interface TestSpellCheckFile {
uri: string;
text: string | undefined;
Expand Down Expand Up @@ -125,6 +128,8 @@ describe('Validate Spell Checking Documents', () => {
return words.map((text) => ({ text })).map((i) => expect.objectContaining(i));
}

const sampleConfigFile = await readConfigFile(rpS('../cspell.config.json'));

// cspell:ignore texxt eslintcache
test.each`
uri | text | settings | options | expected
Expand Down Expand Up @@ -161,9 +166,11 @@ describe('Validate Spell Checking Documents', () => {
expect(r).toEqual(oc(expected));
},
);

test.each`
uri | text | settings | options | expected
${f(tf('issues/issue-1775/hunspell/utf_info.hxx'))} | ${''} | ${{}} | ${{}} | ${{ checked: true, errors: undefined }}
uri | text | settings | options | expected
${f(tf('issues/issue-1775/hunspell/utf_info.hxx'))} | ${''} | ${{}} | ${{}} | ${{ checked: true, errors: undefined }}
${f(__filename)} | ${''} | ${sampleConfigFile} | ${{ noConfigSearch: true }} | ${{ checked: true, localConfigFilepath: undefined, errors: undefined }}
`(
'spellCheckFile fixtures $uri $settings $options',
async ({ uri, text, settings, options, expected }: TestSpellCheckFile) => {
Expand Down Expand Up @@ -322,3 +329,19 @@ function filterKeys<T extends object, K extends keyof T>(obj: T, keys: K[]): Pic
}
return result;
}

function toConfigFile(settings: CSpellUserSettings, url = 'cspell.json') {
return {
url: new URL(url, import.meta.url),
settings,
};
}

async function readConfigFile(filename: string): Promise<ICSpellConfigFile> {
const url = pathToFileURL(filename);
const settings = JSON.parse(await fs.readFile(url, 'utf8'));
return {
url: new URL(url),
settings,
};
}
31 changes: 20 additions & 11 deletions packages/cspell-lib/src/lib/spellCheckFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CSpellSettingsWithSourceTrace, CSpellUserSettings } from '@cspell/cspell-types';
import { ICSpellConfigFile, satisfiesCSpellConfigFile } from 'cspell-config-lib';

import type { Document, DocumentWithText } from './Document/index.js';
import { isBinaryDoc } from './Document/isBinaryDoc.js';
Expand Down Expand Up @@ -62,12 +63,12 @@ export interface SpellCheckFileResult {
export function spellCheckFile(
file: string | Uri | URL,
options: SpellCheckFileOptions,
settings: CSpellUserSettings,
settingsOrConfigFile: CSpellUserSettings | ICSpellConfigFile,
): Promise<SpellCheckFileResult> {
const doc: Document = {
uri: toUri(file).toString(),
};
return spellCheckDocument(doc, options, settings);
return spellCheckDocument(doc, options, settingsOrConfigFile);
}

/**
Expand All @@ -79,13 +80,16 @@ export function spellCheckFile(
export async function spellCheckDocument(
document: Document | DocumentWithText,
options: SpellCheckFileOptions,
settings: CSpellUserSettings,
settingsOrConfigFile: CSpellUserSettings | ICSpellConfigFile,
): Promise<SpellCheckFileResult> {
const settingsUsed = satisfiesCSpellConfigFile(settingsOrConfigFile)
? settingsOrConfigFile.settings
: settingsOrConfigFile;
if (isBinaryDoc(document)) {
return {
document,
options,
settingsUsed: settings,
settingsUsed,
localConfigFilepath: undefined,
issues: [],
checked: false,
Expand All @@ -99,14 +103,14 @@ export async function spellCheckDocument(
return {
document,
options,
settingsUsed: settings,
settingsUsed,
localConfigFilepath: undefined,
issues: [],
checked: false,
errors: undefined,
};
}
const result = await spellCheckFullDocument(doc, options, settings);
const result = await spellCheckFullDocument(doc, options, settingsOrConfigFile);
const perf = result.perf || {};
perf.loadTimeMs = timer.elapsed;
result.perf = perf;
Expand All @@ -116,7 +120,7 @@ export async function spellCheckDocument(
return {
document,
options,
settingsUsed: settings,
settingsUsed,
localConfigFilepath: undefined,
issues: [],
checked: false,
Expand All @@ -128,7 +132,7 @@ export async function spellCheckDocument(
async function spellCheckFullDocument(
document: DocumentWithText,
options: SpellCheckFileOptions,
settings: CSpellUserSettings,
settingsOrConfigFile: CSpellUserSettings | ICSpellConfigFile,
): Promise<SpellCheckFileResult> {
// if (options.skipValidation) {
// return {
Expand All @@ -149,8 +153,9 @@ async function spellCheckFullDocument(

const doc = documentToTextDocument(document);
const docValOptions: DocumentValidatorOptions = options;
const docValidator = new DocumentValidator(doc, docValOptions, settings);
await docValidator.prepare().finally(() => timerPrepare.end());
const docValidator = await DocumentValidator.create(doc, docValOptions, settingsOrConfigFile).finally(() =>
timerPrepare.end(),
);
Object.assign(perf, Object.fromEntries(Object.entries(docValidator.perfTiming).map(([k, v]) => ['_' + k, v])));

const prep = docValidator._getPreparations();
Expand All @@ -159,7 +164,11 @@ async function spellCheckFullDocument(
return {
document,
options,
settingsUsed: prep?.localConfig || settings,
settingsUsed:
prep?.localConfig ||
(satisfiesCSpellConfigFile(settingsOrConfigFile)
? settingsOrConfigFile.settings
: settingsOrConfigFile),
localConfigFilepath: prep?.localConfigFilepath,
issues: [],
checked: false,
Expand Down
30 changes: 30 additions & 0 deletions packages/cspell-lib/src/lib/suggestions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CSpellSettings } from '@cspell/cspell-types';
import { ICSpellConfigFile } from 'cspell-config-lib';
import { describe, expect, test } from 'vitest';

import type { SuggestedWord, SuggestionOptions } from './suggestions.js';
Expand All @@ -10,13 +12,16 @@ const ac = <T>(a: Array<T>) => expect.arrayContaining(a);
const timeout = 20_000;

describe('suggestions', () => {
const cfgFile = toConfigFile({ language: 'en-gb' });

test.each`
word | options | settings | expected
${'apple'} | ${undefined} | ${undefined} | ${ac([sug('apple', 0, ['en_us']), sug('Apple', 1, ['en_us', 'companies'])])}
${'apple'} | ${opt({ strict: false })} | ${undefined} | ${ac([sug('apple', 0, ['en_us', 'companies']), sug('Apple', 1, ['en_us', 'companies'])])}
${'apple'} | ${opt({ includeDefaultConfig: false })} | ${undefined} | ${[]}
${'apple'} | ${{}} | ${{}} | ${ac([sug('apple', 0, ['en_us']), sug('Apple', 1, ['en_us', 'companies'])])}
${'apple'} | ${{}} | ${{ language: 'en-gb' }} | ${ac([sug('apple', 0, ['en-gb']), sug('Apple', 1, ['companies'])])}
${'apple'} | ${{}} | ${cfgFile} | ${ac([sug('apple', 0, ['en-gb']), sug('Apple', 1, ['companies'])])}
${'apple'} | ${{ locale: 'en-gb' }} | ${undefined} | ${ac([sug('apple', 0, ['en-gb']), sug('Apple', 1, ['companies'])])}
${'apple'} | ${{ dictionaries: ['en-gb'] }} | ${undefined} | ${ac([sug('apple', 0, ['en-gb'])])}
${'apple'} | ${undefined} | ${{ dictionaries: ['en-gb'] }} | ${ac([sug('apple', 0, ['en_us', 'en-gb']), sug('Apple', 1, ['en_us', 'companies'])])}
Expand Down Expand Up @@ -65,8 +70,33 @@ describe('Suggestions English', async () => {
sug('Orange', 201),
]);
});

test('Orangges 2', async () => {
const results = await suggestionsForWord(
'orangges',
{ languageId: 'typescript' },
toConfigFile({ language: 'en-us' }),
);
expect(results.suggestions).toEqual([
sug('oranges', 100),
sug('ranges', 185),
sug('orangs', 190),
sug('orange', 200),
sug('orangey', 200),
sug('orangier', 200),
sug('orangiest'),
sug('Orange', 201),
]);
});
});

function toConfigFile(settings: CSpellSettings): ICSpellConfigFile {
return {
url: new URL('cspell.json', import.meta.url),
settings,
};
}

function opt(opt: Partial<SuggestionOptions>): SuggestionOptions {
return opt;
}
Expand Down
Loading
Loading