diff --git a/lib/types/library/LibraryFormatter.js b/lib/types/library/LibraryFormatter.js index 86e66e0b8..c3713ac66 100644 --- a/lib/types/library/LibraryFormatter.js +++ b/lib/types/library/LibraryFormatter.js @@ -276,10 +276,7 @@ class LibraryFormatter extends AbstractUi5Formatter { // Remove base path from fsPath const posixBasePath = this.getSourceBasePath(true); - // Can match /library/src as well as /library/src/some/namespace - const basePathPrefixRegExp = new RegExp(`^${posixBasePath}/?`); - - if (!basePathPrefixRegExp.test(posixFsPath)) { + if (!posixFsPath.startsWith(posixBasePath)) { if (posixBasePath === posixFsPath + "/") { // The given file system path does not contain a namespace path // It is equal to the source base path @@ -289,7 +286,11 @@ class LibraryFormatter extends AbstractUi5Formatter { throw new Error(`Given file system path ${posixFsPath} is not based on source base ` + `path ${posixBasePath}.`); } - const namespacePath = posixFsPath.replace(basePathPrefixRegExp, ""); + let namespacePath = posixFsPath.replace(posixBasePath, ""); + if (namespacePath.startsWith("/")) { + // Remove any leading slash + namespacePath = namespacePath.substr(1); + } return namespacePath; } diff --git a/test/lib/types/library/LibraryFormatter.js b/test/lib/types/library/LibraryFormatter.js index c7b372157..0033d30b5 100644 --- a/test/lib/types/library/LibraryFormatter.js +++ b/test/lib/types/library/LibraryFormatter.js @@ -1459,6 +1459,20 @@ test("getNamespaceFromFsPath: fsPath is not based on base path", async (t) => { "Threw with correct error message"); }); +test("getNamespaceFromFsPath: fsPath w/ regex metacharacters", async (t) => { + const myProject = clone(libraryETree); + myProject.resources.pathMappings = { + "/resources/": myProject.resources.configuration.paths.src + }; + + const libraryFormatter = new LibraryFormatter({project: myProject}); + sinon.stub(libraryFormatter, "getSourceBasePath").returns("/some/(path"); + + const fsPath = "/some/(path/my/namespace"; + const res = libraryFormatter.getNamespaceFromFsPath(fsPath); + t.deepEqual(res, "my/namespace", "Returned correct namespace"); +}); + test.serial("getPreloadExcludesFromDotLibrary: No excludes", async (t) => { const log = { verbose: sinon.stub()