From 93a953aec124ca9f93de32e9b0fcf532a34f660f Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Tue, 7 Jan 2025 23:59:58 +0300 Subject: [PATCH] fix: remove backslashes from the pattern's base directory --- src/managers/tasks.spec.ts | 10 ++++++++++ src/managers/tasks.ts | 8 +++++++- src/utils/path.spec.ts | 7 +++++++ src/utils/path.ts | 4 ++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/managers/tasks.spec.ts b/src/managers/tasks.spec.ts index 49081971..6d4f23a2 100644 --- a/src/managers/tasks.spec.ts +++ b/src/managers/tasks.spec.ts @@ -179,6 +179,16 @@ describe('Managers → Task', () => { assert.deepStrictEqual(actual, expected); }); + + it('should remove backslashes from the base directory', () => { + const expected: PatternsGroup = { + "a'b": [String.raw`a\'b/*`], + }; + + const actual = manager.groupPatternsByBaseDirectory([String.raw`a\'b/*`]); + + assert.deepStrictEqual(actual, expected); + }); }); describe('.convertPatternGroupsToTasks', () => { diff --git a/src/managers/tasks.ts b/src/managers/tasks.ts index dab09743..c574c742 100644 --- a/src/managers/tasks.ts +++ b/src/managers/tasks.ts @@ -104,7 +104,13 @@ export function groupPatternsByBaseDirectory(patterns: Pattern[]): PatternsGroup const group: PatternsGroup = {}; return patterns.reduce((collection, pattern) => { - const base = utils.pattern.getBaseDirectory(pattern); + let base = utils.pattern.getBaseDirectory(pattern); + + /** + * After extracting the basic static part of the pattern, it becomes a path, + * so escaping leads to referencing non-existent paths. + */ + base = utils.path.removeBackslashes(base); if (base in collection) { collection[base].push(pattern); diff --git a/src/utils/path.spec.ts b/src/utils/path.spec.ts index 28c39dd0..c4198c3b 100644 --- a/src/utils/path.spec.ts +++ b/src/utils/path.spec.ts @@ -81,6 +81,13 @@ describe('Utils → Path', () => { }); }); + describe('.removeBackslashes', () => { + it('should return path without backslashes', () => { + assert.strictEqual(util.removeBackslashes(String.raw`a\b`), 'ab'); + assert.strictEqual(util.removeBackslashes(String.raw`a\\\b`), String.raw`ab`); + }); + }); + describe('.convertPathToPattern', () => { it('should return a pattern', () => { assert.strictEqual(util.convertPathToPattern('.{directory}'), String.raw`.\{directory\}`); diff --git a/src/utils/path.ts b/src/utils/path.ts index 5ac8f837..c84aaee4 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -42,6 +42,10 @@ export function removeLeadingDotSegment(entry: string): string { return entry; } +export function removeBackslashes(entry: string): string { + return entry.replaceAll('\\', ''); +} + export const escape = IS_WINDOWS_PLATFORM ? escapeWindowsPath : escapePosixPath; export function escapeWindowsPath(pattern: Pattern): Pattern {