From bb9168535dc6d19e8e7c1a46923164aad21b6157 Mon Sep 17 00:00:00 2001 From: Aditya Date: Mon, 14 Sep 2020 16:25:28 +0530 Subject: [PATCH] refactor regex add test for context module refactor linting refactor test --- lib/ContextModule.js | 4 +++- .../cases/context/issue-10969/folder/file1.js | 0 test/cases/context/issue-10969/index.js | 21 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/cases/context/issue-10969/folder/file1.js create mode 100644 test/cases/context/issue-10969/index.js diff --git a/lib/ContextModule.js b/lib/ContextModule.js index 0e3f8cc69..ec0365d1a 100644 --- a/lib/ContextModule.js +++ b/lib/ContextModule.js @@ -144,7 +144,9 @@ class ContextModule extends Module { prettyRegExp(regexString) { // remove the "/" at the front and the beginning // "/foo/" -> "foo" - return regexString.substring(1, regexString.length - 1); + return regexString + .substring(1, regexString.length - 1) + .replace(/!/g, "%21"); } _createIdentifier() { diff --git a/test/cases/context/issue-10969/folder/file1.js b/test/cases/context/issue-10969/folder/file1.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/cases/context/issue-10969/index.js b/test/cases/context/issue-10969/index.js new file mode 100644 index 000000000..b0403cb8c --- /dev/null +++ b/test/cases/context/issue-10969/index.js @@ -0,0 +1,21 @@ +expect.extend({ + toBeValidModuleId(received, moduleIdString) { + const pass = typeof received === "number" || received === moduleIdString; + if (pass) { + return { + message: () => `expected ${received} not to be a valid module id`, + pass: true + }; + } else { + return { + message: () => `expected ${received} to be a valid module id`, + pass: false + }; + } + } +}); + +it("should replace ! with %21 in the module id string of the context module", function () { + const moduleId = require.context("./folder", true, /^(?!file1\.js$).*$/i, "lazy").id; + expect(moduleId).toBeValidModuleId("./context/issue-10969/folder lazy recursive ^(?%21file1\\.js$).*$/"); +});