Skip to content

Commit

Permalink
fix: escape square braces on Windows platform
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Oct 29, 2023
1 parent 91dc88a commit 8638dc6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ Returns the path with escaped special characters depending on the platform.
* `@+!` before the opening parenthesis;
* `\\` before non-special characters;
* Windows:
* `(){}`
* `(){}[]`
* `!` at the beginning of line;
* `@+!` before the opening parenthesis;
* Characters like `*?|[]` cannot be used in the path ([windows_naming_conventions][windows_naming_conventions]), so they will not be escaped;
* Characters like `*?|` cannot be used in the path ([windows_naming_conventions][windows_naming_conventions]), so they will not be escaped;

```js
fg.escapePath('!abc');
Expand All @@ -294,7 +294,7 @@ fg.win32.escapePath('C:\\Program Files (x86)\\**\\*');
Converts a path to a pattern depending on the platform, including special character escaping.

* Posix. Works similarly to the `fg.posix.escapePath` method.
* Windows. Works similarly to the `fg.win32.escapePath` method, additionally converting backslashes to forward slashes in cases where they are not escape characters (`!()+@{}`).
* Windows. Works similarly to the `fg.win32.escapePath` method, additionally converting backslashes to forward slashes in cases where they are not escape characters (`!()+@{}[]`).

```js
fg.convertPathToPattern('[OpenSource] mrmlnc – fast-glob (Deluxe Edition) 2014') + '/*.flac';
Expand Down
3 changes: 3 additions & 0 deletions src/utils/path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('Utils → Path', () => {
assert.strictEqual(util.escapeWindowsPath('!abc'), '\\!abc');
assert.strictEqual(util.escapeWindowsPath('()'), '\\(\\)');
assert.strictEqual(util.escapeWindowsPath('{}'), '\\{\\}');
assert.strictEqual(util.escapeWindowsPath('[]'), '\\[\\]');
assert.strictEqual(util.escapeWindowsPath('@('), '\\@\\(');
assert.strictEqual(util.escapeWindowsPath('!('), '\\!\\(');
assert.strictEqual(util.escapeWindowsPath('+('), '\\+\\(');
Expand Down Expand Up @@ -113,6 +114,8 @@ describe('Utils → Path', () => {
assert.strictEqual(util.convertWindowsPathToPattern('\\)\\'), '\\)/');
assert.strictEqual(util.convertWindowsPathToPattern('\\{\\'), '\\{/');
assert.strictEqual(util.convertWindowsPathToPattern('\\}\\'), '\\}/');
assert.strictEqual(util.convertWindowsPathToPattern('\\[\\'), '\\[/');
assert.strictEqual(util.convertWindowsPathToPattern('\\]\\'), '\\]/');

assert.strictEqual(util.convertWindowsPathToPattern('.\\*'), './*');
assert.strictEqual(util.convertWindowsPathToPattern('.\\**'), './**');
Expand Down
8 changes: 4 additions & 4 deletions src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const IS_WINDOWS_PLATFORM = os.platform() === 'win32';
const LEADING_DOT_SEGMENT_CHARACTERS_COUNT = 2; // ./ or .\\
/**
* All non-escaped special characters.
* Posix: ()*?[\]{|}, !+@ before (, ! at the beginning, \\ before non-special characters.
* Windows: (){}, !+@ before (, ! at the beginning.
* Posix: ()*?[]{|}, !+@ before (, ! at the beginning, \\ before non-special characters.
* Windows: (){}[], !+@ before (, ! at the beginning.
*/
const POSIX_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()*?[\]{|}]|^!|[!+@](?=\()|\\(?![!()*+?@[\]{|}]))/g;
const WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([(){}]|^!|[!+@](?=\())/g;
const WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()[\]{}]|^!|[!+@](?=\())/g;
/**
* The device path (\\.\ or \\?\).
* https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#dos-device-paths
Expand All @@ -22,7 +22,7 @@ const DOS_DEVICE_PATH_RE = /^\\\\([.?])/;
* Windows: !()+@{}
* https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
*/
const WINDOWS_BACKSLASHES_RE = /\\(?![!()+@{}])/g;
const WINDOWS_BACKSLASHES_RE = /\\(?![!()+@[\]{}])/g;

/**
* Designed to work only with simple paths: `dir\\file`.
Expand Down

0 comments on commit 8638dc6

Please sign in to comment.