@@ -62,9 +62,38 @@ module.exports = function(babel) {
62
62
63
63
let prodErrorId = errorMap [ errorMsgLiteral ] ;
64
64
if ( prodErrorId === undefined ) {
65
- // There is no error code for this message. We use a lint rule to
66
- // enforce that messages can be minified, so assume this is
67
- // intentional and exit gracefully.
65
+ // There is no error code for this message. Add an inline comment
66
+ // that flags this as an unminified error. This allows the build
67
+ // to proceed, while also allowing a post-build linter to detect it.
68
+ //
69
+ // Outputs:
70
+ // /* FIXME (minify-errors-in-prod): Unminified error message in production build! */
71
+ // if (!condition) {
72
+ // throw Error(`A ${adj} message that contains ${noun}`);
73
+ // }
74
+
75
+ const leadingComments = path . parentPath . node . leadingComments ;
76
+ if ( leadingComments !== undefined ) {
77
+ for ( let i = 0 ; i < leadingComments . length ; i ++ ) {
78
+ // TODO: Since this only detects one of many ways to disable a lint
79
+ // rule, we should instead search for a custom directive (like
80
+ // no-minify-errors) instead of ESLint. Will need to update our lint
81
+ // rule to recognize the same directive.
82
+ const commentText = leadingComments [ i ] . value ;
83
+ if (
84
+ commentText . includes (
85
+ 'eslint-disable-next-line react-internal/prod-error-codes'
86
+ )
87
+ ) {
88
+ return ;
89
+ }
90
+ }
91
+ }
92
+
93
+ path . addComment (
94
+ 'leading' ,
95
+ 'FIXME (minify-errors-in-prod): Unminified error message in production build!'
96
+ ) ;
68
97
return ;
69
98
}
70
99
prodErrorId = parseInt ( prodErrorId , 10 ) ;
@@ -89,12 +118,11 @@ module.exports = function(babel) {
89
118
// ? `A ${adj} message that contains ${noun}`
90
119
// : formatProdErrorMessage(ERR_CODE, adj, noun)
91
120
// );
92
- path . replaceWith ( t . callExpression ( t . identifier ( 'Error' ) , [ prodMessage ] ) ) ;
93
- path . replaceWith (
94
- t . callExpression ( t . identifier ( 'Error' ) , [
95
- t . conditionalExpression ( DEV_EXPRESSION , errorMsgNode , prodMessage ) ,
96
- ] )
97
- ) ;
121
+ const newErrorCall = t . callExpression ( t . identifier ( 'Error' ) , [
122
+ t . conditionalExpression ( DEV_EXPRESSION , errorMsgNode , prodMessage ) ,
123
+ ] ) ;
124
+ newErrorCall [ SEEN_SYMBOL ] = true ;
125
+ path . replaceWith ( newErrorCall ) ;
98
126
}
99
127
100
128
return {
@@ -162,16 +190,17 @@ module.exports = function(babel) {
162
190
// if (!condition) {
163
191
// throw Error(`A ${adj} message that contains ${noun}`);
164
192
// }
193
+ const errorCallNode = t . callExpression ( t . identifier ( 'Error' ) , [
194
+ devMessage ,
195
+ ] ) ;
196
+ errorCallNode [ SEEN_SYMBOL ] = true ;
165
197
parentStatementPath . replaceWith (
166
198
t . ifStatement (
167
199
t . unaryExpression ( '!' , condition ) ,
168
- t . blockStatement ( [
169
- t . throwStatement (
170
- t . callExpression ( t . identifier ( 'Error' ) , [ devMessage ] )
171
- ) ,
172
- ] )
200
+ t . blockStatement ( [ t . throwStatement ( errorCallNode ) ] )
173
201
)
174
202
) ;
203
+
175
204
return ;
176
205
}
177
206
@@ -187,14 +216,14 @@ module.exports = function(babel) {
187
216
// if (!condition) {
188
217
// throw Error(`A ${adj} message that contains ${noun}`);
189
218
// }
219
+ const errorCall = t . callExpression ( t . identifier ( 'Error' ) , [
220
+ devMessage ,
221
+ ] ) ;
222
+ errorCall [ SEEN_SYMBOL ] = true ;
190
223
parentStatementPath . replaceWith (
191
224
t . ifStatement (
192
225
t . unaryExpression ( '!' , condition ) ,
193
- t . blockStatement ( [
194
- t . throwStatement (
195
- t . callExpression ( t . identifier ( 'Error' ) , [ devMessage ] )
196
- ) ,
197
- ] )
226
+ t . blockStatement ( [ t . throwStatement ( errorCall ) ] )
198
227
)
199
228
) ;
200
229
parentStatementPath . addComment (
@@ -219,6 +248,11 @@ module.exports = function(babel) {
219
248
[ t . numericLiteral ( prodErrorId ) , ...errorMsgExpressions ]
220
249
) ;
221
250
251
+ const errorCall = t . callExpression ( t . identifier ( 'Error' ) , [
252
+ t . conditionalExpression ( DEV_EXPRESSION , devMessage , prodMessage ) ,
253
+ ] ) ;
254
+ errorCall [ SEEN_SYMBOL ] = true ;
255
+
222
256
// Outputs:
223
257
// if (!condition) {
224
258
// throw Error(
@@ -231,17 +265,7 @@ module.exports = function(babel) {
231
265
t . ifStatement (
232
266
t . unaryExpression ( '!' , condition ) ,
233
267
t . blockStatement ( [
234
- t . blockStatement ( [
235
- t . throwStatement (
236
- t . callExpression ( t . identifier ( 'Error' ) , [
237
- t . conditionalExpression (
238
- DEV_EXPRESSION ,
239
- devMessage ,
240
- prodMessage
241
- ) ,
242
- ] )
243
- ) ,
244
- ] ) ,
268
+ t . blockStatement ( [ t . throwStatement ( errorCall ) ] ) ,
245
269
] )
246
270
)
247
271
) ;
0 commit comments