29
// into this:
30
//
31
// if (!condition) {
32
- // if (__DEV__) {
33
- // throw ReactError(Error(`A ${adj} message that contains ${noun}`));
34
- // } else {
35
- // throw ReactErrorProd(Error(ERR_CODE), adj, noun);
36
- // }
32
+ // throw Error(
33
+ // __DEV__
34
+ // ? `A ${adj} message that contains ${noun}`
35
+ // : formatProdErrorMessage(ERR_CODE, adj, noun)
36
+ // );
37
// }
38
//
39
// where ERR_CODE is an error code: a unique identifier (a number
46
.split('%s')
47
.map(raw => t.templateElement({raw, cooked: String.raw({raw})}));
48
49
- const reactErrorIdentfier = helperModuleImports.addDefault(
50
- path,
51
- 'shared/ReactError',
52
- {
53
- nameHint: 'ReactError',
54
- }
55
- );
56
-
49
// Outputs:
58
- // throw ReactError(Error(`A ${adj} message that contains ${noun}`));
59
- const devThrow = t.throwStatement(
60
- t.callExpression(reactErrorIdentfier, [
61
- t.callExpression(t.identifier('Error'), [
62
- t.templateLiteral(errorMsgQuasis, errorMsgExpressions),
63
- ]),
64
- ])
50
+ // `A ${adj} message that contains ${noun}`;
51
+ const devMessage = t.templateLiteral(
52
+ errorMsgQuasis,
53
+ errorMsgExpressions
54
);
55
56
const parentStatementPath = path.parentPath;
66
//
67
// Outputs:
68
// if (!condition) {
80
- // throw ReactError(Error(`A ${adj} message that contains ${noun}`));
69
+ // throw Error(`A ${adj} message that contains ${noun}`);
70
// }
71
parentStatementPath.replaceWith(
72
t.ifStatement(
73
t.unaryExpression('!', condition),
85
- t.blockStatement([devThrow])
74
+ t.blockStatement([
75
+ t.throwStatement(
76
+ t.callExpression(t.identifier('Error'), [devMessage])
77
+ ),
78
+ ])
79
)
80
);
81
return;
97
// Outputs:
98
// /* FIXME (minify-errors-in-prod): Unminified error message in production build! */
99
// if (!condition) {
107
- // throw ReactError(Error(`A ${adj} message that contains ${noun}`));
100
+ // throw Error(`A ${adj} message that contains ${noun}`);
101
// }
109
- path.replaceWith(
102
+ parentStatementPath.replaceWith(
103
t.ifStatement(
104
t.unaryExpression('!', condition),
112
- t.blockStatement([devThrow])
105
+ t.blockStatement([
106
+ t.throwStatement(
107
+ t.callExpression(t.identifier('Error'), [devMessage])
108
+ ),
109
+ ])
110
)
111
);
115
- path.addComment(
112
+ parentStatementPath.addComment(
113
'leading',
114
'FIXME (minify-errors-in-prod): Unminified error message in production build!'
115
);
118
prodErrorId = parseInt(prodErrorId, 10);
119
120
// Import ReactErrorProd
124
- const reactErrorProdIdentfier = helperModuleImports.addDefault(
121
+ const formatProdErrorMessageIdentifier = helperModuleImports.addDefault(
122
path,
126
- 'shared/ReactErrorProd',
127
- {nameHint: 'ReactErrorProd'}
123
+ 'shared/formatProdErrorMessage',
124
+ {nameHint: 'formatProdErrorMessage'}
125
);
126
127
// Outputs:
131
- // throw ReactErrorProd(Error(ERR_CODE), adj, noun);
132
- const prodThrow = t.throwStatement(
133
- t.callExpression(reactErrorProdIdentfier, [
134
- t.callExpression(t.identifier('Error'), [
135
- t.numericLiteral(prodErrorId),
136
- ]),
137
- ...errorMsgExpressions,
138
- ])
128
+ // formatProdErrorMessage(ERR_CODE, adj, noun);
129
+ const prodMessage = t.callExpression(
130
+ formatProdErrorMessageIdentifier,
131
+ [t.numericLiteral(prodErrorId), ...errorMsgExpressions]
132
);
133
134
// Outputs:
142
- // if (!condition) {
143
- // if (__DEV__) {
144
- // throw ReactError(Error(`A ${adj} message that contains ${noun}`));
145
- // } else {
146
- // throw ReactErrorProd(Error(ERR_CODE), adj, noun);
147
- // }
148
- // }
135
+ // if (!condition) {
136
+ // throw Error(
137
+ // __DEV__
138
+ // ? `A ${adj} message that contains ${noun}`
139
+ // : formatProdErrorMessage(ERR_CODE, adj, noun)
140
+ // );
141
+ // }
142
parentStatementPath.replaceWith(
143
t.ifStatement(
144
t.unaryExpression('!', condition),
145
t.blockStatement([
153
- t.ifStatement(
154
- DEV_EXPRESSION,
155
- t.blockStatement([devThrow]),
156
- t.blockStatement([prodThrow])
157
- ),
146
+ t.blockStatement([
147
+ t.throwStatement(
148
+ t.callExpression(t.identifier('Error'), [
149
+ t.conditionalExpression(
150
+ DEV_EXPRESSION,
151
+ devMessage,
152
+ prodMessage
153
+ ),
154
+ ])
155
+ ),
156
+ ]),
157
])
158
)
159
);