Skip to content

Commit 66b901c

Browse files
committed
Improve escaping path separators for regexp on Windows
1 parent d1ce3cd commit 66b901c

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([#6398](https://github.com/facebook/jest/pull/6398))
1515
- `[jest-util]` `console.timeEnd` now properly log elapsed time in milliseconds. ([#6456](https://github.com/facebook/jest/pull/6456))
1616
- `[jest-mock]` Fix `MockNativeMethods` access in react-native `jest.mock()` ([#6505](https://github.com/facebook/jest/pull/6505))
17+
- `[jest-regex-util]` Improve handling already escaped path separators on Windows ([#6523](https://github.com/facebook/jest/pull/6523))
1718

1819
### Chore & Maintenance
1920

packages/jest-regex-util/src/__tests__/index.test.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@ describe('replacePathSepForRegex()', () => {
3131

3232
it('should not escape an escaped dot', () => {
3333
expect(replacePathSepForRegex('a\\.dotfile')).toBe('a\\.dotfile');
34-
35-
// If we expect Windows path separators to be escaped, one would expect
36-
// the regular expression "\\\." to be unescaped as "\.". This is not the
37-
// current behavior.
38-
expect(replacePathSepForRegex('a\\\\\\.dotfile')).toBe(
39-
'a\\\\\\\\\\.dotfile',
40-
);
34+
expect(replacePathSepForRegex('a\\\\\\.dotfile')).toBe('a\\\\\\.dotfile');
4135
});
4236

4337
it('should not escape an escaped regexp symbol', () => {
4438
expect(replacePathSepForRegex('b\\(86')).toBe('b\\(86');
4539
});
40+
41+
it('should escape Windows path separators inside groups', () => {
42+
expect(replacePathSepForRegex('[/\\\\]')).toBe('[\\\\\\\\]');
43+
});
44+
45+
it('should not escape several already escaped path separators', () => {
46+
expect(replacePathSepForRegex('\\\\\\\\')).toBe('\\\\\\\\');
47+
});
4648
});
4749
});

packages/jest-regex-util/src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export const escapeStrForRegex = (string: string) =>
2323

2424
export const replacePathSepForRegex = (string: string) => {
2525
if (path.sep === '\\') {
26-
return string.replace(/(\/|\\(?![[\]{}()*+?.^$|]))/g, '\\\\');
26+
return string.replace(
27+
/(\/|(.)\\(?![[\]{}()*+?.^$|\\]))/g,
28+
(_match, p1, p2) => (p2 && p2 !== '\\' ? p2 + '\\\\' : '\\\\'),
29+
);
2730
}
2831
return string;
2932
};

0 commit comments

Comments
 (0)