Skip to content

Commit 0b4cbe7

Browse files
authored
Merge pull request #159 from lolmaus/ember-debug
[BREAKING] Convert to a module. Drops support for Ember < 3.28, requires manual initialization
2 parents 5738444 + 594b2eb commit 0b4cbe7

File tree

14 files changed

+682
-501
lines changed

14 files changed

+682
-501
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,20 @@ jobs:
6262
matrix:
6363
try-scenario:
6464
[
65-
ember-lts-2.12,
66-
ember-lts-2.18,
67-
ember-lts-3.16,
68-
ember-lts-3.20,
69-
ember-lts-3.24,
7065
ember-lts-3.28,
7166
ember-lts-4.4,
7267
ember-lts-4.8,
7368
ember-lts-4.12,
69+
ember-lts-5.4,
7470
ember-release,
7571
ember-beta,
7672
ember-canary,
7773
ember-3.28-with-jquery,
7874
ember-3.28-classic,
7975
]
76+
include:
77+
- ember-try-scenario: ember-canary
78+
allow-failure: true
8079

8180
steps:
8281
- name: Checkout

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ addressing a single deprecation at a time, and prevents backsliding
2323

2424
### Compatibility
2525

26+
3.x
27+
28+
- Ember.js 3.28 until at least 5.4
29+
- Ember CLI 4.12 or above
30+
- Node.js 16 or above
31+
2632
2.x
2733

2834
- Ember.js 2.12 until at least 4.12
@@ -37,13 +43,27 @@ addressing a single deprecation at a time, and prevents backsliding
3743

3844
### Getting started
3945

40-
The initial steps needed to get started:
41-
4246
1. Install the ember-cli-deprecation-workflow addon (`ember install ember-cli-deprecation-workflow`).
43-
2. Run your test suite\* with `ember test --server`.
44-
3. Navigate to your tests (default: http://localhost:7357/)
45-
4. Run `deprecationWorkflow.flushDeprecations()` from your browsers console.
46-
5. Copy the string output into `config/deprecation-workflow.js` in your project.
47+
2. Create an `app/deprecation-workflow.js` file with the following content:
48+
49+
```js
50+
import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
51+
52+
setupDeprecationWorkflow();
53+
```
54+
55+
3. In your `app/app.js`, do:
56+
57+
```js
58+
import './deprecation-workflow';
59+
```
60+
61+
4. Run your test suite\* with `ember test --server`.
62+
5. Navigate to your tests (default: http://localhost:7357/)
63+
6. Run `deprecationWorkflow.flushDeprecations()` in your browsers console.
64+
7. Copy the string output and overwrite the content of `app/deprecation-workflow.js`.
65+
66+
In Chrome, use right click → "Copy string contents" to avoid escape characters.
4767

4868
Once this initial setup is completed the "deprecation spew" should be largely
4969
"fixed". Only unhandled deprecations will be displayed in your console.

addon/index.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { registerDeprecationHandler } from '@ember/debug';
2+
3+
const LOG_LIMIT = 100;
4+
5+
export default function setupDeprecationWorkflow(config) {
6+
self.deprecationWorkflow = self.deprecationWorkflow || {};
7+
self.deprecationWorkflow.deprecationLog = {
8+
messages: {},
9+
};
10+
11+
registerDeprecationHandler((message, options, next) =>
12+
handleDeprecationWorkflow(config, message, options, next),
13+
);
14+
15+
registerDeprecationHandler(deprecationCollector);
16+
17+
self.deprecationWorkflow.flushDeprecations = flushDeprecations;
18+
}
19+
20+
let preamble = `import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
21+
22+
setupDeprecationWorkflow({
23+
workflow: [
24+
`;
25+
26+
let postamble = ` ]
27+
});`;
28+
29+
export function detectWorkflow(config, message, options) {
30+
if (!config || !config.workflow) {
31+
return;
32+
}
33+
34+
let i, workflow, matcher, idMatcher;
35+
for (i = 0; i < config.workflow.length; i++) {
36+
workflow = config.workflow[i];
37+
matcher = workflow.matchMessage;
38+
idMatcher = workflow.matchId;
39+
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)) {
45+
return workflow;
46+
}
47+
}
48+
}
49+
50+
export function flushDeprecations() {
51+
let messages = self.deprecationWorkflow.deprecationLog.messages;
52+
let logs = [];
53+
54+
for (let message in messages) {
55+
logs.push(messages[message]);
56+
}
57+
58+
let deprecations = logs.join(',\n') + '\n';
59+
60+
return preamble + deprecations + postamble;
61+
}
62+
63+
export function handleDeprecationWorkflow(config, message, options, next) {
64+
let matchingWorkflow = detectWorkflow(config, message, options);
65+
if (!matchingWorkflow) {
66+
if (config && config.throwOnUnhandled) {
67+
throw new Error(message);
68+
} else {
69+
next(message, options);
70+
}
71+
} else {
72+
switch (matchingWorkflow.handler) {
73+
case 'silence':
74+
// no-op
75+
break;
76+
case 'log': {
77+
let key = (options && options.id) || message;
78+
79+
if (!self.deprecationWorkflow.logCounts) {
80+
self.deprecationWorkflow.logCounts = {};
81+
}
82+
83+
let count = self.deprecationWorkflow.logCounts[key] || 0;
84+
self.deprecationWorkflow.logCounts[key] = ++count;
85+
86+
if (count <= LOG_LIMIT) {
87+
console.warn('DEPRECATION: ' + message);
88+
if (count === LOG_LIMIT) {
89+
console.warn(
90+
'To avoid console overflow, this deprecation will not be logged any more in this run.',
91+
);
92+
}
93+
}
94+
95+
break;
96+
}
97+
case 'throw':
98+
throw new Error(message);
99+
default:
100+
next(message, options);
101+
break;
102+
}
103+
}
104+
}
105+
106+
export function deprecationCollector(message, options, next) {
107+
let key = (options && options.id) || message;
108+
let matchKey = options && key === options.id ? 'matchId' : 'matchMessage';
109+
110+
self.deprecationWorkflow.deprecationLog.messages[key] =
111+
' { handler: "silence", ' + matchKey + ': ' + JSON.stringify(key) + ' }';
112+
113+
next(message, options);
114+
}

0 commit comments

Comments
 (0)