Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RegExp for matchId #206

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ setupDeprecationWorkflow({
let postamble = ` ]
});`;

function matchesWorkflow(matcher, value) {
return (
(typeof matcher === 'string' && matcher === value) ||
(matcher instanceof RegExp && matcher.exec(value))
);
}

export function detectWorkflow(config, message, options) {
if (!config || !config.workflow) {
return;
Expand All @@ -37,11 +44,10 @@ export function detectWorkflow(config, message, options) {
matcher = workflow.matchMessage;
idMatcher = workflow.matchId;

if (typeof idMatcher === 'string' && options && idMatcher === options.id) {
return workflow;
} else if (typeof matcher === 'string' && matcher === message) {
return workflow;
} else if (matcher instanceof RegExp && matcher.exec(message)) {
if (
matchesWorkflow(idMatcher, options?.id) ||
matchesWorkflow(matcher, message)
) {
return workflow;
}
}
Expand Down
78 changes: 78 additions & 0 deletions tests/unit/handle-deprecation-workflow-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ module('handleDeprecationWorkflow', function (hooks) {

test('deprecation silenced with string matcher', function (assert) {
const config = {
throwOnUnhandled: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are unrelated to the actual PR, but I think these prevent false positives. The "silenced" test cases in particular don't assert anything specific (assert.ok(true) in L100), only make sure that nothing throws. But without the throwOnUnhandled flag that wouldn't happen anyways, with or without the explicit workflow rule.

workflow: [{ matchMessage: 'Interesting', handler: 'silence' }],
};

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

const config = {
throwOnUnhandled: true,
workflow: [{ matchMessage: message, handler: 'log' }],
};

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

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

Expand Down Expand Up @@ -184,6 +187,7 @@ module('handleDeprecationWorkflow', function (hooks) {
};

const config = {
throwOnUnhandled: true,
workflow: [{ matchMessage: /Inter/, handler: 'log' }],
};

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

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

Expand Down Expand Up @@ -278,6 +283,7 @@ module('handleDeprecationWorkflow', function (hooks) {
};

const config = {
throwOnUnhandled: true,
workflow: [{ matchId: 'ember.deprecation-workflow', handler: 'log' }],
};

Expand Down Expand Up @@ -312,4 +318,76 @@ module('handleDeprecationWorkflow', function (hooks) {
);
}, 'deprecation throws');
});

test('deprecation silenced with id regex', function (assert) {
const config = {
throwOnUnhandled: true,
workflow: [{ matchId: /^ember\..*/, handler: 'silence' }],
};

handleDeprecationWorkflow(
config,
'Slightly interesting',
{
id: 'ember.deprecation-workflow',
since: 'the beginning',
until: '3.0.0',
for: 'testing',
},
() => {},
);

assert.ok(true, 'Deprecation did not raise');
});

// eslint-disable-next-line qunit/require-expect
test('deprecation logs with id regex', function (assert) {
assert.expect(1);

let message = 'Slightly interesting';

console.warn = function (passedMessage) {
assert.strictEqual(
passedMessage,
'DEPRECATION: ' + message,
'deprecation logs',
);
};

const config = {
throwOnUnhandled: true,
workflow: [{ matchId: /^ember\..*/, handler: 'log' }],
};

handleDeprecationWorkflow(
config,
'Slightly interesting',
{
id: 'ember.deprecation-workflow',
since: 'the beginning',
until: '3.0.0',
for: 'testing',
},
() => {},
);
});

test('deprecation thrown with id regex', function (assert) {
const config = {
workflow: [{ matchId: /^ember\..*/, handler: 'throw' }],
};
assert.throws(function () {
handleDeprecationWorkflow(
config,
'Slightly interesting',
{
id: 'ember.deprecation-workflow',
since: 'the beginning',
until: '3.0.0',
for: 'testing',
},
() => {},
);
}, 'deprecation throws');
});
});