-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdedupeScratchWarnings.js
More file actions
79 lines (67 loc) · 1.92 KB
/
dedupeScratchWarnings.js
File metadata and controls
79 lines (67 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const scratchWarningsKey = "__scratchWarningsDeduped";
const scratchWarningMatchers = {
error: [
[
"Support for defaultProps will be removed",
"React 18 compatibility warnings about function-component defaultProps",
],
[
"React does not recognize the `%s` prop on a DOM element",
"React warnings about scratch-editor props leaking onto DOM nodes",
],
[
"Unknown event handler property `%s`",
"React warnings about unsupported event handler props",
],
[
"findDOMNode is deprecated",
"React warnings about deprecated findDOMNode usage",
],
],
warn: [
[
"componentWillMount has been renamed",
"React warnings about legacy componentWillMount usage",
],
[
"componentWillReceiveProps has been renamed",
"React warnings about legacy componentWillReceiveProps usage",
],
],
};
const dedupeScratchWarnings = () => {
if (
process.env.NODE_ENV !== "development" ||
typeof window !== "object" ||
window[scratchWarningsKey]
) {
return;
}
window[scratchWarningsKey] = true;
const seenSummaries = new Set();
const originalWarn = console.warn.bind(console);
const wrapConsoleMethod = (method) => {
const originalMethod = console[method].bind(console);
return (...args) => {
const message = typeof args[0] === "string" ? args[0] : "";
const summary = scratchWarningMatchers[method]?.find(([needle]) =>
message.includes(needle),
);
if (!summary) {
originalMethod(...args);
return;
}
const [, text] = summary;
if (seenSummaries.has(text)) {
return;
}
seenSummaries.add(text);
originalWarn(
`[scratch-editor] emitted ${text}. Further duplicates suppressed.`,
);
};
};
console.error = wrapConsoleMethod("error");
console.warn = wrapConsoleMethod("warn");
};
export default dedupeScratchWarnings;