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 db0c1bd
Show file tree
Hide file tree
Showing 3 changed files with 10 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
12 changes: 5 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,15 @@ 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('[\\\\\\\\]');
});
});
});
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 db0c1bd

Please sign in to comment.