Skip to content

Commit 9187e13

Browse files
committed
feat: flushDeprecations merges detected deprecations with existing config
Currently calling flushDeprecations() is destructive in that it ignores any existing config. Users would need to merge the detected deprecations with their existing config manually. Which is error prone, and does not scale for very large monorepos. This change is doing the merge automatically. Only minor drawback is that we need to serialize the merged config with `JSON.serialize`, so it is getting returned as e.g. `{ "handler": "silence", "matchId": "test" }` instead of `{ handler: 'silence', matchId: 'test' }`. But when using prettier, this will get auto-fixed anyway, so I think the trade-offs are positive here.
1 parent b4943a7 commit 9187e13

File tree

4 files changed

+124
-42
lines changed

4 files changed

+124
-42
lines changed

addon/index.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ export default function setupDeprecationWorkflow(config) {
1414

1515
registerDeprecationHandler(deprecationCollector);
1616

17-
self.deprecationWorkflow.flushDeprecations = flushDeprecations;
17+
self.deprecationWorkflow.flushDeprecations = (options) =>
18+
flushDeprecations({ config, ...options });
1819
}
1920

20-
let preamble = `import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
21-
22-
setupDeprecationWorkflow({
23-
workflow: [
24-
`;
25-
26-
let postamble = ` ]
27-
});`;
28-
2921
function matchesWorkflow(matcher, value) {
3022
return (
3123
(typeof matcher === 'string' && matcher === value) ||
@@ -53,21 +45,25 @@ export function detectWorkflow(config, message, options) {
5345
}
5446
}
5547

56-
export function flushDeprecations({ handler = 'silence' } = {}) {
48+
export function flushDeprecations({ handler = 'silence', config = {} } = {}) {
5749
let messages = self.deprecationWorkflow.deprecationLog.messages;
58-
let logs = [];
59-
60-
for (let id of messages.values()) {
61-
logs.push(
62-
` { handler: ${JSON.stringify(handler)}, matchId: ${JSON.stringify(
63-
id,
64-
)} }`,
65-
);
66-
}
50+
let existing = config.workflow ?? [];
51+
let collected = messages
52+
.values()
53+
.filter((matchId) => !existing.some((entry) => entry.matchId === matchId))
54+
.map((matchId) => ({
55+
handler,
56+
matchId,
57+
}));
58+
59+
let mergedConfig = {
60+
...config,
61+
workflow: [...existing, ...collected],
62+
};
6763

68-
let deprecations = logs.join(',\n') + '\n';
64+
return `import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
6965
70-
return preamble + deprecations + postamble;
66+
setupDeprecationWorkflow(${JSON.stringify(mergedConfig, undefined, 2)});`;
7167
}
7268

7369
export function handleDeprecationWorkflow(config, message, options, next) {

tests/acceptance/flush-deprecations-test.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { deprecate } from '@ember/debug';
22
import { module } from 'qunit';
33
import test from '../helpers/debug-test';
4+
import { config } from 'dummy/deprecation-workflow';
45

56
let originalWarn;
67

@@ -56,12 +57,19 @@ module('flushDeprecations', function (hooks) {
5657
deprecationsPayload,
5758
`import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
5859
59-
setupDeprecationWorkflow({
60-
workflow: [
61-
{ handler: "silence", matchId: "test" },
62-
{ handler: "silence", matchId: "log-strict" }
63-
]
64-
});`,
60+
setupDeprecationWorkflow(${JSON.stringify(
61+
{
62+
...config,
63+
workflow: [
64+
...config.workflow,
65+
{ handler: 'silence', matchId: 'test' },
66+
// this one is already present in the existing config, so don't expect another entry, as we deduplicate
67+
// { handler: 'silence', matchId: 'log-strict' },
68+
],
69+
},
70+
undefined,
71+
2,
72+
)});`,
6573
);
6674
});
6775
});

tests/dummy/app/deprecation-workflow.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
22

3-
setupDeprecationWorkflow({
3+
// We export this here to be able to import from our own tests
4+
export const config = {
45
throwOnUnhandled: true,
56
workflow: [
67
/*
@@ -26,4 +27,6 @@ setupDeprecationWorkflow({
2627

2728
{ matchMessage: 'throw-strict', handler: 'throw' },
2829
],
29-
});
30+
};
31+
32+
setupDeprecationWorkflow(config);

tests/unit/flush-deprecations-test.js

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ module('flushDeprecations', function (hooks) {
3434
]);
3535

3636
let deprecationsPayload = flushDeprecations();
37+
let expectedConfig = {
38+
workflow: [
39+
{ handler: 'silence', matchId: 'first' },
40+
{ handler: 'silence', matchId: 'second' },
41+
],
42+
};
43+
3744
assert.strictEqual(
3845
deprecationsPayload,
3946
`import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
4047
41-
setupDeprecationWorkflow({
42-
workflow: [
43-
{ handler: "silence", matchId: "first" },
44-
{ handler: "silence", matchId: "second" }
45-
]
46-
});`,
48+
setupDeprecationWorkflow(${JSON.stringify(expectedConfig, undefined, 2)});`,
4749
);
4850
});
4951

@@ -54,16 +56,89 @@ setupDeprecationWorkflow({
5456
]);
5557

5658
let deprecationsPayload = flushDeprecations({ handler: 'log' });
59+
let expectedConfig = {
60+
workflow: [
61+
{ handler: 'log', matchId: 'first' },
62+
{ handler: 'log', matchId: 'second' },
63+
],
64+
};
65+
66+
assert.strictEqual(
67+
deprecationsPayload,
68+
`import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
69+
70+
setupDeprecationWorkflow(${JSON.stringify(expectedConfig, undefined, 2)});`,
71+
);
72+
});
73+
74+
test('calling flushDeprecations with existing config and no deprecations returns original config', function (assert) {
75+
let config = {
76+
throwOnUnhandled: true,
77+
workflow: [{ handler: 'log', matchId: 'existing' }],
78+
};
79+
self.deprecationWorkflow.deprecationLog.messages = new Set([]);
80+
81+
let deprecationsPayload = flushDeprecations({ config });
82+
assert.strictEqual(
83+
deprecationsPayload,
84+
`import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
85+
86+
setupDeprecationWorkflow(${JSON.stringify(config, undefined, 2)});`,
87+
);
88+
});
89+
90+
test('calling flushDeprecations with existing config returns augmented config', function (assert) {
91+
let config = {
92+
throwOnUnhandled: true,
93+
workflow: [{ handler: 'log', matchId: 'existing' }],
94+
};
95+
self.deprecationWorkflow.deprecationLog.messages = new Set([
96+
'first',
97+
'second',
98+
]);
99+
100+
let deprecationsPayload = flushDeprecations({ config });
101+
let expectedConfig = {
102+
throwOnUnhandled: true,
103+
workflow: [
104+
{ handler: 'log', matchId: 'existing' },
105+
{ handler: 'silence', matchId: 'first' },
106+
{ handler: 'silence', matchId: 'second' },
107+
],
108+
};
109+
assert.strictEqual(
110+
deprecationsPayload,
111+
`import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
112+
113+
setupDeprecationWorkflow(${JSON.stringify(expectedConfig, undefined, 2)});`,
114+
);
115+
});
116+
117+
test('calling flushDeprecations with existing config does not override existing deprecations', function (assert) {
118+
let config = {
119+
throwOnUnhandled: true,
120+
workflow: [{ handler: 'log', matchId: 'existing' }],
121+
};
122+
self.deprecationWorkflow.deprecationLog.messages = new Set([
123+
'first',
124+
'second',
125+
'existing',
126+
]);
127+
128+
let deprecationsPayload = flushDeprecations({ config });
129+
let expectedConfig = {
130+
throwOnUnhandled: true,
131+
workflow: [
132+
{ handler: 'log', matchId: 'existing' },
133+
{ handler: 'silence', matchId: 'first' },
134+
{ handler: 'silence', matchId: 'second' },
135+
],
136+
};
57137
assert.strictEqual(
58138
deprecationsPayload,
59139
`import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
60140
61-
setupDeprecationWorkflow({
62-
workflow: [
63-
{ handler: "log", matchId: "first" },
64-
{ handler: "log", matchId: "second" }
65-
]
66-
});`,
141+
setupDeprecationWorkflow(${JSON.stringify(expectedConfig, undefined, 2)});`,
67142
);
68143
});
69144
});

0 commit comments

Comments
 (0)