forked from kossnocorp/react-guard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (106 loc) · 3.34 KB
/
index.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* eslint-disable no-redeclare */
// XXX: This is highly optimized implementation, used in production
// see naive.js for the logic reference.
//
// See V8 Bailout Reasons for optimization reference:
// https://github.com/vhf/v8-bailout-reasons
var reactGuard = function (React, guardFn) {
guardFn =
guardFn ||
function () {
return null
}
function classComponentRender () {
try {
return this.__guardedRender__()
} catch (err) {
return guardFn(err, {
props: this.props,
state: this.state,
displayName: this.constructor.displayName || this.constructor.name
})
}
}
function buildFunctionComponent (type) {
var _type = function (props, publicContext, updateQueue) {
try {
return type(props, publicContext, updateQueue)
} catch (err) {
return guardFn(err, { props: props, displayName: type.displayName || type.name })
}
}
Object.assign(_type, type, { displayName: type.displayName || type.name })
return _type
}
React.__reactGuardOriginalCreateElement__ = React.createElement
React.createElement = function (type, createElementProps) {
// DOM component
if (typeof type === 'string') {
if (arguments.length === 2) {
return React.__reactGuardOriginalCreateElement__(
type,
createElementProps
)
} else {
var args = new Array(arguments.length)
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i]
}
return React.__reactGuardOriginalCreateElement__.apply(React, args)
}
// Class component
} else if (
typeof type === 'function' &&
type.prototype &&
'render' in type.prototype &&
!('__guardedRender__' in type.prototype)
) {
type.prototype.__guardedRender__ = type.prototype.render
type.prototype.render = classComponentRender
var args = new Array(arguments.length)
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i]
}
return React.__reactGuardOriginalCreateElement__.apply(React, args)
// Function component
} else if (
typeof type === 'function' &&
(!type.prototype || !('render' in type.prototype))
) {
var _type
if (type.__reactGuardGuardedFunction__) {
_type = type.__reactGuardGuardedFunction__
} else {
_type = buildFunctionComponent(type)
type.__reactGuardGuardedFunction__ = _type
}
var args = new Array(arguments.length)
args[0] = _type
for (var i = 1; i < args.length; ++i) {
args[i] = arguments[i]
}
return React.__reactGuardOriginalCreateElement__.apply(React, args)
// "Warm" class component
} else {
if (arguments.length === 2) {
return React.__reactGuardOriginalCreateElement__(
type,
createElementProps
)
} else {
var args = new Array(arguments.length)
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i]
}
return React.__reactGuardOriginalCreateElement__.apply(React, args)
}
}
}
}
reactGuard.restore = function (React) {
if ('__reactGuardOriginalCreateElement__' in React) {
React.createElement = React.__reactGuardOriginalCreateElement__
delete React.__reactGuardOriginalCreateElement__
}
}
module.exports = reactGuard