From aa9fd9688870164ebf912cc32203940c94bb286f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juho=20Veps=C3=A4l=C3=A4inen?= Date: Sat, 12 Dec 2020 11:47:31 +0100 Subject: [PATCH] feat: Throw if trying to append/prepend non-arrays --- src/index.ts | 18 +++++-- test/merge-with-rules.test.ts | 98 +++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7fec961..af445b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -177,10 +177,14 @@ function mergeWithRule({ }); break; case CustomizeRule.Append: + const appendValue = last(bMatches)[k]; + + if (!isArray(v) || !isArray(appendValue)) { + throw new TypeError("Trying to append non-arrays"); + } + ret[k] = - bMatches.length > 0 - ? (v as Array).concat(last(bMatches)[k]) - : v; + bMatches.length > 0 ? (v as Array).concat(appendValue) : v; break; case CustomizeRule.Merge: const lastValue = last(bMatches)[k]; @@ -193,7 +197,13 @@ function mergeWithRule({ ret[k] = { ...v, ...lastValue }; break; case CustomizeRule.Prepend: - ret[k] = bMatches.length > 0 ? last(bMatches)[k].concat(v) : v; + const prependValue = last(bMatches)[k]; + + if (!isArray(v) || !isArray(prependValue)) { + throw new TypeError("Trying to prepend non-arrays"); + } + + ret[k] = bMatches.length > 0 ? prependValue.concat(v) : v; break; case CustomizeRule.Replace: ret[k] = bMatches.length > 0 ? last(bMatches)[k] : v; diff --git a/test/merge-with-rules.test.ts b/test/merge-with-rules.test.ts index 3b8b4e7..ea44ba6 100644 --- a/test/merge-with-rules.test.ts +++ b/test/merge-with-rules.test.ts @@ -723,6 +723,104 @@ describe("Merge with rules", function () { }); }); + it("should throw if trying to append non-arrays", function () { + const _mergeWithExplicitRule = mergeWithRules({ + module: { + rules: { + test: CustomizeRule.Match, + use: { + loader: CustomizeRule.Match, + options: CustomizeRule.Append, + }, + }, + }, + }); + const a = { + resolve: { extensions: [".js"] }, + module: { + rules: [ + { + test: /\.css$/, + use: [ + { loader: "style-loader", options: {} }, + { loader: "sass-loader" }, + ], + }, + ], + }, + }; + const b = { + resolve: { extensions: [".css"] }, + module: { + rules: [ + { + test: /\.css$/, + use: [ + { + loader: "style-loader", + options: {}, + }, + ], + }, + ], + }, + }; + + assert.throws(() => _mergeWithExplicitRule(a, b), { + name: "TypeError", + message: "Trying to append non-arrays", + }); + }); + + it("should throw if trying to prepend non-arrays", function () { + const _mergeWithExplicitRule = mergeWithRules({ + module: { + rules: { + test: CustomizeRule.Match, + use: { + loader: CustomizeRule.Match, + options: CustomizeRule.Prepend, + }, + }, + }, + }); + const a = { + resolve: { extensions: [".js"] }, + module: { + rules: [ + { + test: /\.css$/, + use: [ + { loader: "style-loader", options: {} }, + { loader: "sass-loader" }, + ], + }, + ], + }, + }; + const b = { + resolve: { extensions: [".css"] }, + module: { + rules: [ + { + test: /\.css$/, + use: [ + { + loader: "style-loader", + options: {}, + }, + ], + }, + ], + }, + }; + + assert.throws(() => _mergeWithExplicitRule(a, b), { + name: "TypeError", + message: "Trying to prepend non-arrays", + }); + }); + it("should work with multi-level match (#153)", function () { const a = { module: {