From 62535952fa605052c0a5e832ed403f2619d47efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 12 Sep 2017 09:50:21 +0200 Subject: [PATCH] Make FUNCTION_NAME_RESERVED_PATTERN stateless (#4466) * Make FUNCTION_NAME_RESERVED_PATTERN stateless * Ignore valid flow annotations in *.md files --- .eslintrc.js | 1 + packages/jest-mock/src/__tests__/jest_mock.test.js | 6 ++++++ packages/jest-mock/src/index.js | 8 ++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e0b85157b57e..6e181060dd40 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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, diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 08d369a2b791..4e68634a7922 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -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(); diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 0712f4551991..ab307207d5fb 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -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), { @@ -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 =