Skip to content

Commit

Permalink
fix: Make sure files globs are normalized. (#5665)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored May 23, 2024
1 parent 992115f commit bb4b104
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { fileURLToPath, pathToFileURL } from 'node:url';

import { describe, expect, test } from 'vitest';

import { normalizeSettingsGlobs } from './normalizeRawSettings.js';

const pathToSettings = '/path/to/settings.json';
const defaultSettingsUrl = pathToFileURL(pathToSettings);
const defaultSettingsPath = fileURLToPath(defaultSettingsUrl);

describe('normalize settings', () => {
test.each`
settings | settingsUrl | expected
${{}} | ${defaultSettingsUrl} | ${{}}
${{ files: [], ignorePaths: [], id: 'id' }} | ${defaultSettingsUrl} | ${{ files: [], ignorePaths: [] }}
${{ files: [], ignorePaths: undefined }} | ${defaultSettingsUrl} | ${{ files: [] }}
${{ files: ['*.md'] }} | ${defaultSettingsUrl} | ${{ files: [{ glob: '*.md', source: defaultSettingsPath }] }}
${{ files: ['*.md'] }} | ${'vscode-vfs:///path/to/settings.json'} | ${{ files: [{ glob: '*.md', source: 'vscode-vfs:///path/to/settings.json' }] }}
${{ files: ['**/*.md'] }} | ${defaultSettingsUrl} | ${{ files: [{ glob: '**/*.md', source: defaultSettingsPath }] }}
${{ ignorePaths: ['**/*.md'] }} | ${defaultSettingsUrl} | ${{ ignorePaths: [{ glob: '**/*.md', source: defaultSettingsPath }] }}
`('normalizeSettingsGlobs $settings $settingsUrl', ({ settings, settingsUrl, expected }) => {
expect(normalizeSettingsGlobs(settings, new URL(settingsUrl))).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,31 @@ export function normalizeGitignoreRoot(
}

interface NormalizeSettingsGlobs {
files?: CSpellUserSettings['files'];
globRoot?: CSpellUserSettings['globRoot'];
ignorePaths?: CSpellUserSettings['ignorePaths'];
}
interface NormalizeSettingsGlobsResult {
ignorePaths?: GlobDef[];
files?: GlobDef[];
}
export function normalizeSettingsGlobs(
settings: NormalizeSettingsGlobs,
pathToSettingsFile: URL,
): NormalizeSettingsGlobsResult {
const { globRoot } = settings;
if (settings.ignorePaths === undefined) return {};

const ignorePaths = toGlobDef(settings.ignorePaths, globRoot, toFilePathOrHref(pathToSettingsFile));
return {
ignorePaths,
};
const normalized: NormalizeSettingsGlobsResult = {};

if (settings.ignorePaths) {
normalized.ignorePaths = toGlobDef(settings.ignorePaths, globRoot, toFilePathOrHref(pathToSettingsFile));
}

if (settings.files) {
normalized.files = toGlobDef(settings.files, globRoot, toFilePathOrHref(pathToSettingsFile));
}

return normalized;
}
export function normalizeCacheSettings(
settings: Pick<CSpellUserSettings, 'cache'>,
Expand Down

0 comments on commit bb4b104

Please sign in to comment.