Skip to content

Commit 72cf5f6

Browse files
authored
Merge pull request webpack#3175 from BrainCrumbz/feat/ruleset-error-context
feat(ruleset): show context in error when `exclude` holds undefined item
2 parents d745146 + f2c0e59 commit 72cf5f6

2 files changed

Lines changed: 89 additions & 5 deletions

File tree

lib/RuleSet.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<condition>: { and: [<condition>] }
5252
<condition>: { or: [<condition>] }
5353
<condition>: { not: [<condition>] }
54-
<condition>: { test: <condition>, include: <condition>, exclude: <codition> }
54+
<condition>: { test: <condition>, include: <condition>, exclude: <condition> }
5555
5656
5757
normalized:
@@ -106,23 +106,37 @@ RuleSet.normalizeRule = function(rule) {
106106
var newRule = {};
107107
var useSource;
108108
var resourceSource;
109+
var condition;
109110

110111
if(rule.test || rule.include || rule.exclude) {
111112
checkResourceSource("test + include + exclude");
112-
newRule.resource = RuleSet.normalizeCondition({
113+
condition = {
113114
test: rule.test,
114115
include: rule.include,
115116
exclude: rule.exclude
116-
});
117+
};
118+
try {
119+
newRule.resource = RuleSet.normalizeCondition(condition);
120+
} catch (error) {
121+
throw new Error(RuleSet.buildErrorMessage(condition, error));
122+
}
117123
}
118124

119125
if(rule.resource) {
120126
checkResourceSource("resource");
121-
newRule.resource = RuleSet.normalizeCondition(rule.resource);
127+
try {
128+
newRule.resource = RuleSet.normalizeCondition(rule.resource);
129+
} catch (error) {
130+
throw new Error(RuleSet.buildErrorMessage(rule.resource, error));
131+
}
122132
}
123133

124134
if(rule.issuer) {
125-
newRule.issuer = RuleSet.normalizeCondition(rule.issuer);
135+
try {
136+
newRule.issuer = RuleSet.normalizeCondition(rule.issuer);
137+
} catch (error) {
138+
throw new Error(RuleSet.buildErrorMessage(rule.issuer, error));
139+
}
126140
}
127141

128142
if(rule.loader && rule.loaders)
@@ -179,6 +193,14 @@ RuleSet.normalizeRule = function(rule) {
179193
return newRule;
180194
};
181195

196+
RuleSet.buildErrorMessage = function buildErrorMessage(condition, error) {
197+
var conditionAsText = JSON.stringify(condition, function(key, value) {
198+
return (value === undefined) ? "undefined" : value;
199+
}, 2)
200+
var message = error.message + " in " + conditionAsText;
201+
return message;
202+
};
203+
182204
RuleSet.normalizeUse = function normalizeUse(use) {
183205
if(Array.isArray(use)) {
184206
return use.map(RuleSet.normalizeUse).reduce(function(arr, items) {

test/RuleSet.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,66 @@ describe("RuleSet", function() {
268268
(match(loader, 'style.css')).should.eql(['css']);
269269
}, /No loader specified/)
270270
});
271+
describe('when exclude array holds an undefined item', function() {
272+
function errorHasContext(err) {
273+
if (/Expected condition but got falsy value/.test(err)
274+
&& /test/.test(err)
275+
&& /include/.test(err)
276+
&& /exclude/.test(err)
277+
&& /node_modules/.test(err)
278+
&& /undefined/.test(err)) {
279+
return true;
280+
}
281+
}
282+
it('should throw with context', function() {
283+
should.throws(function() {
284+
var loader = new RuleSet([{
285+
test: /\.css$/,
286+
loader: 'css',
287+
include: [
288+
'src',
289+
],
290+
exclude: [
291+
'node_modules',
292+
undefined,
293+
],
294+
}]);
295+
(match(loader, 'style.css')).should.eql(['css']);
296+
}, errorHasContext)
297+
});
298+
it('in resource should throw with context', function() {
299+
should.throws(function() {
300+
var loader = new RuleSet([{
301+
resource: {
302+
test: /\.css$/,
303+
include: [
304+
'src',
305+
],
306+
exclude: [
307+
'node_modules',
308+
undefined,
309+
],
310+
},
311+
}]);
312+
(match(loader, 'style.css')).should.eql(['css']);
313+
}, errorHasContext)
314+
});
315+
it('in issuer should throw with context', function() {
316+
should.throws(function() {
317+
var loader = new RuleSet([{
318+
issuer: {
319+
test: /\.css$/,
320+
include: [
321+
'src',
322+
],
323+
exclude: [
324+
'node_modules',
325+
undefined,
326+
],
327+
},
328+
}]);
329+
(match(loader, 'style.css')).should.eql(['css']);
330+
}, errorHasContext)
331+
});
332+
});
271333
});

0 commit comments

Comments
 (0)