@@ -53,10 +53,10 @@ function fmt(strings: TemplateStringsArray, ...keys: unknown[]) {
53
53
. replace ( / .u s e s t r i c t .; \n / g, '' )
54
54
. replace ( / v a r _ [ ^ ; ] + ; \n / g, '' )
55
55
. replace ( / f u n c t i o n _ i n t e r o p R e q u i r e D e f a u l t \( [ ^ ) ] * \) { [ ^ ; ] + ?; [ \s \n ] * } \n / g, '' )
56
- . replace ( / _ f o r m a t ( D e v | P r o d ) E r r o r M e s s a g e \d + / g, 'format$1ErrorMessage ' )
56
+ . replace ( / _ f o r m a t ( D e v | P r o d ) ( E r r o r | W a r n i n g ) M e s s a g e \d + / g, 'format$1$2Message ' )
57
57
. replace (
58
- / \( 0 , \s * f o r m a t ( D e v | P r o d ) E r r o r M e s s a g e \. d e f a u l t \) / g,
59
- 'format$1ErrorMessage ' ,
58
+ / \( 0 , \s * f o r m a t ( D e v | P r o d ) ( E r r o r | W a r n i n g ) M e s s a g e \. d e f a u l t \) / g,
59
+ 'format$1$2Message ' ,
60
60
)
61
61
. trim ( ) ;
62
62
return prettier . format ( before , {
@@ -98,77 +98,155 @@ async function expectTransform(opts) {
98
98
}
99
99
100
100
describe ( 'transform-error-messages' , ( ) => {
101
- describe ( '{extractCodes: true, noMinify: false}' , ( ) => {
102
- const opts = { extractCodes : true , noMinify : false } ;
103
- it ( 'inserts known and extracts unknown message codes' , async ( ) => {
104
- await expectTransform ( {
105
- codeBefore : `
101
+ describe ( 'invariant' , ( ) => {
102
+ describe ( '{extractCodes: true, noMinify: false}' , ( ) => {
103
+ const opts = { extractCodes : true , noMinify : false } ;
104
+ it ( 'inserts known and extracts unknown message codes' , async ( ) => {
105
+ await expectTransform ( {
106
+ codeBefore : `
106
107
invariant(condition, ${ JSON . stringify ( NEW_MSG ) } );
107
108
invariant(condition, ${ JSON . stringify ( KNOWN_MSG ) } , adj, noun);
108
109
` ,
109
- codeExpect : `
110
+ codeExpect : `
110
111
if (!condition) {
111
112
formatProdErrorMessage(1);
112
113
}
113
114
if (!condition) {
114
115
formatProdErrorMessage(0, adj, noun);
115
116
}` ,
116
- messageMapBefore : KNOWN_MSG_MAP ,
117
- messageMapExpect : NEW_MSG_MAP ,
118
- opts,
117
+ messageMapBefore : KNOWN_MSG_MAP ,
118
+ messageMapExpect : NEW_MSG_MAP ,
119
+ opts,
120
+ } ) ;
119
121
} ) ;
120
122
} ) ;
121
- } ) ;
122
- describe ( '{extractCodes: true, noMinify: true}' , ( ) => {
123
- const opts = { extractCodes : true , noMinify : true } ;
124
- it ( 'inserts known and extracts unknown message codes' , async ( ) => {
125
- await expectTransform ( {
126
- codeBefore : `
123
+ describe ( '{extractCodes: true, noMinify: true}' , ( ) => {
124
+ const opts = { extractCodes : true , noMinify : true } ;
125
+ it ( 'inserts known and extracts unknown message codes' , async ( ) => {
126
+ await expectTransform ( {
127
+ codeBefore : `
127
128
invariant(condition, ${ JSON . stringify ( NEW_MSG ) } );
128
129
invariant(condition, ${ JSON . stringify ( KNOWN_MSG ) } , adj, noun);
129
130
` ,
130
- codeExpect : `
131
+ codeExpect : `
131
132
if (!condition) {
132
133
formatDevErrorMessage(\`A new invariant\`);
133
134
}
134
135
if (!condition) {
135
136
formatDevErrorMessage(\`A \${adj} message that contains \${noun}\`);
136
137
}` ,
137
- messageMapBefore : KNOWN_MSG_MAP ,
138
- messageMapExpect : NEW_MSG_MAP ,
139
- opts,
138
+ messageMapBefore : KNOWN_MSG_MAP ,
139
+ messageMapExpect : NEW_MSG_MAP ,
140
+ opts,
141
+ } ) ;
140
142
} ) ;
141
143
} ) ;
142
- } ) ;
143
- describe ( '{extractCodes: false, noMinify: false}' , ( ) => {
144
- const opts = { extractCodes : false , noMinify : false } ;
145
- it ( 'inserts known message' , async ( ) => {
146
- await expectTransform ( {
147
- codeBefore : `invariant(condition, ${ JSON . stringify (
148
- KNOWN_MSG ,
149
- ) } , adj, noun)`,
150
- codeExpect : `
144
+ describe ( '{extractCodes: false, noMinify: false}' , ( ) => {
145
+ const opts = { extractCodes : false , noMinify : false } ;
146
+ it ( 'inserts known message' , async ( ) => {
147
+ await expectTransform ( {
148
+ codeBefore : `invariant(condition, ${ JSON . stringify (
149
+ KNOWN_MSG ,
150
+ ) } , adj, noun)`,
151
+ codeExpect : `
151
152
if (!condition) {
152
153
formatProdErrorMessage(0, adj, noun);
153
154
}
154
155
` ,
155
- messageMapBefore : KNOWN_MSG_MAP ,
156
- messageMapExpect : KNOWN_MSG_MAP ,
157
- opts,
156
+ messageMapBefore : KNOWN_MSG_MAP ,
157
+ messageMapExpect : KNOWN_MSG_MAP ,
158
+ opts,
159
+ } ) ;
160
+ } ) ;
161
+ it ( 'inserts warning comment for unknown messages' , async ( ) => {
162
+ await expectTransform ( {
163
+ codeBefore : `invariant(condition, ${ JSON . stringify ( NEW_MSG ) } )` ,
164
+ codeExpect : `
165
+ /*FIXME (minify-errors-in-prod): Unminified error message in production build!*/
166
+ if (!condition) {
167
+ formatDevErrorMessage(\`A new invariant\`);
168
+ }
169
+ ` ,
170
+ messageMapBefore : KNOWN_MSG_MAP ,
171
+ messageMapExpect : KNOWN_MSG_MAP ,
172
+ opts,
173
+ } ) ;
158
174
} ) ;
159
175
} ) ;
160
- it ( 'inserts warning comment for unknown messages' , async ( ) => {
161
- await expectTransform ( {
162
- codeBefore : `invariant(condition, ${ JSON . stringify ( NEW_MSG ) } )` ,
163
- codeExpect : `
176
+ } ) ;
177
+ describe ( 'devInvariant' , ( ) => {
178
+ describe ( '{extractCodes: true, noMinify: false}' , ( ) => {
179
+ const opts = { extractCodes : true , noMinify : false } ;
180
+ it ( 'inserts known and extracts unknown message codes' , async ( ) => {
181
+ await expectTransform ( {
182
+ codeBefore : `
183
+ devInvariant(condition, ${ JSON . stringify ( NEW_MSG ) } );
184
+ devInvariant(condition, ${ JSON . stringify ( KNOWN_MSG ) } , adj, noun);
185
+ ` ,
186
+ codeExpect : `
187
+ if (!condition) {
188
+ formatProdWarningMessage(1);
189
+ }
190
+ if (!condition) {
191
+ formatProdWarningMessage(0, adj, noun);
192
+ }` ,
193
+ messageMapBefore : KNOWN_MSG_MAP ,
194
+ messageMapExpect : NEW_MSG_MAP ,
195
+ opts,
196
+ } ) ;
197
+ } ) ;
198
+ } ) ;
199
+ describe ( '{extractCodes: true, noMinify: true}' , ( ) => {
200
+ const opts = { extractCodes : true , noMinify : true } ;
201
+ it ( 'inserts known and extracts unknown message codes' , async ( ) => {
202
+ await expectTransform ( {
203
+ codeBefore : `
204
+ devInvariant(condition, ${ JSON . stringify ( NEW_MSG ) } );
205
+ devInvariant(condition, ${ JSON . stringify ( KNOWN_MSG ) } , adj, noun);
206
+ ` ,
207
+ codeExpect : `
208
+ if (!condition) {
209
+ formatDevErrorMessage(\`A new invariant\`);
210
+ }
211
+ if (!condition) {
212
+ formatDevErrorMessage(\`A \${adj} message that contains \${noun}\`);
213
+ }` ,
214
+ messageMapBefore : KNOWN_MSG_MAP ,
215
+ messageMapExpect : NEW_MSG_MAP ,
216
+ opts,
217
+ } ) ;
218
+ } ) ;
219
+ } ) ;
220
+ describe ( '{extractCodes: false, noMinify: false}' , ( ) => {
221
+ const opts = { extractCodes : false , noMinify : false } ;
222
+ it ( 'inserts known message' , async ( ) => {
223
+ await expectTransform ( {
224
+ codeBefore : `devInvariant(condition, ${ JSON . stringify (
225
+ KNOWN_MSG ,
226
+ ) } , adj, noun)`,
227
+ codeExpect : `
228
+ if (!condition) {
229
+ formatProdWarningMessage(0, adj, noun);
230
+ }
231
+ ` ,
232
+ messageMapBefore : KNOWN_MSG_MAP ,
233
+ messageMapExpect : KNOWN_MSG_MAP ,
234
+ opts,
235
+ } ) ;
236
+ } ) ;
237
+ it ( 'inserts warning comment for unknown messages' , async ( ) => {
238
+ await expectTransform ( {
239
+ codeBefore : `devInvariant(condition, ${ JSON . stringify ( NEW_MSG ) } )` ,
240
+ codeExpect : `
164
241
/*FIXME (minify-errors-in-prod): Unminified error message in production build!*/
165
242
if (!condition) {
166
243
formatDevErrorMessage(\`A new invariant\`);
167
244
}
168
245
` ,
169
- messageMapBefore : KNOWN_MSG_MAP ,
170
- messageMapExpect : KNOWN_MSG_MAP ,
171
- opts,
246
+ messageMapBefore : KNOWN_MSG_MAP ,
247
+ messageMapExpect : KNOWN_MSG_MAP ,
248
+ opts,
249
+ } ) ;
172
250
} ) ;
173
251
} ) ;
174
252
} ) ;
0 commit comments