Skip to content

Commit a5487ec

Browse files
authored
Merge pull request #206 from simonihmig/matchId-regex
Support RegExp for matchId
2 parents 0d464a1 + 24b7ef2 commit a5487ec

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

addon/index.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ setupDeprecationWorkflow({
2626
let postamble = ` ]
2727
});`;
2828

29+
function matchesWorkflow(matcher, value) {
30+
return (
31+
(typeof matcher === 'string' && matcher === value) ||
32+
(matcher instanceof RegExp && matcher.exec(value))
33+
);
34+
}
35+
2936
export function detectWorkflow(config, message, options) {
3037
if (!config || !config.workflow) {
3138
return;
@@ -37,11 +44,10 @@ export function detectWorkflow(config, message, options) {
3744
matcher = workflow.matchMessage;
3845
idMatcher = workflow.matchId;
3946

40-
if (typeof idMatcher === 'string' && options && idMatcher === options.id) {
41-
return workflow;
42-
} else if (typeof matcher === 'string' && matcher === message) {
43-
return workflow;
44-
} else if (matcher instanceof RegExp && matcher.exec(message)) {
47+
if (
48+
matchesWorkflow(idMatcher, options?.id) ||
49+
matchesWorkflow(matcher, message)
50+
) {
4551
return workflow;
4652
}
4753
}

tests/unit/handle-deprecation-workflow-test.js

+78
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ module('handleDeprecationWorkflow', function (hooks) {
8787

8888
test('deprecation silenced with string matcher', function (assert) {
8989
const config = {
90+
throwOnUnhandled: true,
9091
workflow: [{ matchMessage: 'Interesting', handler: 'silence' }],
9192
};
9293

@@ -113,6 +114,7 @@ module('handleDeprecationWorkflow', function (hooks) {
113114
};
114115

115116
const config = {
117+
throwOnUnhandled: true,
116118
workflow: [{ matchMessage: message, handler: 'log' }],
117119
};
118120

@@ -151,6 +153,7 @@ module('handleDeprecationWorkflow', function (hooks) {
151153

152154
test('deprecation silenced with regex matcher', function (assert) {
153155
const config = {
156+
throwOnUnhandled: true,
154157
workflow: [{ matchMessage: /Inter/, handler: 'silence' }],
155158
};
156159

@@ -184,6 +187,7 @@ module('handleDeprecationWorkflow', function (hooks) {
184187
};
185188

186189
const config = {
190+
throwOnUnhandled: true,
187191
workflow: [{ matchMessage: /Inter/, handler: 'log' }],
188192
};
189193

@@ -245,6 +249,7 @@ module('handleDeprecationWorkflow', function (hooks) {
245249

246250
test('deprecation silenced with id matcher', function (assert) {
247251
const config = {
252+
throwOnUnhandled: true,
248253
workflow: [{ matchId: 'ember.deprecation-workflow', handler: 'silence' }],
249254
};
250255

@@ -278,6 +283,7 @@ module('handleDeprecationWorkflow', function (hooks) {
278283
};
279284

280285
const config = {
286+
throwOnUnhandled: true,
281287
workflow: [{ matchId: 'ember.deprecation-workflow', handler: 'log' }],
282288
};
283289

@@ -312,4 +318,76 @@ module('handleDeprecationWorkflow', function (hooks) {
312318
);
313319
}, 'deprecation throws');
314320
});
321+
322+
test('deprecation silenced with id regex', function (assert) {
323+
const config = {
324+
throwOnUnhandled: true,
325+
workflow: [{ matchId: /^ember\..*/, handler: 'silence' }],
326+
};
327+
328+
handleDeprecationWorkflow(
329+
config,
330+
'Slightly interesting',
331+
{
332+
id: 'ember.deprecation-workflow',
333+
since: 'the beginning',
334+
until: '3.0.0',
335+
for: 'testing',
336+
},
337+
() => {},
338+
);
339+
340+
assert.ok(true, 'Deprecation did not raise');
341+
});
342+
343+
// eslint-disable-next-line qunit/require-expect
344+
test('deprecation logs with id regex', function (assert) {
345+
assert.expect(1);
346+
347+
let message = 'Slightly interesting';
348+
349+
console.warn = function (passedMessage) {
350+
assert.strictEqual(
351+
passedMessage,
352+
'DEPRECATION: ' + message,
353+
'deprecation logs',
354+
);
355+
};
356+
357+
const config = {
358+
throwOnUnhandled: true,
359+
workflow: [{ matchId: /^ember\..*/, handler: 'log' }],
360+
};
361+
362+
handleDeprecationWorkflow(
363+
config,
364+
'Slightly interesting',
365+
{
366+
id: 'ember.deprecation-workflow',
367+
since: 'the beginning',
368+
until: '3.0.0',
369+
for: 'testing',
370+
},
371+
() => {},
372+
);
373+
});
374+
375+
test('deprecation thrown with id regex', function (assert) {
376+
const config = {
377+
workflow: [{ matchId: /^ember\..*/, handler: 'throw' }],
378+
};
379+
assert.throws(function () {
380+
handleDeprecationWorkflow(
381+
config,
382+
'Slightly interesting',
383+
{
384+
id: 'ember.deprecation-workflow',
385+
since: 'the beginning',
386+
until: '3.0.0',
387+
for: 'testing',
388+
},
389+
() => {},
390+
);
391+
}, 'deprecation throws');
392+
});
315393
});

0 commit comments

Comments
 (0)