From a083806528e896b8ef1ea93a94c5852e5394bc49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=F0=9F=8E=83=20Khripkov?= Date: Mon, 29 Apr 2024 02:57:35 +0300 Subject: [PATCH] interpolateName.js: fix unreachable code; add tests --- lib/interpolateName.js | 5 +++-- test/interpolateName.test.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/interpolateName.js b/lib/interpolateName.js index 267dd2c..60a898c 100644 --- a/lib/interpolateName.js +++ b/lib/interpolateName.js @@ -51,9 +51,10 @@ function interpolateName(loaderContext, name, options = {}) { directory = resourcePath.replace(/\\/g, "/").replace(/\.\.(\/)?/g, "_$1"); } - if (directory.length === 1) { + if (directory.length <= 1) { directory = ""; - } else if (directory.length > 1) { + } else { + // directory.length > 1 folder = path.basename(directory); } } diff --git a/test/interpolateName.test.js b/test/interpolateName.test.js index d84bf8e..77d9a17 100644 --- a/test/interpolateName.test.js +++ b/test/interpolateName.test.js @@ -308,11 +308,21 @@ describe("interpolateName()", () => { "/path/to/", "should interpolate [path] token", ], + [ + [loaderContext, "[path]", { context: "/path/" }], + "to/", + "should interpolate [path] token with context", + ], [ [loaderContext, "[folder]", {}], "to", "should interpolate [folder] token", ], + [ + [{ resourcePath: "." }, "[folder]", {}], + "", + "should interpolate [folder] token with short resourcePath", + ], ]); });