@@ -62,9 +62,39 @@ 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 statementParent = path . getStatementParent ( ) ;
76
+ const leadingComments = statementParent . node . leadingComments ;
77
+ if ( leadingComments !== undefined ) {
78
+ for ( let i = 0 ; i < leadingComments . length ; i ++ ) {
79
+ // TODO: Since this only detects one of many ways to disable a lint
80
+ // rule, we should instead search for a custom directive (like
81
+ // no-minify-errors) instead of ESLint. Will need to update our lint
82
+ // rule to recognize the same directive.
83
+ const commentText = leadingComments [ i ] . value ;
84
+ if (
85
+ commentText . includes (
86
+ 'eslint-disable-next-line react-internal/prod-error-codes'
87
+ )
88
+ ) {
89
+ return ;
90
+ }
91
+ }
92
+ }
93
+
94
+ statementParent . addComment (
95
+ 'leading' ,
96
+ 'FIXME (minify-errors-in-prod): Unminified error message in production build!'
97
+ ) ;
68
98
return ;
69
99
}
70
100
prodErrorId = parseInt ( prodErrorId , 10 ) ;
@@ -89,12 +119,11 @@ module.exports = function(babel) {
89
119
// ? `A ${adj} message that contains ${noun}`
90
120
// : formatProdErrorMessage(ERR_CODE, adj, noun)
91
121
// );
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
- ) ;
122
+ const newErrorCall = t . callExpression ( t . identifier ( 'Error' ) , [
123
+ t . conditionalExpression ( DEV_EXPRESSION , errorMsgNode , prodMessage ) ,
124
+ ] ) ;
125
+ newErrorCall [ SEEN_SYMBOL ] = true ;
126
+ path . replaceWith ( newErrorCall ) ;
98
127
}
99
128
100
129
return {
@@ -162,16 +191,17 @@ module.exports = function(babel) {
162
191
// if (!condition) {
163
192
// throw Error(`A ${adj} message that contains ${noun}`);
164
193
// }
194
+ const errorCallNode = t . callExpression ( t . identifier ( 'Error' ) , [
195
+ devMessage ,
196
+ ] ) ;
197
+ errorCallNode [ SEEN_SYMBOL ] = true ;
165
198
parentStatementPath . replaceWith (
166
199
t . ifStatement (
167
200
t . unaryExpression ( '!' , condition ) ,
168
- t . blockStatement ( [
169
- t . throwStatement (
170
- t . callExpression ( t . identifier ( 'Error' ) , [ devMessage ] )
171
- ) ,
172
- ] )
201
+ t . blockStatement ( [ t . throwStatement ( errorCallNode ) ] )
173
202
)
174
203
) ;
204
+
175
205
return ;
176
206
}
177
207
@@ -187,14 +217,14 @@ module.exports = function(babel) {
187
217
// if (!condition) {
188
218
// throw Error(`A ${adj} message that contains ${noun}`);
189
219
// }
220
+ const errorCall = t . callExpression ( t . identifier ( 'Error' ) , [
221
+ devMessage ,
222
+ ] ) ;
223
+ errorCall [ SEEN_SYMBOL ] = true ;
190
224
parentStatementPath . replaceWith (
191
225
t . ifStatement (
192
226
t . unaryExpression ( '!' , condition ) ,
193
- t . blockStatement ( [
194
- t . throwStatement (
195
- t . callExpression ( t . identifier ( 'Error' ) , [ devMessage ] )
196
- ) ,
197
- ] )
227
+ t . blockStatement ( [ t . throwStatement ( errorCall ) ] )
198
228
)
199
229
) ;
200
230
parentStatementPath . addComment (
@@ -219,6 +249,11 @@ module.exports = function(babel) {
219
249
[ t . numericLiteral ( prodErrorId ) , ...errorMsgExpressions ]
220
250
) ;
221
251
252
+ const errorCall = t . callExpression ( t . identifier ( 'Error' ) , [
253
+ t . conditionalExpression ( DEV_EXPRESSION , devMessage , prodMessage ) ,
254
+ ] ) ;
255
+ errorCall [ SEEN_SYMBOL ] = true ;
256
+
222
257
// Outputs:
223
258
// if (!condition) {
224
259
// throw Error(
@@ -231,17 +266,7 @@ module.exports = function(babel) {
231
266
t . ifStatement (
232
267
t . unaryExpression ( '!' , condition ) ,
233
268
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
- ] ) ,
269
+ t . blockStatement ( [ t . throwStatement ( errorCall ) ] ) ,
245
270
] )
246
271
)
247
272
) ;
0 commit comments