-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (90 loc) · 2.87 KB
/
index.js
File metadata and controls
102 lines (90 loc) · 2.87 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
var pluginName = 'babel-plugin-react-error-catcher';
var pluginPath = typeof __dirname === 'undefined' ? pluginName : __dirname;
var catcherName = 'errorCatcher';
var catcherPath = pluginPath+'/error-catcher.js';
var reporterName = 'errorReporter';
var reactName = 'React';
var reactPath = 'react';
function isRenderMethod (member) {
return member.kind === 'method' &&
member.key.name === 'render';
}
/**
* Returns a plugin for Babel that catches and reports errors. To display a
* message in the event of an error, pass the path of a reporter module to be
* imported, which can be either of the following:
*
* a) ReactComponent that may use the props below to render a message
* b) function that accepts the arguments below; it may also return a
* ReactComponent or ReactElement to be rendered
*
* Props:
* - error
* - instance
* - filename
* - displayName
*
* @param {String} reporterPath
* @return {Function}
* @api public
*/
module.exports = createErrorCatcher;
function createErrorCatcher (reporterPath) {
return function transform (babel) {
var t = babel.types;
return new babel.Transformer(pluginName, {
/**
* ES6 ReactComponent
*/
ClassDeclaration: function (node, parent, scope, file) {
var hasRenderMethod = node.body.body.filter(isRenderMethod).length > 0;
if (!hasRenderMethod) {
return;
}
var React = file.addImport(reactPath, reactName);
var errorCatcher = file.addImport(catcherPath, catcherName);
var reporter = file.addImport(reporterPath, reporterName);
node.decorators = node.decorators || [];
node.decorators.push(
t.decorator(
t.callExpression(
errorCatcher,
[
React,
t.literal(file.opts._address || file.opts.filename),
t.literal(node.id && node.id.name || ''),
reporter
]
)
)
);
},
/**
* ReactClassComponent
*/
CallExpression: function (node, parent, scope, file) {
var callee = this.get('callee');
if (node._catchErrors || !callee.matchesPattern('React.createClass')) {
return;
}
var React = file.addImport(reactPath, reactName);
var errorCatcher = file.addImport(catcherPath, catcherName);
var reporter = file.addImport(reporterPath, reporterName);
node._catchErrors = true;
return t.callExpression(
t.callExpression(
errorCatcher,
[
React,
t.literal(file.opts._address || file.opts.filename),
t.literal(node.id && node.id.name || ''),
reporter
]
),
[node]
);
}
});
}
}