@@ -102,6 +102,68 @@ function handleUnminifyableError(missingError, path) {
102
102
}
103
103
}
104
104
105
+ /**
106
+ * @param {babel.types } t
107
+ * @param {babel.NodePath<babel.types.NewExpression> } newExpressionPath
108
+ * @param {{ detection: Options['detection']; missingError: MissingError} } param2
109
+ * @returns {null | { messageNode: babel.types.Expression; messagePath: babel.NodePath<babel.types.ArgumentPlaceholder | babel.types.SpreadElement | babel.types.Expression>; message: { message: string; expressions: babel.types.Expression[] } } }
110
+ */
111
+ function findMessageNode ( t , newExpressionPath , { detection, missingError } ) {
112
+ if ( ! newExpressionPath . get ( 'callee' ) . isIdentifier ( { name : 'Error' } ) ) {
113
+ return null ;
114
+ }
115
+
116
+ switch ( detection ) {
117
+ case 'opt-in' : {
118
+ if (
119
+ ! newExpressionPath . node . leadingComments ?. some ( ( comment ) =>
120
+ comment . value . includes ( COMMENT_OPT_IN_MARKER ) ,
121
+ )
122
+ ) {
123
+ return null ;
124
+ }
125
+ newExpressionPath . node . leadingComments = newExpressionPath . node . leadingComments . filter (
126
+ ( comment ) => ! comment . value . includes ( COMMENT_OPT_IN_MARKER ) ,
127
+ ) ;
128
+ break ;
129
+ }
130
+ case 'opt-out' : {
131
+ if (
132
+ newExpressionPath . node . leadingComments ?. some ( ( comment ) =>
133
+ comment . value . includes ( COMMENT_OPT_OUT_MARKER ) ,
134
+ )
135
+ ) {
136
+ newExpressionPath . node . leadingComments = newExpressionPath . node . leadingComments . filter (
137
+ ( comment ) => ! comment . value . includes ( COMMENT_OPT_OUT_MARKER ) ,
138
+ ) ;
139
+ return null ;
140
+ }
141
+
142
+ break ;
143
+ }
144
+ default : {
145
+ throw new Error ( `Unknown detection option: ${ detection } ` ) ;
146
+ }
147
+ }
148
+
149
+ const messagePath = newExpressionPath . get ( 'arguments' ) [ 0 ] ;
150
+ if ( ! messagePath ) {
151
+ return null ;
152
+ }
153
+
154
+ const messageNode = messagePath . node ;
155
+ if ( t . isSpreadElement ( messageNode ) || t . isArgumentPlaceholder ( messageNode ) ) {
156
+ handleUnminifyableError ( missingError , newExpressionPath ) ;
157
+ return null ;
158
+ }
159
+ const message = extractMessage ( t , messageNode ) ;
160
+ if ( ! message ) {
161
+ handleUnminifyableError ( missingError , newExpressionPath ) ;
162
+ return null ;
163
+ }
164
+ return { messagePath, messageNode, message } ;
165
+ }
166
+
105
167
/**
106
168
* Transforms the error message node.
107
169
* @param {babel.types } t
@@ -261,59 +323,15 @@ module.exports = function plugin(
261
323
name : '@mui/internal-babel-plugin-minify-errors' ,
262
324
visitor : {
263
325
NewExpression ( newExpressionPath , state ) {
264
- if ( ! newExpressionPath . get ( 'callee' ) . isIdentifier ( { name : 'Error' } ) ) {
265
- return ;
266
- }
267
-
268
- switch ( detection ) {
269
- case 'opt-in' : {
270
- if (
271
- ! newExpressionPath . node . leadingComments ?. some ( ( comment ) =>
272
- comment . value . includes ( COMMENT_OPT_IN_MARKER ) ,
273
- )
274
- ) {
275
- return ;
276
- }
277
- newExpressionPath . node . leadingComments = newExpressionPath . node . leadingComments . filter (
278
- ( comment ) => ! comment . value . includes ( COMMENT_OPT_IN_MARKER ) ,
279
- ) ;
280
- break ;
281
- }
282
- case 'opt-out' : {
283
- if (
284
- newExpressionPath . node . leadingComments ?. some ( ( comment ) =>
285
- comment . value . includes ( COMMENT_OPT_OUT_MARKER ) ,
286
- )
287
- ) {
288
- newExpressionPath . node . leadingComments =
289
- newExpressionPath . node . leadingComments . filter (
290
- ( comment ) => ! comment . value . includes ( COMMENT_OPT_OUT_MARKER ) ,
291
- ) ;
292
- return ;
293
- }
294
-
295
- break ;
296
- }
297
- default : {
298
- throw new Error ( `Unknown detection option: ${ detection } ` ) ;
299
- }
300
- }
301
-
302
- const messagePath = newExpressionPath . get ( 'arguments' ) [ 0 ] ;
303
- if ( ! messagePath ) {
304
- return ;
305
- }
306
-
307
- const messageNode = messagePath . node ;
308
- if ( t . isSpreadElement ( messageNode ) || t . isArgumentPlaceholder ( messageNode ) ) {
309
- handleUnminifyableError ( missingError , newExpressionPath ) ;
326
+ const message = findMessageNode ( t , newExpressionPath , { detection, missingError } ) ;
327
+ if ( ! message ) {
310
328
return ;
311
329
}
312
330
313
331
const transformedMessage = transformMessage (
314
332
t ,
315
333
newExpressionPath ,
316
- messageNode ,
334
+ message . messageNode ,
317
335
state ,
318
336
errorCodesLookup ,
319
337
missingError ,
@@ -322,7 +340,7 @@ module.exports = function plugin(
322
340
) ;
323
341
324
342
if ( transformedMessage ) {
325
- messagePath . replaceWith ( transformedMessage ) ;
343
+ message . messagePath . replaceWith ( transformedMessage ) ;
326
344
}
327
345
} ,
328
346
} ,
@@ -336,3 +354,7 @@ module.exports = function plugin(
336
354
} ,
337
355
} ;
338
356
} ;
357
+
358
+ module . exports . findMessageNode = findMessageNode ;
359
+
360
+ exports . findMessageNode = findMessageNode ;
0 commit comments