Skip to content

Commit e8a8f62

Browse files
hronSimenB
authored andcommitted
Improve escaping path separators for regexp on Windows (#6523)
1 parent d6e0a1b commit e8a8f62

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- `[jest-snapshot]` Introduce `toMatchInlineSnapshot` and `toThrowErrorMatchingInlineSnapshot` matchers ([#6380](https://github.com/facebook/jest/pull/6380))
66

7+
### Fixes
8+
9+
- `[jest-regex-util]` Improve handling already escaped path separators on Windows ([#6523](https://github.com/facebook/jest/pull/6523))
10+
711
### Chore & Maintenance
812

913
- `[website]` Switch domain to https://jestjs.io ([#6549](https://github.com/facebook/jest/pull/6549))

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,23 @@ 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 escape Windows path separator at the beginning', () => {
46+
expect(replacePathSepForRegex('\\a')).toBe('\\\\a');
47+
});
48+
49+
it('should not escape several already escaped path separators', () => {
50+
expect(replacePathSepForRegex('\\\\\\\\')).toBe('\\\\\\\\');
51+
});
4652
});
4753
});

Diff for: 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)