diff --git a/packages/cspell-glob/src/__snapshots__/index.test.ts.snap b/packages/cspell-glob/src/__snapshots__/index.test.ts.snap index fca95f20302..bd97f891972 100644 --- a/packages/cspell-glob/src/__snapshots__/index.test.ts.snap +++ b/packages/cspell-glob/src/__snapshots__/index.test.ts.snap @@ -5,6 +5,7 @@ exports[`Validate index loads > API 1`] = ` "GlobMatcher": [Function], "escapeGlobPath": [Function], "fileOrGlobToGlob": [Function], + "globInFolder": [Function], "isGlobPatternNormalized": [Function], "isGlobPatternWithOptionalRoot": [Function], "isGlobPatternWithRoot": [Function], diff --git a/packages/cspell-glob/src/globHelper.test.ts b/packages/cspell-glob/src/globHelper.test.ts index 46f290370ec..5fb21d11c1b 100644 --- a/packages/cspell-glob/src/globHelper.test.ts +++ b/packages/cspell-glob/src/globHelper.test.ts @@ -10,6 +10,7 @@ import { __testing__, escapeGlobPath, fileOrGlobToGlob, + globInFolder, normalizeGlobPattern, normalizeGlobPatterns, normalizeGlobToRoot, @@ -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`)); diff --git a/packages/cspell-glob/src/globHelper.ts b/packages/cspell-glob/src/globHelper.ts index 15aebba9a1d..fc1ecb78602 100644 --- a/packages/cspell-glob/src/globHelper.ts +++ b/packages/cspell-glob/src/globHelper.ts @@ -330,7 +330,7 @@ export function normalizeGlobToRoot( return { ...glob, - glob: prefix + posix.join(escapeGlobPath(relGlob), g), + glob: prefix + globInFolder(relGlob, g), root, }; } @@ -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 !== '..'); @@ -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; } } @@ -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 = {}; return glob diff --git a/packages/cspell-glob/src/index.ts b/packages/cspell-glob/src/index.ts index 5ec9f70d41b..ce6de1337b3 100644 --- a/packages/cspell-glob/src/index.ts +++ b/packages/cspell-glob/src/index.ts @@ -2,6 +2,7 @@ export type { NormalizeOptions } from './globHelper.ts'; export { escapeGlobPath, fileOrGlobToGlob, + globInFolder, isGlobPatternNormalized, isGlobPatternWithOptionalRoot, isGlobPatternWithRoot, diff --git a/packages/cspell/src/util/glob.test.ts b/packages/cspell/src/util/glob.test.ts index 4e2499d6eae..ada01d7986a 100644 --- a/packages/cspell/src/util/glob.test.ts +++ b/packages/cspell/src/util/glob.test.ts @@ -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; @@ -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]); diff --git a/packages/cspell/src/util/glob.ts b/packages/cspell/src/util/glob.ts index a25a4928f23..7ed65f99b9a 100644 --- a/packages/cspell/src/util/glob.ts +++ b/packages/cspell/src/util/glob.ts @@ -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'; @@ -173,7 +172,7 @@ async function adjustPossibleDirectory(glob: Glob, root: string): Promise 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 {