Skip to content

Commit

Permalink
Improve escaping path separators for regexp on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
hron committed Jun 22, 2018
1 parent d1ce3cd commit 66b901c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- `[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))
- `[jest-util]` `console.timeEnd` now properly log elapsed time in milliseconds. ([#6456](https://github.com/facebook/jest/pull/6456))
- `[jest-mock]` Fix `MockNativeMethods` access in react-native `jest.mock()` ([#6505](https://github.com/facebook/jest/pull/6505))
- `[jest-regex-util]` Improve handling already escaped path separators on Windows ([#6523](https://github.com/facebook/jest/pull/6523))

### Chore & Maintenance

Expand Down
16 changes: 9 additions & 7 deletions packages/jest-regex-util/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ describe('replacePathSepForRegex()', () => {

it('should not escape an escaped dot', () => {
expect(replacePathSepForRegex('a\\.dotfile')).toBe('a\\.dotfile');

// If we expect Windows path separators to be escaped, one would expect
// the regular expression "\\\." to be unescaped as "\.". This is not the
// current behavior.
expect(replacePathSepForRegex('a\\\\\\.dotfile')).toBe(
'a\\\\\\\\\\.dotfile',
);
expect(replacePathSepForRegex('a\\\\\\.dotfile')).toBe('a\\\\\\.dotfile');
});

it('should not escape an escaped regexp symbol', () => {
expect(replacePathSepForRegex('b\\(86')).toBe('b\\(86');
});

it('should escape Windows path separators inside groups', () => {
expect(replacePathSepForRegex('[/\\\\]')).toBe('[\\\\\\\\]');
});

it('should not escape several already escaped path separators', () => {
expect(replacePathSepForRegex('\\\\\\\\')).toBe('\\\\\\\\');
});
});
});
5 changes: 4 additions & 1 deletion packages/jest-regex-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export const escapeStrForRegex = (string: string) =>

export const replacePathSepForRegex = (string: string) => {
if (path.sep === '\\') {
return string.replace(/(\/|\\(?![[\]{}()*+?.^$|]))/g, '\\\\');
return string.replace(
/(\/|(.)\\(?![[\]{}()*+?.^$|\\]))/g,
(_match, p1, p2) => (p2 && p2 !== '\\' ? p2 + '\\\\' : '\\\\'),
);
}
return string;
};

0 comments on commit 66b901c

Please sign in to comment.