Skip to content

Commit

Permalink
Make FUNCTION_NAME_RESERVED_PATTERN stateless (#4466)
Browse files Browse the repository at this point in the history
* Make FUNCTION_NAME_RESERVED_PATTERN stateless

* Ignore valid flow annotations in *.md files
  • Loading branch information
thymikee authored and cpojer committed Sep 12, 2017
1 parent 19b5cce commit 72af99d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
files: ['*.md'],
rules: {
'consistent-return': 0,
'flowtype/require-valid-file-annotation': 0,
'import/no-extraneous-dependencies': 0,
'import/no-unresolved': 0,
'jest/no-focused-tests': 0,
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ describe('moduleMocker', () => {
expect(
getMockFnWithOriginalName('foo-bar').name === 'foo$bar',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo-bar-2').name === 'foo$bar$2',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo-bar-3').name === 'foo$bar$3',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo/bar').name === 'foo$bar',
).toBeTruthy();
Expand Down
8 changes: 6 additions & 2 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ type MockFunctionConfig = {

const MOCK_CONSTRUCTOR_NAME = 'mockConstructor';

const FUNCTION_NAME_RESERVED_PATTERN = /[\s!-\/:-@\[-`{-~]/g;
const FUNCTION_NAME_RESERVED_PATTERN = /[\s!-\/:-@\[-`{-~]/;
const FUNCTION_NAME_RESERVED_REPLACE = new RegExp(
FUNCTION_NAME_RESERVED_PATTERN.source,
'g',
);

// $FlowFixMe
const RESERVED_KEYWORDS = Object.assign(Object.create(null), {
Expand Down Expand Up @@ -477,7 +481,7 @@ class ModuleMockerClass {
// It's also a syntax error to define a function with a reserved character
// as part of it's name.
if (FUNCTION_NAME_RESERVED_PATTERN.test(name)) {
name = name.replace(FUNCTION_NAME_RESERVED_PATTERN, '$');
name = name.replace(FUNCTION_NAME_RESERVED_REPLACE, '$');
}

const body =
Expand Down

0 comments on commit 72af99d

Please sign in to comment.