Skip to content
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
1 change: 1 addition & 0 deletions packages/cspell-glob/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`Validate index loads > API 1`] = `
"GlobMatcher": [Function],
"escapeGlobPath": [Function],
"fileOrGlobToGlob": [Function],
"globInFolder": [Function],
"isGlobPatternNormalized": [Function],
"isGlobPatternWithOptionalRoot": [Function],
"isGlobPatternWithRoot": [Function],
Expand Down
47 changes: 46 additions & 1 deletion packages/cspell-glob/src/globHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
__testing__,
escapeGlobPath,
fileOrGlobToGlob,
globInFolder,
normalizeGlobPattern,
normalizeGlobPatterns,
normalizeGlobToRoot,
Expand Down Expand Up @@ -609,12 +610,56 @@ describe('Validate cspell-glob assumptions', () => {
});
});

describe('globInFolder', () => {
test.each`
folder | glob | expected
${''} | ${'**'} | ${'**'}
${'src'} | ${'**'} | ${'src/**'}
${'src/'} | ${'*.md'} | ${'src/*.md'}
${'src'} | ${'/*.md'} | ${'src/*.md'}
${'src'} | ${'./x'} | ${'src/x'}
${'a/b'} | ${'**/x'} | ${'a/b/**/x'}
${'wt (1)'} | ${'**'} | ${'wt [(]1[)]/**'}
${'v #w'} | ${'**'} | ${'v [#]w/**'}
${' a'} | ${'**'} | ${'[ ]a/**'}
${'v '} | ${'**'} | ${'v[ ]/**'}
${'!imp/'} | ${'**'} | ${'@(!)imp/**'}
`('globInFolder($folder, $glob)', ({ folder, glob, expected }) => {
expect(globInFolder(folder, glob)).toBe(expected);
});
});

describe('rebasing a glob to a root inside its folders', () => {
// The matcher's root is below the glob's root. The rebased globs are a first-pass filter for a file search.
test.each`
glob | rootFolder | expected
${'wt [(]1[)]/*.md'} | ${'wt (1)'} | ${'*.md'}
${'wt [(]1[)]/*.md'} | ${'wt 1'} | ${'../wt [(]1[)]/*.md'}
${'src/*.md'} | ${'src'} | ${'*.md'}
${'src/**/*.md'} | ${'src'} | ${'**/*.md'}
${'[b]/*.md'} | ${'b'} | ${'../[b]/*.md'}
`('$glob rebased to the root $rootFolder is $expected', ({ glob, rootFolder, expected }) => {
const matcher = new GlobMatcher([{ glob, root: '/r' }], { root: `/r/${rootFolder}`, nodePath: posix });
expect(matcher.patternsNormalizedToRoot[0].glob).toBe(expected);
});

test.each`
glob | fromRootToGlob | fromGlobToRoot | expected
${'wt [(]1[)]/*.md'} | ${'../'} | ${'wt (1)/'} | ${'*.md'}
${'v [#]w/src/*.md'} | ${'../../'} | ${'v #w/src/'} | ${'*.md'}
${'a/wt [(]1[)]/**/*.md'} | ${'../../'} | ${'a/wt (1)/'} | ${'**/*.md'}
${'wt (1)/*.md'} | ${'../'} | ${'wt (1)/'} | ${'../wt (1)/*.md'}
`('rebaseGlob($glob, $fromRootToGlob, $fromGlobToRoot)', ({ glob, fromRootToGlob, fromGlobToRoot, expected }) => {
expect(rebaseGlob(glob, fromRootToGlob, fromGlobToRoot)).toBe(expected);
});
});

describe('normalizeGlobToRoot escapes the folder it adds', () => {
test.each(foldersWithSymbols)('folder "%s"', (folder) => {
const root = '/r/';
const glob: GlobPatternWithRoot = { glob: '*.md', root: `${root}${folder}/`, isGlobalPattern: false };
const result = normalizeGlobToRoot(glob, root, posix);
expect(result).toEqual({ ...glob, glob: escapeGlobPath(folder + '/') + '*.md', root });
expect(result).toEqual({ ...glob, glob: globInFolder(folder, '*.md'), root });

const matcher = new GlobMatcher(result.glob, { root, nodePath: posix });
const matched = foldersWithSymbols.filter((f) => matcher.match(`${root}${f}/readme.md`));
Expand Down
24 changes: 20 additions & 4 deletions packages/cspell-glob/src/globHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export function normalizeGlobToRoot<Glob extends GlobPatternWithRoot>(

return {
...glob,
glob: prefix + posix.join(escapeGlobPath(relGlob), g),
glob: prefix + globInFolder(relGlob, g),
root,
};
}
Expand Down Expand Up @@ -368,8 +368,9 @@ export function rebaseGlob(glob: string, fromRootToGlob: string, fromGlobToRoot:

// console.warn('rebaseGlob 1: %o', { glob, fromRootToGlob, fromGlobToRoot, relToParts, relFromParts });

if (glob.startsWith(fromGlobToRoot) && fromRootToGlob === '../'.repeat(relToParts.length - 1)) {
return glob.slice(fromGlobToRoot.length);
const globToRoot = escapeGlobPath(fromGlobToRoot);
if (glob.startsWith(globToRoot) && fromRootToGlob === '../'.repeat(relToParts.length - 1)) {
return glob.slice(globToRoot.length);
}

const lastRelIdx = relToParts.findIndex((s) => s !== '..');
Expand All @@ -392,7 +393,7 @@ export function rebaseGlob(glob: string, fromRootToGlob: string, fromGlobToRoot:
if (!relSeg || globSeg === '**') {
return globParts.slice(i).join('/');
}
if (relSeg !== globSeg && globSeg !== '*') {
if (escapeGlobPath(relSeg) !== globSeg && globSeg !== '*') {
break;
}
}
Expand Down Expand Up @@ -600,6 +601,21 @@ export function escapeGlobPath(path: string): string {
.replace(/\s$/, '[$&]');
}

/**
* Make a glob that matches inside a folder, such as `globInFolder('wt (1)', '**')`, which gives `wt [(]1[)]/**`.
*
* The folder's glob symbols are escaped, so the result matches only inside that folder.
*
* @param folder - a folder path that uses `/` as the separator. An empty path gives the glob unchanged.
* @param glob - the glob to match inside the folder.
* @returns the joined glob.
*/
export function globInFolder(folder: string, glob: string): string {
if (!folder) return glob;
const prefix = escapeGlobPath(folder.endsWith('/') ? folder.slice(0, -1) : folder);
return prefix + '/' + glob.replace(/^\.?\//, '');
}

export function workaroundPicomatchBug(glob: string): string {
const obj: Record<string, string> = {};
return glob
Expand Down
1 change: 1 addition & 0 deletions packages/cspell-glob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type { NormalizeOptions } from './globHelper.ts';
export {
escapeGlobPath,
fileOrGlobToGlob,
globInFolder,
isGlobPatternNormalized,
isGlobPatternWithOptionalRoot,
isGlobPatternWithRoot,
Expand Down
7 changes: 6 additions & 1 deletion packages/cspell/src/util/glob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { minimatch } from 'minimatch';
import { glob as tinyGlob, isDynamicPattern } from 'tinyglobby';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';

import { calcGlobs, normalizeGlobsToRoot } from './glob.js';
import { calcGlobs, normalizeFileOrGlobsToRoot, normalizeGlobsToRoot } from './glob.js';

interface MinimatchOptions {
windowsPathsNoEscape?: boolean;
Expand Down Expand Up @@ -266,6 +266,11 @@ describe('Validate tinyglobby assumptions', () => {
expect(found).toEqual(['t']);
});

test.each(['v #w', 'v w', ' a', 'a'])('the folder "%s" named on the command line', async (folder) => {
const globs = await normalizeFileOrGlobsToRoot([folder], escRoot);
expect(await tinyGlob(globs, { cwd: escRoot, dot: true })).toEqual([folder + '/file.txt']);
});

test.each(folders)('ignore a glob rooted in folder "%s"', async (folder) => {
const globs = normalizeGlobsToRoot([{ glob: '/**', root: path.join(escRoot, folder) }], escRoot, true);
expect(await findSkipped(globs)).toEqual([folder]);
Expand Down
5 changes: 2 additions & 3 deletions packages/cspell/src/util/glob.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { promises as fs } from 'node:fs';
import * as path from 'node:path';
import { posix } from 'node:path';

import type { CSpellUserSettings, Glob } from '@cspell/cspell-types';
import type { GlobPatternWithRoot } from 'cspell-glob';
import { fileOrGlobToGlob, GlobMatcher, workaroundPicomatchBug } from 'cspell-glob';
import { fileOrGlobToGlob, globInFolder, GlobMatcher, workaroundPicomatchBug } from 'cspell-glob';
import type { GlobOptions as TinyGlobbyOptions } from 'tinyglobby';
import { glob as tinyGlob } from 'tinyglobby';

Expand Down Expand Up @@ -173,7 +172,7 @@ async function adjustPossibleDirectory(glob: Glob, root: string): Promise<Glob>
try {
const stat = await fs.stat(dirPath);
if (stat.isDirectory()) {
const useGlob = posix.join(posixPath(g.glob), '**');
const useGlob = globInFolder(posixPath(g.glob), '**');
return typeof glob === 'string' ? useGlob : { ...glob, glob: useGlob };
}
} catch {
Expand Down