Skip to content

Commit

Permalink
feat: Throw if trying to merge non-objects
Browse files Browse the repository at this point in the history
  • Loading branch information
bebraw committed Dec 12, 2020
1 parent 8a7d155 commit 157b9a0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,14 @@ function mergeWithRule({
: v;
break;
case CustomizeRule.Merge:
// @ts-ignore: The assumption here is that both are objects
ret[k] = { ...v, ...last(bMatches)[k] };
const lastValue = last(bMatches)[k];

if (!isPlainObject(v) || !isPlainObject(lastValue)) {
throw new TypeError("Trying to merge non-objects");
}

// @ts-ignore: These should be objects now
ret[k] = { ...v, ...lastValue };
break;
case CustomizeRule.Prepend:
ret[k] = bMatches.length > 0 ? last(bMatches)[k].concat(v) : v;
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function isFunction(functionToCheck) {
}

function isPlainObject(a) {
if (a === null) {
if (a === null || Array.isArray(a)) {
return false;
}

Expand Down
49 changes: 49 additions & 0 deletions test/merge-with-rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,55 @@ describe("Merge with rules", function () {
expect(_mergeWithExplicitRule(a, b)).toEqual(result);
});

it("should throw if trying to merge non-objects", function () {
const _mergeWithExplicitRule = mergeWithRules({
module: {
rules: {
test: CustomizeRule.Match,
use: {
loader: CustomizeRule.Match,
options: CustomizeRule.Merge,
},
},
},
});
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 merge non-objects",
});
});

it("should work with multi-level match (#153)", function () {
const a = {
module: {
Expand Down

0 comments on commit 157b9a0

Please sign in to comment.